requestorigin

package
v4.2.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package requestorigin resolves the scheme, host and client IP of an HTTP request. Forwarding headers (X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-For, X-Real-IP) are honoured only when the TCP peer is one of the configured reverse proxies (server.trusted_proxies); from any other peer they are ignored and the connection decides: https when it is TLS, the Host header, and the peer address.

Every value the kernel builds a URL from (install scripts, subscription links, webhook URLs, the request_scheme and request_host sent to package hosts) must come from here, so a client cannot make the server hand out links to a host of its choosing.

Index

Constants

View Source
const (
	HeaderForwardedProto = "X-Forwarded-Proto"
	HeaderForwardedHost  = "X-Forwarded-Host"
	HeaderForwardedFor   = "X-Forwarded-For"
	HeaderRealIP         = "X-Real-IP"
)

Header names Control reads from a trusted proxy.

Variables

View Source
var DefaultTrustedProxies = []string{"127.0.0.1/32", "::1/128"}

DefaultTrustedProxies is the trust list when server.trusted_proxies is not set: loopback only, so a reverse proxy on the same host (and Control's own UI server, which proxies /api to the API listener) keeps working.

Functions

func IsForwardingHeader

func IsForwardingHeader(name string) bool

IsForwardingHeader reports whether name is a header a proxy uses to describe the original request (X-Forwarded-*, Forwarded, X-Real-IP). The kernel drops them from requests it hands on, after resolving the origin.

func NormalizeHost

func NormalizeHost(value string) (string, bool)

NormalizeHost validates a host[:port] authority and lower-cases it. It rejects anything else: a scheme, a path, user info, spaces or control characters, an empty or out-of-range port, an unbracketed IPv6 literal, and names that are not DNS-like labels or IP addresses.

func SetDefault

func SetDefault(policy *Policy)

SetDefault makes policy the process-wide policy Resolve uses. The server sets it once at startup from server.trusted_proxies.

Types

type Origin

type Origin struct {
	// Scheme is "http" or "https".
	Scheme string
	// Host is a validated host[:port], lower-cased; empty when the request
	// carried no valid host.
	Host string
	// ClientIP is the client address (empty when none could be parsed).
	ClientIP string
	// Forwarded is true when a trusted proxy's headers set Scheme or Host.
	Forwarded bool
}

Origin is where a request was addressed and who sent it.

func Resolve

func Resolve(r *http.Request) Origin

Resolve resolves r with the process-wide policy.

func (Origin) BaseURL

func (o Origin) BaseURL() string

BaseURL is scheme://host, or "" when the host is unknown.

type Policy

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

Policy is a set of trusted proxy networks.

func Default

func Default() *Policy

Default returns the process-wide policy.

func NewPolicy

func NewPolicy(proxies []string) (*Policy, error)

NewPolicy parses proxies (IP addresses or CIDRs). A nil list is the default (DefaultTrustedProxies); an empty, non-nil list trusts no proxy.

func (*Policy) ApplyTo

func (p *Policy) ApplyTo(engine *gin.Engine) error

ApplyTo configures a gin engine with the same trust list, so Context.ClientIP (rate limits, login throttling, audit logs) honours X-Forwarded-For and X-Real-IP only from these proxies.

func (*Policy) CIDRs

func (p *Policy) CIDRs() []string

CIDRs returns the trusted networks in CIDR form.

func (*Policy) ClientIP

func (p *Policy) ClientIP(r *http.Request) string

ClientIP returns the client address the way gin's Context.ClientIP does on an engine configured with ApplyTo: the peer address, unless the peer is trusted, in which case X-Forwarded-For is walked from the right past trusted proxies (then X-Real-IP).

func (*Policy) Resolve

func (p *Policy) Resolve(r *http.Request) Origin

Resolve returns r's origin. Forwarding headers are read only when the TCP peer is trusted; the last value of a header (the one the nearest proxy wrote) is used, and a value that is not a plain http/https scheme or a valid host[:port] is ignored.

func (*Policy) TrustedIP

func (p *Policy) TrustedIP(ip net.IP) bool

TrustedIP reports whether ip is in a trusted network.

func (*Policy) TrustedPeer

func (p *Policy) TrustedPeer(r *http.Request) bool

TrustedPeer reports whether the request's TCP peer is a trusted proxy.

Jump to

Keyboard shortcuts

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