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 ¶
const ( DefaultTimeout = 30 * time.Second DefaultMaxBytes = 16 << 20 )
Defaults.
Variables ¶
This section is empty.
Functions ¶
func CheckURL ¶
CheckURL validates scheme, host and the absence of embedded credentials. It returns E_PERMISSION-coded errors suitable for users.
func IsLoopbackHost ¶
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 (*Client) CloseIdleConnections ¶
func (c *Client) CloseIdleConnections()
CloseIdleConnections closes connections that are kept alive but unused.
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.