Documentation
¶
Overview ¶
Package netguard keeps outbound requests to user-supplied URLs away from internal networks. A URL is vetted when it is accepted (Guard.CheckURL) and again on every new connection (Guard.DialContext): every address a connection is about to be made to must be globally reachable per the IANA special-purpose registries or allowlisted, so a DNS answer that changes in between (rebinding) can't send it to a blocked address. Non-ASCII host names must be given in punycode.
Index ¶
Constants ¶
const ( // ReasonNotGlobal: an address is neither globally reachable nor // allowlisted. ReasonNotGlobal = "not_global" // ReasonNoAddresses: the host resolved to no address at all. ReasonNoAddresses = "no_addresses" )
Reasons an *AddressNotAllowedError carries.
Variables ¶
This section is empty.
Functions ¶
func IsAddressNotAllowed ¶
IsAddressNotAllowed reports whether err's chain holds an *AddressNotAllowedError (through *url.Error and *net.OpError too).
func IsGlobal ¶
IsGlobal reports whether addr is globally reachable per the IANA special-purpose address registries. Unlike netip.Addr.IsGlobalUnicast it rejects shared address space, documentation, benchmarking and reserved ranges, and it judges IPv4-mapped (::ffff:0:0/96), NAT64 (64:ff9b::/96) and 6to4 (2002::/16) addresses by the IPv4 address they embed. Invalid addresses and addresses with a zone are never global.
Types ¶
type AddressNotAllowedError ¶
type AddressNotAllowedError struct {
// Host is the host as given (URL host or dial host), without port.
Host string
// Addr is the refused address; zero when no address is involved
// (ReasonNoAddresses).
Addr netip.Addr
Reason string
}
AddressNotAllowedError is returned when the guard refuses a URL or a connection. It is for server-side handling and logs only: callers map it to a fixed client-visible result and never echo Addr, which can reveal internal DNS. Error() leaves Addr out for the same reason.
func AsAddressNotAllowed ¶
func AsAddressNotAllowed(err error) (*AddressNotAllowedError, bool)
AsAddressNotAllowed returns the *AddressNotAllowedError in err's chain.
func (*AddressNotAllowedError) Error ¶
func (e *AddressNotAllowedError) Error() string
type Allowlist ¶
type Allowlist struct {
// contains filtered or unexported fields
}
Allowlist holds the address ranges a Guard lets connections reach even though they are not globally reachable. It holds IP ranges only. A hostname entry would trust whatever DNS answers for that name, which is exactly what the guard is there to distrust. A nil *Allowlist is empty.
func ParseAllowlist ¶
ParseAllowlist parses allowlist entries: an IP address, a CIDR range, or "localhost" (127.0.0.0/8 and ::1/128). Blank entries are skipped. A /0 range, a range with host bits set, an address with a zone and anything unparsable is an error; all bad entries are reported together. IPv4-mapped IPv6 entries are converted to IPv4, since the guard compares unmapped addresses.
type Guard ¶
type Guard struct {
Allowlist *Allowlist
// Resolver defaults to net.DefaultResolver. Names are always looked up
// in rooted form ("example.com."), so CheckURL and DialContext resolve
// the same name regardless of resolv.conf search domains. DialContext
// uses it only if it is a *net.Resolver.
Resolver Resolver
}
Guard decides which addresses outbound connections may reach: globally reachable ones (IsGlobal) and allowlisted ones. CheckURL fails a host if any resolved address is refused; DialContext skips refused addresses and fails only if none is left. Scheme and port don't matter.
The zero value is usable: no allowlist and net.DefaultResolver. A Guard is safe for concurrent use; don't change its fields once it is in use.
func (*Guard) CheckURL ¶
CheckURL vets a URL before it is accepted. IP-literal hosts are checked as they are; names are resolved and every address in the answer must pass. Passing CheckURL guarantees nothing later, since DNS can change: DialContext checks again on every new connection.
A non-ASCII host is refused rather than guessed at. Callers must map every error (resolver failure, empty answer, non-global address) to the same client-visible result, or the check becomes an oracle for internal DNS. Policy refusals are *AddressNotAllowedError; resolver errors pass through unchanged.
func (*Guard) DialContext ¶
DialContext is an http.Transport DialContext: a net.Dialer with the default transport's timeouts whose Control hook refuses each address before the socket connects to it. The TLS server name and Host header stay the original host name, since the transport derives them from the request URL.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package netguardtest provides an in-process DNS server for tests that resolve host names through the standard library.
|
Package netguardtest provides an in-process DNS server for tests that resolve host names through the standard library. |