netguard

package
v0.6.124 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 10 Imported by: 1 module, 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 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 CheckURL added in v0.6.109

func CheckURL(endpoint *url.URL, hosts []string) error

CheckURL refuses (ErrForbiddenURL) an endpoint that is not a public service's own: not https, with userinfo, on a port other than 443 (or an empty one after ":"), at an IP literal, or at a host outside hosts (exact or *.suffix; empty: any name). Only the scheme, host and port are named in the error: a path or query may carry a token.

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