Documentation
¶
Overview ¶
Package netguard keeps probes and webhook senders away from private networks (SSRF).
The check runs in the dialer's Control hook, on the IP address the kernel is about to connect to, after DNS resolution and for every connection attempt. A name that resolves to a public address at one moment and a private one the next (DNS rebinding) is refused on the second connection, and so is a redirect to a private host: there is no separate validate-then-connect step to race. Redirect targets and proxies go through the same dialer, so callers must not configure an HTTP proxy.
Index ¶
- Constants
- Variables
- func Blocked(ip netip.Addr, allowPrivate bool) bool
- func DeniedTotal() map[string]uint64
- func DialContext(d *net.Dialer) func(ctx context.Context, network, addr string) (net.Conn, error)
- func Guarded(purpose string, timeout time.Duration) *http.Client
- func GuardedNoRedirect(purpose string, timeout time.Duration) *http.Client
- func MetricsText() string
- func New(o Options) *net.Dialer
- func NewClient(p Policy) *http.Client
- func NewClientWith(p Policy, o Options, tune func(*http.Transport)) *http.Client
- func SafeDialer(allowPrivate bool) *net.Dialer
- type BlockedError
- type ErrEgressDenied
- type FetchOptions
- type Fetched
- type Options
- type Policy
- type Proxy
Constants ¶
const ( DefaultMaxBytes = 1 << 20 DefaultTimeout = 10 * time.Second )
Fetch limits for FetchOptions' zero values.
Variables ¶
var DefaultFamily string
DefaultFamily is the Family of SafeDialer; a private probe sets it once at startup from its location's prefer_ip_family. shortcut: process-wide, set before the first dial; per-location families need a dialer per engine.
var ErrBlocked = errors.New("target address is not allowed")
ErrBlocked is returned (wrapped) when a connection target is not allowed.
var ErrTooLarge = errors.New("response is over the size cap")
ErrTooLarge is returned when the body is over the cap.
Functions ¶
func Blocked ¶
Blocked reports whether ip may not be connected to. With allowPrivate (private probes), only the never-reachable ranges are refused.
func DeniedTotal ¶
DeniedTotal returns wt_egress_denied_total per purpose.
func DialContext ¶
DialContext is a convenience for http.Transport.
func Guarded ¶
Guarded is the first-party client (FromEnv policy: private ranges allowed, WT_AIRGAP honoured) with the given overall timeout (0 = none). Redirects are followed as net/http does by default.
func GuardedNoRedirect ¶
GuardedNoRedirect is Guarded but returns the first response instead of following redirects.
func MetricsText ¶
func MetricsText() string
MetricsText renders wt_egress_denied_total in Prometheus text format for a /metrics handler.
func NewClient ¶
NewClient is the only sanctioned way to build an outbound HTTP client: the host is checked against the policy before DNS, and the resolved address against the private-range rules at connect (redirects included). Proxies are not used. Callers set Timeout on the result.
func NewClientWith ¶
NewClientWith is NewClient with explicit dialer Options (Block, Resolver, Family, Timeout) and an optional hook to tune the transport (TLS config, keep-alives, header timeout). The dialer is private-allowing when either o or p says so.
func SafeDialer ¶
SafeDialer is the dialer for public probe locations (allowPrivate false) and for private probes (true).
Types ¶
type BlockedError ¶
BlockedError says which address was refused; it matches ErrBlocked with errors.Is.
func (*BlockedError) Error ¶
func (e *BlockedError) Error() string
func (*BlockedError) Is ¶
func (e *BlockedError) Is(target error) bool
type ErrEgressDenied ¶
type ErrEgressDenied struct{ Host, Purpose string }
ErrEgressDenied is returned (wrapped) when the policy refuses a host.
func (*ErrEgressDenied) Error ¶
func (e *ErrEgressDenied) Error() string
type FetchOptions ¶
type FetchOptions struct {
// ETag is sent as If-None-Match.
ETag string
// MaxBytes caps the body (default 1 MiB).
MaxBytes int64
// Timeout bounds the whole fetch (default 10 s).
Timeout time.Duration
// Dialer replaces the public-only guarded dialer (tests).
Options Options
}
FetchOptions bound one fetch.
type Fetched ¶
type Fetched struct {
// NotModified is true for a 304: there is no body to parse.
NotModified bool
Body []byte
ETag string
}
Fetched is the result of Fetch.
type Options ¶
type Options struct {
// AllowPrivate lets a private probe reach private address space.
AllowPrivate bool
// Block replaces the address policy (tests use it to make local listeners "public").
Block func(netip.Addr) bool
// Resolver replaces the system resolver (tests).
Resolver *net.Resolver
// Timeout bounds one connection attempt (default 10 s).
Timeout time.Duration
// Family "ipv4" or "ipv6" restricts connections to that family; the dialer's happy-eyeballs
// still tries the other family first if the resolver lists it, and the attempt is refused.
Family string
}
Options configure a guarded dialer. The zero value is a public-only dialer.
type Policy ¶
type Policy struct {
// AllowHosts are exact host names, IP literals or "*.suffix" patterns (no ports).
AllowHosts []string
// AllowPrivate lets the client reach private address space.
AllowPrivate bool
// DenyAll refuses every host that is not in AllowHosts.
DenyAll bool
// Purpose names the caller for the denied counter; unknown values count as "other".
Purpose string
}
Policy is the egress policy of one HTTP client (R28). With DenyAll, only AllowHosts may be dialled; otherwise any public host may (private ranges stay blocked unless AllowPrivate).
func FromEnv ¶
FromEnv is the first-party policy: unrestricted (private ranges allowed, as in-cluster calls need) unless WT_AIRGAP=1, when every host except those in WT_EGRESS_ALLOW (comma separated) is denied.
func (Policy) HostAllowed ¶
HostAllowed reports whether the policy lets host (no port) be dialled.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy is a local forward proxy (HTTP and CONNECT) whose upstream dialer applies the address policy at connect time, so a child process such as k6 that honours HTTP_PROXY/HTTPS_PROXY reaches no private range, metadata address or rebound name on a public location. shortcut: processes that bypass proxies for loopback (Go's localhost rule) are not covered; upgrade to a network namespace if scripts must be fully contained.