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

httpclient

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package httpclient builds outbound HTTP clients for URLs that users control (webhooks, link previews, avatars by URL). Safe refuses to connect to loopback, private, link-local (cloud metadata), multicast and other internal addresses. The check runs on the resolved IP at connect time, so DNS rebinding and redirects to internal hosts are blocked too.

resp, err := httpclient.Safe(httpclient.Options{}).Get(userURL)

Index

Constants

This section is empty.

Variables

View Source
var ErrBlockedAddress = errors.New("httpclient: destination address is not allowed")

ErrBlockedAddress is returned when the destination is an internal address.

View Source
var ErrBodyTooLarge = errors.New("httpclient: response body too large")

ErrBodyTooLarge is returned by reads past Options.MaxBody.

Functions

func Allowed

func Allowed(ip netip.Addr, allow []netip.Prefix) bool

Allowed reports whether ip may be reached with allow as extra prefixes.

func Get

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

Get is a convenience for Safe(Options{}).Get with a context.

func Safe

func Safe(o Options) *http.Client

Safe returns a client that only reaches public addresses over http(s).

Types

type Options

type Options struct {
	// Timeout bounds the whole request (default 10s).
	Timeout time.Duration
	// MaxBody bounds response bodies; reads past it fail (default 10 MiB).
	MaxBody int64
	// MaxRedirects (default 5).
	MaxRedirects int
	// Allow lists extra prefixes that may be reached (e.g. an internal
	// service network). Use sparingly.
	Allow []netip.Prefix
}

Options tune Safe.

Jump to

Keyboard shortcuts

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