safehttp

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package safehttp provides outbound HTTP clients for URLs selected by remote servers. It prevents public endpoints from pivoting requests into local or private networks while preserving explicitly configured local development.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Policy

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

Policy binds remotely selected restricted destinations to an explicitly configured URL. Public destinations remain available in every policy.

func NewPolicy

func NewPolicy(trustedURL string) (*Policy, error)

NewPolicy constructs a destination policy from an operator-selected URL. The selected hostname may resolve into a private network, but that permission does not extend to server-discovered hostnames. Explicit localhost opts into loopback; another restricted IP opts into that exact address only.

func (*Policy) Client

func (p *Policy) Client(timeout time.Duration, redirects RedirectMode) *http.Client

Client returns an HTTP client whose redirects and connections are checked by the policy. Environment proxies are intentionally disabled because a proxy would resolve the final destination outside this process's validated dial.

func (*Policy) Transport

func (p *Policy) Transport() *http.Transport

Transport returns a proxy-free transport whose dialer validates and pins the destination address selected for each new connection.

func (*Policy) ValidateURL

func (p *Policy) ValidateURL(ctx context.Context, rawURL string) error

ValidateURL checks a URL before handing it to an external user agent such as a browser. HTTP clients must still use Client so validation and dialing share the same resolved address.

type RedirectMode

type RedirectMode uint8

RedirectMode controls whether a client may follow redirects to a new origin.

const (
	// PublicRedirects permits redirects whose destinations satisfy the policy.
	PublicRedirects RedirectMode = iota
	// SameOriginRedirects additionally requires every redirect to retain the
	// original request's scheme, host, and effective port.
	SameOriginRedirects
)

Jump to

Keyboard shortcuts

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