Documentation
¶
Overview ¶
Package httpsec holds the Host/Origin validation, bearer-token and security-header helpers shared by LeanProxy's local HTTP servers: the admin endpoints (pkg/dashboard, pkg/metrics, issue #316) and the Streamable HTTP MCP front end (pkg/streamhttp, issue #309). The packages stay independent of each other; this package only avoids duplicating the DNS-rebinding and authentication defenses between them.
Index ¶
- func AllowedHosts(bindHost, port string, extra []string) map[string]struct{}
- func BearerToken(r *http.Request) (string, bool)
- func IsLoopbackBindAddr(bind string) bool
- func IsLoopbackHost(host string) bool
- func OriginAllowed(origin, host string, allowed map[string]struct{}) bool
- func OriginSet(origins []string) map[string]struct{}
- func SecurityHeaders() func(http.Handler) http.Handler
- func ValidBearer(r *http.Request, token string) bool
- func ValidateHost(allowed map[string]struct{}) func(http.Handler) http.Handler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllowedHosts ¶
AllowedHosts builds the canonical set of acceptable HTTP Host header values (lower-cased "host:port") for a server bound to bindHost:port. It always includes the bind host itself (when it names a specific interface rather than "0.0.0.0"/"::"/""), "localhost", "127.0.0.1" and "::1", each combined with port, plus any operator-supplied extra hosts: an extra entry that already has a port is taken literally, otherwise port is appended.
func BearerToken ¶
BearerToken returns the token of an "Authorization: Bearer <token>" header (scheme matched case-insensitively), and whether there was one.
func IsLoopbackBindAddr ¶
IsLoopbackBindAddr reports whether bind ("host:port") only binds a loopback interface. An empty host (e.g. ":9090") binds every interface and is therefore not loopback. An unparsable bind address is treated as non-loopback so callers fail closed.
func IsLoopbackHost ¶
IsLoopbackHost reports whether host (a bare hostname or IP, no port) is a loopback name or address: "localhost" (case-insensitive) or any IP for which net.IP.IsLoopback is true (127.0.0.0/8, ::1).
func OriginAllowed ¶
OriginAllowed reports whether a request carrying the Origin header value origin may reach a server whose (already validated) Host header is host: the origin is either the server's own ("http://<host>", i.e. a page it served itself) or allowlisted. The opaque origin "null" (sandboxed frames, file:// pages) is never allowed. Callers only consult it when an Origin header is present: non-browser clients do not send one.
func OriginSet ¶
OriginSet builds the canonical set of allowlisted browser origins ("scheme://host[:port]", lower-cased, without a trailing slash) from operator-supplied values. Empty entries are skipped.
func SecurityHeaders ¶
SecurityHeaders returns middleware that sets the response headers required by issue #316 for the dashboard: a same-origin-only CSP, clickjacking and MIME-sniffing protection, and no referrer leakage.
func ValidBearer ¶
ValidBearer reports whether r carries "Authorization: Bearer <token>" with exactly token, compared in constant time. An empty token never matches.
func ValidateHost ¶
ValidateHost returns middleware that rejects (403 Forbidden) any request whose Host header is not in allowed, and any state-changing request (anything but GET/HEAD/OPTIONS) whose Origin header, when present, does not match the request's own Host. This is the DNS-rebinding and cross-origin defense required by issue #316: a malicious page served from an unrelated origin cannot drive the dashboard or metrics endpoint even though it runs in the victim's browser and can reach 127.0.0.1.
Types ¶
This section is empty.