Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultHeaders = http.Header{
"Referrer-Policy": {"strict-origin-when-cross-origin"},
"Content-Security-Policy": {"default-src 'self'; frame-ancestors 'none'"},
"X-Content-Type-Options": {"nosniff"},
"X-Frame-Options": {"DENY"},
"X-Xss-Protection": {"0"},
"Permissions-Policy": {"camera=(), microphone=(), geolocation=(), payment=()"},
"Strict-Transport-Security": {"max-age=31536000; includeSubDomains"},
}
DefaultHeaders contains the default security header values used by SetHeaders.
These are not protected by a mutex, so modifying the map while serving requests is racy.
Functions ¶
func BuildContentSecurityPolicy ¶
BuildContentSecurityPolicy returns a CSP header value based on resource URLs.
Resource URLs contribute external source expressions to script, style, image, font and connect directives according to their type.
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, DefaultHeaders is 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.
http.Header // The headers to set. If nil, uses DefaultHeaders
// 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)