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

safefetch

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package safefetch is the outbound-network guard shared by plugin http.fetch and the WordPress importer: it resolves a host itself, refuses private, loopback, link-local, metadata and other non-public addresses, and connects to the checked IP literal so DNS cannot answer differently between the check and the connect (rebinding).

Index

Constants

This section is empty.

Variables

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

ErrBlocked is wrapped by every policy refusal (address, scheme, redirect).

Functions

func BlockedAddr

func BlockedAddr(ip netip.Addr) bool

BlockedAddr reports whether a connection to ip must be refused.

func CheckScheme

func CheckScheme(u *url.URL) error

CheckScheme refuses anything but http and https, and credentials in the URL.

func DefaultLookup

func DefaultLookup(ctx context.Context, host string) ([]netip.Addr, error)

DefaultLookup uses the system resolver.

func Errorf

func Errorf(format string, a ...any) error

Errorf returns an error wrapping ErrBlocked.

func NewClient

func NewClient(o ClientOptions) *http.Client

NewClient returns an http.Client whose every connection (and every redirect hop, which must stay on http/https) goes through the Dialer guard.

Types

type ClientOptions

type ClientOptions struct {
	AllowPrivate bool
	Lookup       Lookup
	Timeout      time.Duration // per request; 0 means 30s
	MaxRedirects int           // 0 means 5
}

ClientOptions configures NewClient.

type Dialer

type Dialer struct {
	Lookup Lookup // nil: DefaultLookup
	// AllowPrivate skips the address ranges. For tests and for importing from
	// a site on a private network, never for plugin traffic.
	AllowPrivate bool
	Net          net.Dialer
}

Dialer connects only to public addresses.

func (*Dialer) DialContext

func (d *Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error)

DialContext resolves, checks and dials the IP literal.

type Lookup

type Lookup func(ctx context.Context, host string) ([]netip.Addr, error)

Lookup resolves a host name to addresses.

Jump to

Keyboard shortcuts

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