netguard

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	DefaultMaxBytes = 1 << 20
	DefaultTimeout  = 10 * time.Second
)

Fetch limits for FetchOptions' zero values.

Variables

View Source
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.

View Source
var ErrBlocked = errors.New("target address is not allowed")

ErrBlocked is returned (wrapped) when a connection target is not allowed.

View Source
var ErrTooLarge = errors.New("response is over the size cap")

ErrTooLarge is returned when the body is over the cap.

Functions

func Blocked

func Blocked(ip netip.Addr, allowPrivate bool) bool

Blocked reports whether ip may not be connected to. With allowPrivate (private probes), only the never-reachable ranges are refused.

func DeniedTotal

func DeniedTotal() map[string]uint64

DeniedTotal returns wt_egress_denied_total per purpose.

func DialContext

func DialContext(d *net.Dialer) func(ctx context.Context, network, addr string) (net.Conn, error)

DialContext is a convenience for http.Transport.

func Guarded

func Guarded(purpose string, timeout time.Duration) *http.Client

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

func GuardedNoRedirect(purpose string, timeout time.Duration) *http.Client

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 New

func New(o Options) *net.Dialer

New returns a dialer whose Control hook refuses blocked addresses.

func NewClient

func NewClient(p Policy) *http.Client

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

func NewClientWith(p Policy, o Options, tune func(*http.Transport)) *http.Client

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

func SafeDialer(allowPrivate bool) *net.Dialer

SafeDialer is the dialer for public probe locations (allowPrivate false) and for private probes (true).

Types

type BlockedError

type BlockedError struct{ Addr netip.Addr }

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.

func Fetch

func Fetch(ctx context.Context, raw string, o FetchOptions) (Fetched, error)

Fetch GETs an https URL through the guarded dialer: the address is checked at connect, for every connection, so private and metadata addresses are refused even behind a public name. Redirects and proxies are not followed. Used for external calendars and metric/symbol sources.

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

func FromEnv(purpose string) Policy

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

func (p Policy) HostAllowed(host string) bool

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.

func NewProxy

func NewProxy(o Options) (*Proxy, error)

NewProxy starts the proxy on 127.0.0.1 with an ephemeral port.

func (*Proxy) Close

func (p *Proxy) Close() error

Close stops the proxy.

func (*Proxy) ServeHTTP

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

ServeHTTP implements http.Handler.

func (*Proxy) URL

func (p *Proxy) URL() string

URL is the value for HTTP_PROXY and HTTPS_PROXY.

Jump to

Keyboard shortcuts

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