Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildContentSecurityPolicy ¶
BuildContentSecurityPolicy returns a CSP header value based on resource URLs.
The default policy includes style-src 'unsafe-inline'.
Resource URLs contribute external source expressions to script, style, image, font and connect directives according to their type.
The resource URLs are expected to come from trusted application configuration, not from arbitrary user input. This function classifies known resources; it is not a URL sanitizer.
func DefaultHeaders ¶
DefaultHeaders returns a copy of the default security headers used by SetHeaders.
func RequestIsSecure ¶
RequestIsSecure reports if a request should be considered HTTPS.
It always treats requests with non-nil TLS as secure.
If trustForwardedHeaders is true, it also honors the forwarding headers X-Forwarded-Ssl, Front-End-Https, X-Forwarded-Proto and Forwarded.
For list-valued forwarding headers, only the first hop is used.
func SetHeaders ¶
func SetHeaders(src http.Header, hw http.ResponseWriter, ishttps bool)
SetHeaders sets the response headers to the values in src. If src is nil, the default security headers are used.
If ishttps is false, Strict-Transport-Security is not set.
Types ¶
type Middleware ¶
type Middleware struct {
http.Handler // Handler receives the request after security headers are set.
Header http.Header // The headers to set. If nil, uses the default security headers.
// TrustForwardedHeaders enables forwarded-header HTTPS detection
// (X-Forwarded-Ssl, Front-End-Https, X-Forwarded-Proto and Forwarded).
// Enable only when these headers are set and sanitized by trusted
// infrastructure.
TrustForwardedHeaders bool
}
Middleware wraps an HTTP handler and sets secure default response headers before delegating to the wrapped handler.
The embedded Handler must be non-nil.
func (Middleware) ServeHTTP ¶
func (m Middleware) ServeHTTP(hw http.ResponseWriter, hr *http.Request)
ServeHTTP sets the security headers on the response and then delegates to the wrapped Handler. Strict-Transport-Security is included only when the request is considered secure (see RequestIsSecure and TrustForwardedHeaders).