netguard

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 10 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 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

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

func IsAddressNotAllowed(err error) bool

IsAddressNotAllowed reports whether err's chain holds an *AddressNotAllowedError (through *url.Error and *net.OpError too).

func IsGlobal

func IsGlobal(addr netip.Addr) bool

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

func ParseAllowlist(entries []string) (*Allowlist, error)

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.

func (*Allowlist) Contains

func (a *Allowlist) Contains(addr netip.Addr) bool

Contains reports whether addr falls in an allowlisted range. IPv4-mapped addresses are unmapped first; an address with a zone never matches.

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

func (g *Guard) CheckURL(ctx context.Context, u *url.URL) error

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

func (g *Guard) DialContext(ctx context.Context, network, address string) (net.Conn, error)

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.

type Resolver

type Resolver interface {
	LookupNetIP(ctx context.Context, network, host string) ([]netip.Addr, error)
}

Resolver resolves host names. *net.Resolver satisfies it.

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.

Jump to

Keyboard shortcuts

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