httpsec

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0 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 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

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsHomeAddress

func IsHomeAddress(ip string) bool

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

func IsHomeHost(hostport string) bool

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.

func LimitKey

func LimitKey(ip string) string

LimitKey is what rate limits are counted against for ip: the address itself for IPv4, the whole /64 for IPv6 (one visitor owns a whole /64, so counting single addresses would let it rotate through billions).

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

func ParseProxies(spec string) (*Proxies, error)

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

func (p *Proxies) CSRF(extraHosts []string, next http.Handler) http.Handler

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

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

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

func (p *Proxies) FromHome(r *http.Request) bool

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

func (p *Proxies) FromTrustedProxy(r *http.Request) bool

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

func (p *Proxies) Headers(next http.Handler) http.Handler

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

func (p *Proxies) IsHTTPS(r *http.Request) bool

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

func (p *Proxies) PublicHosts(r *http.Request) []string

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.

func (*Proxies) Trusts

func (p *Proxies) Trusts(addr netip.Addr) bool

Trusts reports whether addr is one of the trusted proxies.

Jump to

Keyboard shortcuts

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