netutil

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0 modules, 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 netutil provides SSRF-safe outbound HTTP helpers.

Index

Constants

View Source
const (
	// HTTP2ReadIdleTimeout is how long an HTTP/2 connection may receive
	// nothing before it is pinged.
	HTTP2ReadIdleTimeout = 30 * time.Second
	// HTTP2PingTimeout is how long an HTTP/2 health-check ping may go
	// unanswered before the connection is closed.
	HTTP2PingTimeout = 15 * time.Second
)
View Source
const EnvAllowPrivate = "OPENARTIFACT_ALLOW_PRIVATE_UPSTREAMS"

EnvAllowPrivate, when "true", permits outbound requests to private, loopback and link-local addresses (for on-prem upstream registries). Cloud metadata addresses (169.254.0.0/16, fd00:ec2::/16) remain blocked.

Variables

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

ErrBlockedAddress is returned when a destination address is not permitted.

View Source
var ErrBodyStalled = errors.New("upstream response body stalled")

ErrBodyStalled is returned by a response body read that received no data for the configured stall timeout.

View Source
var ErrCannotResolve = errors.New("cannot resolve host")

ErrCannotResolve is returned when the host name does not resolve. Unlike ErrBlockedAddress it says nothing about the destination, only that the resolver or the network is not answering.

Functions

func GuardTransport added in v0.15.0

func GuardTransport(tr *http.Transport)

GuardTransport makes tr connect only to addresses ValidateOutboundURLContext would accept, checked where the connection is made (dialControl), so a DNS answer that changes after a save-time check cannot reach a private or metadata address. The proxies tr.Proxy names are exempt: the operator chose them, and a proxy resolves and connects to the destination under its own egress policy. tr keeps its other settings.

func IsHTTPSOrLoopbackURL added in v0.16.1

func IsHTTPSOrLoopbackURL(raw string) bool

IsHTTPSOrLoopbackURL reports whether raw is an https URL, or an http URL whose host is loopback.

func IsLoopbackHost added in v0.16.1

func IsLoopbackHost(host string) bool

IsLoopbackHost reports whether host (no port) is "localhost" or a loopback IP literal. Used to allow plain http only for local development endpoints.

func NewDownloadClient added in v0.7.0

func NewDownloadClient(timeout time.Duration, trustedHost string) *http.Client

NewDownloadClient returns a client for fetching one operator-configured URL, such as a release archive on a mirror.

Unlike SafeHTTPClient it connects to trustedHost (host or host:port of the configured URL) even when that is on a private network, because the operator chose it, and it honours HTTP_PROXY, HTTPS_PROXY and NO_PROXY like the rest of an enterprise network expects. A redirect is a different matter: the server, not the operator, picks its target, so a redirect must stay on https, go to at most 3 hops, and either return to trustedHost or reach only addresses ValidateOutboundURLContext allows (no private, loopback, link-local or metadata ranges). That is enforced where the connection is made, so a DNS answer that changes between a check and the dial cannot get around it. When a proxy serves a request the proxy, not this process, resolves and connects to the host, so only the scheme and literal IP addresses can be checked here and the proxy's own egress policy applies.

func ParseNetworks added in v0.5.0

func ParseNetworks(entries []string) ([]*net.IPNet, error)

ParseNetworks parses CIDR blocks, accepting bare IPs as /32 (/128). Blank entries are skipped; any unparsable entry is an error.

func SafeHTTPClient

func SafeHTTPClient(timeout time.Duration) *http.Client

SafeHTTPClient returns a client that refuses to connect to blocked addresses (checked at dial time, defeating DNS rebinding) and caps and re-validates redirects, dropping every header but a small allowlist (and refusing to replay a request body) on cross-origin redirects.

func SafeHTTPClientWithHeaderTimeout added in v0.4.0

func SafeHTTPClientWithHeaderTimeout(timeout, responseHeaderTimeout time.Duration) *http.Client

SafeHTTPClientWithHeaderTimeout is SafeHTTPClient with an additional bound on the wait between sending a request and receiving the response headers (0 means unbounded, leaving only the overall timeout). Body transfer is not covered by it, so large downloads are unaffected.

func SafeHTTPClientWithOptions added in v0.21.0

func SafeHTTPClientWithOptions(timeout time.Duration, opts ClientOptions) *http.Client

SafeHTTPClientWithOptions is SafeHTTPClient with the exceptions in opts.

func SafeHTTPClientWithStallTimeout added in v0.13.0

func SafeHTTPClientWithStallTimeout(timeout, responseHeaderTimeout, stallTimeout time.Duration) *http.Client

SafeHTTPClientWithStallTimeout is SafeHTTPClientWithHeaderTimeout with a no-progress bound on response bodies: a body read that stays blocked for stallTimeout without receiving a byte fails with ErrBodyStalled and the request is cancelled (0 disables it). The timer only runs while a Read is waiting for the upstream, so a slow consumer is never mistaken for a stalled upstream, and large downloads that keep making progress are unaffected.

HTTP/2 connections are health-checked: an idle connection is pinged, and one that does not answer is closed, so requests on a dead connection fail fast instead of waiting for TCP retransmit exhaustion.

func ValidateOutboundURLContext

func ValidateOutboundURLContext(ctx context.Context, raw string) error

ValidateOutboundURLContext checks scheme, host and (resolved) addresses of raw, resolving the host under ctx.

func ValidateOutboundURLContextWith added in v0.21.0

func ValidateOutboundURLContextWith(ctx context.Context, raw string, private bool) error

ValidateOutboundURLContextWith is ValidateOutboundURLContext for a caller that may reach private, loopback and CGNAT addresses (private); metadata and link-local addresses stay refused.

Types

type ClientOptions added in v0.21.0

type ClientOptions struct {
	// AllowPrivate lets the client connect to private, loopback and CGNAT
	// addresses (a cluster-internal issuer) without the process-wide
	// OPENARTIFACT_ALLOW_PRIVATE_UPSTREAMS. Link-local, cloud metadata and the
	// other always-blocked ranges stay refused.
	AllowPrivate bool
	// RootCAs, when set, replaces the system roots for the TLS handshakes.
	RootCAs *x509.CertPool
	// DisableKeepAlives closes every connection after its request, for clients
	// that are built often and used rarely.
	DisableKeepAlives bool
}

ClientOptions are the per-client exceptions of SafeHTTPClientWithOptions.

Jump to

Keyboard shortcuts

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