safehttp

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 12 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 safehttp performs bounded, SSRF-safe HTTP fetches for untrusted URLs (registry indexes, remote pipe definitions, std:http-source):

  • HTTPS only; plain HTTP is allowed for loopback hosts only.
  • The address actually dialled is checked (after DNS resolution, so DNS rebinding does not help): requests to public hosts can never reach loopback, private, link-local, CGNAT, multicast or unspecified addresses, and requests to loopback hosts can only reach loopback.
  • Redirects are limited, re-validated per hop, and may not downgrade to HTTP or cross from a public host to a loopback one.
  • Time and response size are bounded. No proxy from the environment, no cookies, no credentials unless the caller sets a header explicitly.

Index

Constants

View Source
const (
	DefaultTimeout  = 30 * time.Second
	DefaultMaxBytes = 16 << 20
)

Defaults.

Variables

This section is empty.

Functions

func CheckURL

func CheckURL(raw string) (*url.URL, error)

CheckURL validates scheme, host and the absence of embedded credentials. It returns E_PERMISSION-coded errors suitable for users.

func HostPort

func HostPort(u *url.URL) string

HostPort returns host:port of a URL with the scheme's default port.

func IsLoopbackHost

func IsLoopbackHost(host string) bool

IsLoopbackHost reports whether a URL host is a loopback name or literal.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an SSRF-safe HTTP client.

func New

func New(o Options) *Client

New creates a Client.

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections closes connections that are kept alive but unused.

func (*Client) Do

func (c *Client) Do(req *http.Request) (*http.Response, error)

Do validates the request URL and performs the request. The caller must close the response body. Errors carry code E_PERMISSION (policy) or E_RUNTIME (transport).

func (*Client) Get

func (c *Client) Get(ctx context.Context, rawURL string, header http.Header) ([]byte, *http.Response, error)

Get fetches a URL fully into memory, bounded by Options.MaxBytes. Non-2xx statuses are errors.

type Options

type Options struct {
	Timeout  time.Duration // whole request including body (default 30 s); <0 disables (streaming)
	MaxBytes int64         // response body limit for Get (default 16 MiB)
	// AllowHost, when set, must approve every host:port contacted (initial
	// request and redirects). Used to enforce network grants.
	AllowHost func(hostport string) bool
	// HeaderTimeout bounds the wait for response headers once the request
	// has been written (default 30 s); <0 disables it, for streaming
	// requests whose response only arrives when a long body has ended.
	HeaderTimeout time.Duration
	// NoRedirects makes every redirect an error (requests that carry
	// credentials or a streaming body).
	NoRedirects bool
}

Options tunes a Client.

Jump to

Keyboard shortcuts

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