Help us shape the future of Go! Take the 2026 Go Developer Survey

sandboxnet

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package sandboxnet implements the sandbox network allowlist: entry parsing and matching, and the filtering HTTP proxy that sandboxed commands use.

Index

Constants

View Source
const (
	ReasonNotAllowed = "host not in sandbox_allowed_domains"
	ReasonNonPublic  = "host resolves to a loopback, link-local or private address"
)

Denial reasons.

View Source
const ReasonLockdown = "host not in untrusted_content.lockdown.allowed_domains (the run has read untrusted content)"

ReasonLockdown is the denial reason for a host the proxy's rules allow but the untrusted-content lockdown allowlist doesn't (issue #171).

Variables

This section is empty.

Functions

func IsNonPublic

func IsNonPublic(a netip.Addr) bool

IsNonPublic reports whether a is a loopback, private, link-local (cloud metadata), shared, multicast or unspecified address: one that is refused after resolution unless listed literally. The web_fetch tool shares it.

func NormalizeHost

func NormalizeHost(h string) string

NormalizeHost lowercases h and strips a trailing dot and IPv6 brackets, the form Rule.Allows expects.

Types

type Denial

type Denial struct {
	Host   string
	Port   int
	Reason string
}

Denial is one request the proxy refused.

type Proxy

type Proxy struct {

	// Resolve and Dial default to the system resolver and a net.Dialer;
	// tests replace them.
	Resolve func(ctx context.Context, host string) ([]netip.Addr, error)
	Dial    func(ctx context.Context, network, addr string) (net.Conn, error)
	// contains filtered or unexported fields
}

Proxy is an HTTP proxy (CONNECT for HTTPS, absolute-URI requests for plain HTTP) that forwards only to hosts its rules allow. It dials directly and never chains to an upstream proxy.

func NewLockdownProxy added in v0.9.0

func NewLockdownProxy(rules []Rule, ln net.Listener) *Proxy

NewLockdownProxy returns a proxy whose only allowlist is rules, the untrusted_content.lockdown.allowed_domains entries (issue #171). The lockdown starts it for a sandbox_network sandbox, whose commands lose their direct network at taint. It counts as narrowed from the start, and a refused host names untrusted_content.lockdown.allowed_domains rather than sandbox_allowed_domains.

func NewProxy

func NewProxy(rules []Rule, ln net.Listener) *Proxy

NewProxy returns a proxy for rules that serves on ln once Serve is called.

func (*Proxy) Addr

func (p *Proxy) Addr() net.Addr

Addr is the listener's address.

func (*Proxy) Close

func (p *Proxy) Close() error

Close stops the listener and every open connection.

func (*Proxy) Narrow added in v0.9.0

func (p *Proxy) Narrow(rules []Rule)

Narrow restricts the proxy for the rest of its life to hosts that match both its own rules and rules; an empty rules refuses everything. Every upstream connection opened before the call is closed, so a tunnel opened before the restriction can't outlive it. A connection that is being dialed while Narrow runs is checked again against the narrowed rules before it is used. Narrowing again replaces rules but never widens past the proxy's own rules.

func (*Proxy) Narrowed added in v0.9.0

func (p *Proxy) Narrowed() bool

Narrowed reports whether Narrow has been called.

func (*Proxy) Serve

func (p *Proxy) Serve()

Serve serves until Close.

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Proxy) TakeDenials

func (p *Proxy) TakeDenials() []Denial

TakeDenials returns the requests refused since the last call.

type Rule

type Rule struct {
	Host     string     // lowercase hostname, or the suffix after "*." when Wildcard
	Wildcard bool       // matches subdomains of Host only
	Addr     netip.Addr // set for IP literals
	Port     int        // 0 means 80 and 443
}

Rule is one parsed sandbox_allowed_domains entry.

func ParseRule

func ParseRule(entry string) (Rule, error)

ParseRule parses an entry: a hostname, "*.example.com" (subdomains only) or an IP literal, with an optional ":port" (IPv6 as "[::1]:port").

func ParseRules

func ParseRules(entries []string) ([]Rule, error)

ParseRules parses entries, naming the first invalid one.

func (Rule) Allows

func (r Rule) Allows(host string, port int) bool

Allows reports whether the rule matches host (a hostname or IP literal, already normalized by normalizeHost) and port.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL