Documentation
¶
Overview ¶
Package netguard refuses connections to internal and reserved addresses, so a destination a caller does not trust cannot reach the host's own network.
A dial guard sees the address being connected to, which is the proxy's when the client uses one, so a guarded http.Transport should set Proxy to nil.
Index ¶
- Variables
- func BlockInternal(_, address string, _ syscall.RawConn) error
- func CheckHost(host string) error
- func IsBlockedIP(ip net.IP) bool
- func NewDialControl(blockPrivate bool, blocked, allowed []netip.Prefix) func(network, address string, c syscall.RawConn) error
- func ParseCIDR(cidr string) (netip.Prefix, error)
- func ReportOnly(control func(network, address string, c syscall.RawConn) error, ...) func(network, address string, c syscall.RawConn) error
- func ReportOnlyContext(control func(network, address string, c syscall.RawConn) error, ...) func(ctx context.Context, network, address string, c syscall.RawConn) error
Constants ¶
This section is empty.
Variables ¶
var ErrBlockedAddress = errors.New("blocked destination address")
ErrBlockedAddress marks a destination the guard refuses. It threads through a transport's url.Error, so callers can classify it as permanent.
Functions ¶
func BlockInternal ¶
BlockInternal is a net.Dialer Control function that refuses every address IsBlockedIP reports, and any zoned address.
func CheckHost ¶
CheckHost rejects a URL host that is localhost, a blocked IP literal, or an IP literal with a zone. A port, and brackets around an IPv6 literal, are accepted. It only catches configuration mistakes early: host names, and IPv4 spellings netip.ParseAddr rejects such as 127.1, pass here and are checked by a dial guard once resolved.
func IsBlockedIP ¶
IsBlockedIP reports whether ip falls in a range a guarded client must never dial: loopback, RFC1918 private and IPv6 ULA (fc00::/7), link-local unicast (including the 169.254.169.254 cloud-metadata IP and fe80::/10), link-local multicast, the unspecified address, any multicast, and the reserved ranges listed in blockedPrefixes. An address in the NAT64 well-known prefix is judged by the IPv4 address it carries. A nil or malformed ip is reported as blocked.
func NewDialControl ¶
func NewDialControl(blockPrivate bool, blocked, allowed []netip.Prefix) func(network, address string, c syscall.RawConn) error
NewDialControl returns a net.Dialer Control function that refuses addresses in blocked, and private ones when blockPrivate is set, unless they are also in allowed. An allowed address is never refused, so a broad allowed range such as 0.0.0.0/0 also lets loopback and the cloud metadata address through. Prefixes should come from ParseCIDR.
func ParseCIDR ¶
ParseCIDR parses a range for NewDialControl. A dialed address is unmapped before matching, so an IPv4-mapped range is converted to its IPv4 range.
func ReportOnly ¶
func ReportOnly( control func(network, address string, c syscall.RawConn) error, report func(address string, err error), ) func(network, address string, c syscall.RawConn) error
ReportOnly wraps a net.Dialer Control function so that an address it refuses with ErrBlockedAddress is passed to report and the connection goes ahead. Other errors from control are returned unchanged.
func ReportOnlyContext ¶
func ReportOnlyContext( control func(network, address string, c syscall.RawConn) error, report func(ctx context.Context, address string, err error), ) func(ctx context.Context, network, address string, c syscall.RawConn) error
ReportOnlyContext is ReportOnly for net.Dialer.ControlContext, passing the dial context to report. Under http.Transport that context carries the values of the request that started the dial; a request that reuses a pooled connection does not dial, so it is not reported.
Types ¶
This section is empty.