Help us shape the future of Go! Take the 2026 Go Developer Survey

safehttp

package
v1.321.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package safehttp builds HTTP clients for fetching user-chosen URLs. Every connection they open, including the ones that follow redirects, must target a public unicast address.

Index

Constants

This section is empty.

Variables

View Source
var ErrBlockedDestination = errors.New("destination is not a public address")

ErrBlockedDestination reports a connection refused because its address is not public.

Functions

func DialControl

func DialControl(allow func(net.IP) bool) func(network, address string, c syscall.RawConn) error

DialControl returns a net.Dialer Control function that refuses, after DNS resolution, every address allow rejects. Nil allow means IsPublicIP. Rejections wrap ErrBlockedDestination.

func IsPublicIP

func IsPublicIP(ip net.IP) bool

IsPublicIP reports whether ip is a public unicast address. It rejects loopback, private and ULA (fc00::/7), unspecified, link-local (which holds the 169.254.169.254 cloud metadata address), multicast and CGNAT addresses.

func NewClient

func NewClient(cfg Config) *http.Client

NewClient returns a client that checks every dialed address after DNS resolution, so redirects and rebinding hostnames cannot reach an address Allow rejects. Redirects may only use http or https.

Types

type Config

type Config struct {
	// Timeout bounds the dial and the whole request; zero means no limit.
	Timeout time.Duration
	// MaxRedirects is the number of redirects followed before failing.
	MaxRedirects int
	// Allow decides which dialed addresses are accepted. Nil means IsPublicIP.
	// Only tests that talk to loopback servers set it.
	Allow func(net.IP) bool
}

Config sets the limits of a client returned by NewClient.

Jump to

Keyboard shortcuts

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