httpsec

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllowedHosts

func AllowedHosts(bindHost, port string, extra []string) map[string]struct{}

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

func BearerToken(r *http.Request) (string, bool)

BearerToken returns the token of an "Authorization: Bearer <token>" header (scheme matched case-insensitively), and whether there was one.

func IsLoopbackBindAddr

func IsLoopbackBindAddr(bind string) bool

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

func IsLoopbackHost(host string) bool

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

func OriginAllowed(origin, host string, allowed map[string]struct{}) bool

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

func OriginSet(origins []string) map[string]struct{}

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

func SecurityHeaders() func(http.Handler) http.Handler

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

func ValidBearer(r *http.Request, token string) bool

ValidBearer reports whether r carries "Authorization: Bearer <token>" with exactly token, compared in constant time. An empty token never matches.

func ValidateHost

func ValidateHost(allowed map[string]struct{}) func(http.Handler) http.Handler

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.

Jump to

Keyboard shortcuts

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