Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // PrivateIPNetworks lists standard IP networks used for private networks. PrivateIPNetworks = []*net.IPNet{ CIDR("0.0.0.0/32"), CIDR("10.0.0.0/8"), CIDR("100.64.0.0/10"), CIDR("127.0.0.0/8"), CIDR("169.254.0.0/16"), CIDR("172.16.0.0/12"), CIDR("192.168.0.0/16"), CIDR("fc00::/7"), CIDR("fd00::/8"), CIDR("fe80::/10"), CIDR("::1/128"), } )
Functions ¶
func CIDR ¶
CIDR is like net.ParseCIDR but panics if the input is invalid. This function is useful to initialize lists of CIDRs without having to check errors.
func HasRestrictedNetworkBypass ¶ added in v1.0.0
HasRestrictedNetworkBypass checks whether or not the context has the value set to bypass restricting connections.
func IPAddressOf ¶
IPAddressOf extracts the IP address of addr, or returns nil if none were found.
Types ¶
type AddrCheck ¶
type AddrCheck interface { // Check validates the address passed as argument, returning a non-nil error // if it did not pass. Check(net.Addr) error }
AddrCheck is an interface used to abstract the logic of validating network addresses.
Implementations of AddrCheck must be safe to use concurrently from multiple goroutines.
type Allowlist ¶ added in v1.0.0
Allowlist is an implementation of the AddrCheck interface which verifies that addresses belong to one of the IP networks it contains.
type Denylist ¶ added in v1.0.0
Denylist is an implementation of the AddrCheck interface which verifies that addresses don't belong to one of the IP networks it contains.
type DialFunc ¶
DialFunc is an alias for the signature of the functions used to establish network connections.
func RestrictedDial ¶
RestrictedDial constructs a dial function which validates the address that it establishes connections to.
A typical use case for this function is to pass checks that either allowlists or denylists IP networks, to prevent access to private networks for example:
transport := http.DefaultTransport.(*http.Transport) transport.DialContext = netsec.RestrictedDial(transport.DialContext, netsec.Denylist(netset.PrivateIPAddresses), )
The implementation protects the program from DNS rebinding attacks because it calls the underlying dial function with the address that it validated, not the address that the program originally dialed.
type RestrictedDialer ¶
type RestrictedDialer struct { // The dial function used to establish network connections. DialFunc func(context.Context, string, string) (net.Conn, error) // List of checks that the dialer is going to apply to the network // addresses that it's attempting to connect to. Checks []AddrCheck // The resolver used to translate host names into network addresses. // // If nil, net.DefaultResolver is used. Resolver interface { LookupIPAddr(context.Context, string) ([]net.IPAddr, error) } }
RestrictedDialer