securityhttp is designed so that adding
features cannot accidentally remove security, and misconfiguration causes a
build-time error rather than a runtime vulnerability.
What is always on
Every chain built withsecurityhttp.New() includes:
Security headers
Written on every response:
These are conservative, broadly-compatible defaults chosen to block MIME
sniffing, clickjacking, and referrer leakage without requiring any
configuration.
Session cookies
WhenSessionStore is configured, the session cookie is:
Secure— transmitted only over HTTPS (set tofalseforlocalhostdev only)HttpOnly— inaccessible to JavaScriptSameSite=Lax— blocks cross-site POST forgery while allowing top-level navigations
CSRF protection
On onceSessionStore is set. The default strategy is the synchronizer
token (session-backed). An X-CSRF-Token header is required for all unsafe
methods (POST, PUT, PATCH, DELETE).
The double-submit cookie strategy (csrf.NewDoubleSubmitRepository) is also
available when a session does not yet exist (e.g. the login endpoint itself):
Default-deny authorization
Build() errors if your route set has no AnyRequest() rule and
AllowUnmatched() was not called. This prevents accidental fail-open
configurations.
Opting out
You can only opt out of CSRF explicitly, and only for bearer-token APIs:Login() with CSRFDisabled() is a login-CSRF risk: an attacker
can silently log a victim into an attacker-controlled account. New() errors
on this pair unless you also call AllowLoginWithoutCSRF() — which is only for
deployments that enforce CSRF at another layer (e.g. an edge gateway).
Errors on misconfiguration
New() / NewHandler() never return a half-configured chain. They return
an error for:
WithLogin()without aWithSessionStore()WithLogin()+CSRFDisabled()withoutAllowLoginWithoutCSRF()WithAuthorize()with noAnyRequest()and noAllowUnmatched()- Invalid CORS configuration (credentials + wildcard origin)
Middleware ordering
The secure-correct fixed order is:HTTPS in production
SetSecure: true on your cookie config (the default) and enable HSTS:
