WeKnora/internal/sandbox/url_guard.go

177 lines
6.0 KiB
Go

// Package sandbox: outbound URL guard for tenant-supplied endpoints.
//
// Tenants configure their own sandbox control-plane URLs and the server dials
// them. Without a guard that is a Server-Side Request Forgery primitive: the
// most damaging target is the cloud metadata service (169.254.169.254), which
// hands out instance credentials, followed by any internal service reachable
// from the application host.
//
// Defence is two-layered on purpose:
//
// - Validate runs when a config is saved or probed, giving the admin an
// immediate, readable error.
// - DialControl runs at connect time on every request. This is the layer
// that actually holds, because a hostname can resolve to a public address
// during validation and to 169.254.169.254 at dial time (DNS rebinding).
//
// Self-hosted deployments complicate this, so private endpoints are an
// explicit field on each workspace config. Even when enabled, link-local
// ranges — including cloud metadata — stay blocked.
package sandbox
import (
"errors"
"fmt"
"net"
"net/url"
"strings"
"syscall"
)
// ErrUnsafeOutboundURL is returned for any endpoint that uses an unsupported
// scheme or resolves to an address the policy forbids.
var ErrUnsafeOutboundURL = errors.New("sandbox: unsafe outbound URL")
// OutboundURLPolicy decides which addresses tenant-supplied endpoints may
// resolve to.
type OutboundURLPolicy struct {
// AllowPrivate permits loopback and RFC1918 addresses. It never permits
// link-local (169.254.0.0/16), which carries the cloud metadata service.
AllowPrivate bool
}
// DefaultOutboundURLPolicy is the fail-closed policy used by callers that do
// not carry a workspace configuration.
func DefaultOutboundURLPolicy() OutboundURLPolicy {
return OutboundURLPolicy{}
}
// ValidateOutboundURL checks raw against the environment's policy.
func ValidateOutboundURL(raw string) error {
return DefaultOutboundURLPolicy().Validate(raw)
}
func ValidateOutboundURLWithPolicy(raw string, policy OutboundURLPolicy) error {
return policy.Validate(raw)
}
// SafeDialControl is a net.Dialer.Control hook using the environment's policy.
func SafeDialControl(network string, address string, conn syscall.RawConn) error {
return DefaultOutboundURLPolicy().DialControl(network, address, conn)
}
func SafeDialControlForPolicy(policy OutboundURLPolicy) func(string, string, syscall.RawConn) error {
return func(network string, address string, conn syscall.RawConn) error {
return policy.DialControl(network, address, conn)
}
}
// Validate reports whether raw is an acceptable tenant-supplied endpoint. It
// rejects non-HTTP schemes and any host that resolves to a forbidden address.
//
// Callers must ALSO install DialControl on the dialer they use; Validate alone
// cannot close the DNS-rebinding window.
func (p OutboundURLPolicy) Validate(raw string) error {
raw = strings.TrimSpace(raw)
if raw == "" {
return fmt.Errorf("%w: empty URL", ErrUnsafeOutboundURL)
}
parsed, err := url.Parse(raw)
if err != nil {
return fmt.Errorf("%w: %v", ErrUnsafeOutboundURL, err)
}
switch strings.ToLower(parsed.Scheme) {
case "http", "https":
default:
return fmt.Errorf("%w: scheme %q is not allowed", ErrUnsafeOutboundURL, parsed.Scheme)
}
host := parsed.Hostname()
if host == "" {
return fmt.Errorf("%w: missing host", ErrUnsafeOutboundURL)
}
// ".local" is mDNS; "localhost" is only acceptable under the opt-in.
lower := strings.ToLower(host)
if strings.HasSuffix(lower, ".local") {
return fmt.Errorf("%w: host %q is mDNS-local", ErrUnsafeOutboundURL, host)
}
if lower == "localhost" || strings.HasSuffix(lower, ".localhost") {
if !p.AllowPrivate {
return fmt.Errorf(
"%w: host %q is loopback; enable private endpoints for this workspace config",
ErrUnsafeOutboundURL, host,
)
}
return nil
}
// A literal IP is checked directly; a hostname has every resolved address
// checked, since one acceptable answer does not make the rest safe.
if ip := net.ParseIP(host); ip != nil {
return p.checkIP(ip)
}
addrs, err := net.LookupIP(host)
if err != nil {
return fmt.Errorf("%w: cannot resolve %q: %v", ErrUnsafeOutboundURL, host, err)
}
for _, ip := range addrs {
if err := p.checkIP(ip); err != nil {
return fmt.Errorf("%w: host %q resolves to %s", ErrUnsafeOutboundURL, host, ip)
}
}
return nil
}
// DialControl refuses connections to forbidden addresses. It inspects the
// concrete address the kernel is about to connect to, which is what makes it
// immune to DNS rebinding.
func (p OutboundURLPolicy) DialControl(_ string, address string, _ syscall.RawConn) error {
host, _, err := net.SplitHostPort(address)
if err != nil {
return fmt.Errorf("%w: cannot parse dial address %q", ErrUnsafeOutboundURL, address)
}
ip := net.ParseIP(host)
if ip == nil {
return fmt.Errorf("%w: dial address %q is not an IP", ErrUnsafeOutboundURL, host)
}
if err := p.checkIP(ip); err != nil {
return err
}
return nil
}
// checkIP applies the policy to a concrete address.
func (p OutboundURLPolicy) checkIP(ip net.IP) error {
if ip == nil {
return fmt.Errorf("%w: missing address", ErrUnsafeOutboundURL)
}
// Never allowed, even under the private opt-in: link-local carries the
// cloud metadata service, and multicast/unspecified are meaningless here.
if ip.IsUnspecified() ||
ip.IsLinkLocalUnicast() ||
ip.IsLinkLocalMulticast() ||
ip.IsInterfaceLocalMulticast() ||
ip.IsMulticast() {
return fmt.Errorf("%w: address %s is never routable to a sandbox", ErrUnsafeOutboundURL, ip)
}
if ip.IsLoopback() || ip.IsPrivate() || isCarrierGradeNAT(ip) {
if !p.AllowPrivate {
return fmt.Errorf(
"%w: address %s is private; enable private endpoints for this workspace config",
ErrUnsafeOutboundURL, ip,
)
}
}
return nil
}
// isCarrierGradeNAT reports whether ip falls in 100.64.0.0/10, which IsPrivate
// does not cover but which is equally unsuitable as a public endpoint.
func isCarrierGradeNAT(ip net.IP) bool {
v4 := ip.To4()
if v4 == nil {
return false
}
return v4[0] == 100 && v4[1] >= 64 && v4[1] <= 127
}