Documentation
¶
Overview ¶
Package httpsec holds the HTTP-level security pieces that depend on how Mediarium is reached: which reverse proxies are trusted to say who the caller is (client IP, HTTPS or not, public host name), the cross-site request check, and the security response headers.
The rule behind all of it: a header such as X-Forwarded-For is only believed when the TCP connection it arrived on comes from a proxy the owner trusts (TRUSTED_PROXIES). From anyone else it is ignored, so a direct visitor cannot pretend to be another address, or to be on HTTPS.
Index ¶
- func IsHomeAddress(ip string) bool
- func IsHomeHost(hostport string) bool
- func LimitKey(ip string) string
- type Proxies
- func (p *Proxies) CSRF(extraHosts []string, next http.Handler) http.Handler
- func (p *Proxies) ClientIP(r *http.Request) string
- func (p *Proxies) FromHome(r *http.Request) bool
- func (p *Proxies) FromTrustedProxy(r *http.Request) bool
- func (p *Proxies) Headers(next http.Handler) http.Handler
- func (p *Proxies) IsHTTPS(r *http.Request) bool
- func (p *Proxies) PublicHosts(r *http.Request) []string
- func (p *Proxies) Trusts(addr netip.Addr) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsHomeAddress ¶
IsHomeAddress reports whether ip (as text) is on the home network: loopback, private, link-local, carrier-grade NAT or unique-local. Anything that is not an address returns false.
func IsHomeHost ¶
IsHomeHost reports whether the host a visitor typed (the Host header, with or without a port) is an address or a name that belongs to a home network: an IP address, "localhost", a name without a dot (a NAS called "nas") or one ending in .local, .lan, .home, .home.arpa or .internal. A public host name such as media.example.com is not, even when it points at a private address on the home network: a web page on another site can make a visitor's browser send requests to a private address under a name of its own ("DNS rebinding"), and such a request must not pass for someone at home.
Types ¶
type Proxies ¶
type Proxies struct {
// contains filtered or unexported fields
}
Proxies is the set of reverse proxies whose forwarding headers are believed. The zero value trusts nobody.
func ParseProxies ¶
ParseProxies reads a TRUSTED_PROXIES value: a comma-separated list of addresses, CIDR ranges and the keywords "private" (loopback and private ranges, the default) and "none" (trust no proxy: forwarding headers are ignored). An empty value means "private".
func (*Proxies) CSRF ¶
CSRF refuses requests that change something when they were started by another web site. The session cookie is SameSite=Strict, which already keeps browsers from sending it cross-site; this is the second layer for browsers that ignore that and for same-site (sibling subdomain) pages.
A request is let through when:
- it only reads (GET, HEAD, OPTIONS), or
- it carries an X-API-Key: a page in a browser cannot add that header to a cross-site request without a preflight, and the key is not a cookie, or
- the browser says it is same-origin (Sec-Fetch-Site: same-origin or none): a web page cannot forge that header, so it holds even when a proxy rewrites the Host header, or
- its Origin (or, without one, Referer) names this server: the host the caller used (Host, or X-Forwarded-Host from a trusted proxy) or one of extraHosts, or
- it has neither Origin, Referer nor Sec-Fetch-Site, which is what scripts and tools such as curl send (a browser always sends at least one of them on a request that changes something).
extraHosts are additional accepted host names (ALLOWED_ORIGINS), for a proxy that rewrites the Host header and a browser too old to send Sec-Fetch-Site.
func (*Proxies) ClientIP ¶
ClientIP is the address of the real caller. Without a trusted proxy in front it is the TCP peer, whatever headers say. Behind one it is taken from X-Forwarded-For by walking the list from the right (the end each proxy appends to) and stopping at the first address that is not itself a trusted proxy: entries further left were supplied by the caller and are never believed. X-Real-IP is used only when there is no X-Forwarded-For. The result is "" only if the peer address is unreadable.
func (*Proxies) FromHome ¶
FromHome reports whether r plainly comes from someone on the home network: the caller's address (see ClientIP) is a home address and the request was made to a home address or name (IsHomeHost).
It is deliberately strict where it cannot tell. A request that a trusted proxy passed on without saying who the caller is (forwarding headers, but no readable X-Forwarded-For or X-Real-IP) looks like the proxy's own address, which is a home address, yet the visitor behind it could be anyone. That is not "from home". The first-run setup uses this to decide who needs the setup code.
func (*Proxies) FromTrustedProxy ¶
FromTrustedProxy reports whether the connection r arrived on comes from a trusted proxy, i.e. whether its X-Forwarded-* headers may be believed.
func (*Proxies) Headers ¶
Headers sets the security response headers on every response. Strict-Transport-Security is added only when the caller is on HTTPS (directly or as reported by a trusted proxy): sent over plain HTTP it would be ignored anyway, and a LAN visit over http:// must keep working.
func (*Proxies) IsHTTPS ¶
IsHTTPS reports whether the caller reached Mediarium over HTTPS: this connection is TLS, or a trusted proxy says the caller's was (X-Forwarded-Proto, or the proto of the Forwarded header).
func (*Proxies) PublicHosts ¶
PublicHosts lists the host names (with port when not a default one) the caller may have used to reach Mediarium: the Host header of the request, and the X-Forwarded-Host a trusted proxy adds. Lower case.