SecurityHeaders writes a conservative set of default response security
headers on every request; HSTS and no-store caching are opt-in via options.
Import
Default headers
Every chain built withsecurityhttp.New() includes middleware.SecurityHeaders()
with these defaults:
HSTS and Cache-Control are off by default (see below).
Customizing headers
SecurityHeaders takes HeaderOptions. Pass an empty string to disable a
default:
HSTS
HTTP Strict Transport Security is off by default (HSTS over plain HTTP is meaningless). Enable in production:- Your service is served exclusively over HTTPS in production
- You’re prepared for the entire domain (and subdomains) to require HTTPS for
the duration of
max-age - You want to submit to the HSTS preload list (requires
preload: trueand a minimummax-ageof 1 year)
No-store Cache-Control for sensitive responses
SecurityHeaders does not set Cache-Control by default. Turn it on for a
subset of routes with a matcher, or for every response:
Cache-Control: no-store, Pragma: no-cache, and
Expires: 0.
Content Security Policy and other custom headers
There is no built-in CSP or Permissions-Policy option —HeaderOption only
covers the fixed set above. Add any other header with a small middleware of
your own, composed alongside SecurityHeaders in the chain:
