netguard

package
v0.6.101 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package netguard keeps a service's requests to URLs it was handed — a webhook receiver, a push endpoint, a link to preview — off the installation's own network (SSRF).

client := netguard.Client(netguard.Redirects(5))
res, err := client.Do(req) // errors.Is(err, netguard.ErrForbiddenAddress), ErrForbiddenURL

Every request and every redirect hop is https (Hosts when set), and its connection goes only to a public address: checked on the address dialed, after DNS, so a name that resolves inside — or rebinds to it — is refused. Requests go direct: through a proxy the address dialed would be the proxy's. Redirects are not followed unless Redirects allows them. The client's *url.Error names the whole URL: scrub it before logging when the URL may carry a token.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrForbiddenAddress: the destination resolved to an address that is
	// not public (Forbidden).
	ErrForbiddenAddress = errors.New("netguard: destination is not a public address")
	// ErrForbiddenURL: a request or redirect URL that is not https, or whose
	// host is not among Hosts.
	ErrForbiddenURL = errors.New("netguard: url is not allowed")
	// ErrRedirects: more redirects than Redirects allows.
	ErrRedirects = errors.New("netguard: too many redirects")
)

Errors.

Functions

func Allowed

func Allowed(host string, hosts []string) bool

Allowed reports whether host matches hosts: an exact name or *.suffix; never an IP address, zoned ones included.

func Client

func Client(opts ...Option) *http.Client

Client is an *http.Client over Transport whose every request and redirect is https to an allowed host, traced (OpenTelemetry). Set its Timeout, or use request contexts, to bound a fetch.

func Forbidden

func Forbidden(addr netip.Addr) bool

Forbidden reports whether addr is not public: loopback, private, link-local (169.254.169.254 included), shared, multicast, unspecified, another special-purpose range, IPv6 outside global unicast or embedding IPv4, or zoned (scoped to an interface).

func Transport

func Transport() *http.Transport

Transport dials only public addresses (ErrForbiddenAddress) and goes direct, never through a proxy. It checks no URL: Client does.

Types

type Option

type Option func(*settings)

Option configures Client.

func Hosts

func Hosts(hosts ...string) Option

Hosts limits every request and redirect to these hosts: exact names or *.suffix (Allowed). Without it any public host is allowed.

func Redirects

func Redirects(n int) Option

Redirects follows at most n redirects, each checked as the first request (https, Hosts, a public address); one more is ErrRedirects. Without it (or n <= 0) no redirect is followed: the 3xx response is returned, as a delivery (a webhook, a push) wants.

Jump to

Keyboard shortcuts

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