Documentation
¶
Overview ¶
Package clientaddr is the ONE reading of "which address is this request from". Every per-address limit, every address forwarded to GoTrue (X-Muster-Client-IP), every audit row and log line that names a client takes it from here (Of), in muster-server and in muster-preview alike.
🔴 THE DEFAULT TRUSTS NOTHING. The answer is the TCP peer of the connection, whatever the request's headers say, on every listener. A forwarding header (CF-Connecting-IP, X-Forwarded-For, X-Real-IP) is something any client can type, and a plain reverse proxy passes most of them through unchanged, so a header is believed ONLY when the TCP peer is inside a configured trusted range (MUSTER_TRUSTED_PROXIES) — and then only the one header the operator named (MUSTER_CLIENT_IP_HEADER).
🔴 X-Forwarded-For IS READ FROM THE RIGHT. Each proxy APPENDS the address it received the request from, so the right-most entries are the ones written by proxies, and the left-most is whatever the client sent. The client is the first entry, walking from the right, that is NOT a trusted proxy. The left-most entry is used only when every entry is trusted.
⚠ THAT IS SAFE ONLY BEHIND A PROXY THAT APPENDS. A proxy that forwards the client's X-Forwarded-For untouched (nginx with a bare proxy_pass) leaves a client-typed value right-most, and no reading of the header can undo that: such a proxy must name the client in a header it OVERWRITES (X-Real-IP) or append it ($proxy_add_x_forwarded_for). docs/configuration.md has the recipes.
🔴 RESTRICTED, THE HEADER IS BELIEVED ON THE PUBLIC LISTENERS AND NOWHERE ELSE. A deployment that fronts its public listener with a proxy must trust the proxy's address range, and in a cluster that can be a whole pod CIDR: every other pod in it could then reach the machine port and choose its own address in every per-address limit and audit row. OnlyOnPublicListeners makes the header count only on a request a listener marked with PublicListener (muster-server's browser and public pages listeners; it applies the restriction whenever MUSTER_BROWSER_PORT is set); every other listener answers the TCP peer, whatever the peer.
⚠ IT DOES NOT MAKE A WIDE RANGE SAFE ON THE PUBLIC LISTENERS THEMSELVES: any trusted peer that can reach one still names its own address there. A range wider than the proxy is safe only when nothing else can reach them (a NetworkPolicy); docs/configuration.md says so.
The package is a leaf: the standard library only.
Index ¶
- Constants
- func Of(r *http.Request) string
- func PublicListener(h http.Handler) http.Handler
- func RateKey(ip string) string
- type Config
- type Policy
- func (p *Policy) Describe() string
- func (p *Policy) Header() string
- func (p *Policy) Middleware(next http.Handler) http.Handler
- func (p *Policy) OnlyOnPublicListeners() *Policy
- func (p *Policy) Resolve(r *http.Request) string
- func (p *Policy) Trusted() []string
- func (p *Policy) WithLog(logf func(format string, args ...any)) *Policy
Constants ¶
const ( HeaderCFConnectingIP = "CF-Connecting-IP" HeaderXForwardedFor = "X-Forwarded-For" HeaderXRealIP = "X-Real-IP" )
The headers MUSTER_CLIENT_IP_HEADER may name, in their canonical spelling.
const Unknown = "unknown"
Unknown is the address of a request whose TCP peer does not parse (never a real network connection; a test's hand-made RemoteAddr).
Variables ¶
This section is empty.
Functions ¶
func Of ¶
Of is r's client address: the one Middleware resolved, or — for a request that did not pass through it — the TCP peer (the strict default; never a header).
func PublicListener ¶ added in v0.311.0
PublicListener marks every request h serves as having arrived on a listener whose forwarding header may be believed (OnlyOnPublicListeners). It must wrap OUTSIDE Middleware, so the mark is there when the address is resolved.
func RateKey ¶ added in v0.307.0
RateKey is the key a per-address LIMIT counts ip under: an IPv4 address as it is, an IPv6 one as its /64. One host is routinely given a whole /64, so a limit keyed on full IPv6 addresses lets one machine pose as 2^64 clients. Anything that does not parse as an address (a non-address rate key such as a user's) is returned as it is. Audit rows and logs keep the full address.
Types ¶
type Config ¶
type Config struct {
// TrustedProxies is the comma-separated CIDR list (a bare address is a
// single host). Empty trusts nothing.
TrustedProxies string
// Header is the header a trusted peer names the client in: one of the
// Header* constants, case-insensitive. Empty with TrustedProxies empty.
Header string
TrustedVar, HeaderVar string
}
Config is what New reads. The two *Var names are only for messages, so the same code serves muster-server (MUSTER_*) and muster-preview (PREVIEW_*).
type Policy ¶
type Policy struct {
// contains filtered or unexported fields
}
Policy decides a request's client address. A nil *Policy is the strict default: the TCP peer, always.
func New ¶
New parses c. Both settings or neither: a header with no trusted range would never be read, and a trusted range with no header would believe nothing, and either is a configuration that silently does not do what it says.
func (*Policy) Middleware ¶
Middleware resolves each request's client address once, with p, and carries it on the request for Of.
func (*Policy) OnlyOnPublicListeners ¶ added in v0.311.0
OnlyOnPublicListeners returns a copy of p that believes its header only on a request PublicListener marked; every other request resolves to its TCP peer. A nil p stays nil (the strict default believes no header anywhere).
func (*Policy) Resolve ¶
Resolve is the client address of r: the TCP peer, unless the peer is a trusted proxy, in which case the configured header's client (validated; the peer when the header is absent or does not parse). Always a canonical IP string, or Unknown.