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 with securityhttp.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:
Or standalone, without securityhttp:

HSTS

HTTP Strict Transport Security is off by default (HSTS over plain HTTP is meaningless). Enable in production:
Only enable HSTS when:
  • 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: true and a minimum max-age of 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:
Matching requests get 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: