safehttp

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package safehttp makes HTTP requests to addresses that come from users, such as webhooks, link previews and images, without letting them reach the app's own network (server-side request forgery):

client := safehttp.New(safehttp.Options{})
res, err := client.Get(userURL)

The client only connects to public addresses, checked on the address it actually dials, so a name that resolves to 127.0.0.1 or 10.0.0.5, or a redirect to one, is refused. It ignores proxy settings, which would connect on its behalf, and limits time, redirects, ports and the size of response bodies.

Index

Constants

This section is empty.

Variables

View Source
var ErrBlocked = errors.New("safehttp: blocked")

ErrBlocked is returned, wrapped, for a request to an address or port the client doesn't allow.

View Source
var ErrTooLarge = errors.New("safehttp: response body too large")

ErrTooLarge is returned while reading a response body past MaxBody.

Functions

func Get

func Get(ctx context.Context, url string) (*http.Response, error)

Get fetches url with a default client, bound to ctx.

func New

func New(opts Options) *http.Client

New returns a client that only connects to public addresses.

func Public

func Public(a netip.Addr) bool

Public reports whether a is an address on the public internet: not loopback, private, link-local, multicast, unspecified, carrier-grade NAT or another reserved range.

Types

type Options

type Options struct {
	// Timeout limits a whole request, redirects and body included; the
	// default is 10 seconds.
	Timeout time.Duration
	// MaxBody limits response bodies; the default is 10 MiB.
	MaxBody int64
	// MaxRedirects is how many redirects are followed; the default is 5,
	// and -1 follows none.
	MaxRedirects int
	// Ports are the ports it may connect to; the default is 80 and 443.
	Ports []string
	// Allow lists addresses it may connect to although they aren't public,
	// such as a service of your own on the private network.
	Allow []netip.Prefix
}

Options configure a client. The zero value is a safe default.

Jump to

Keyboard shortcuts

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