secureheaders

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package secureheaders provides an HTTP middleware that sets security-relevant response headers. Apply it once at the top of your middleware stack.

Quick start — opinionated secure defaults:

mux.Handle("/", secureheaders.Strict()(handler))

Custom configuration:

mux.Handle("/", secureheaders.New(secureheaders.Config{
    HSTS:            secureheaders.HSTSConfig{MaxAge: 365 * 24 * time.Hour, IncludeSubDomains: true},
    ContentTypeOpts: true,
    FrameOptions:    "DENY",
    ReferrerPolicy:  "strict-origin-when-cross-origin",
    CSP:             "default-src 'self'",
})(handler))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) func(http.Handler) http.Handler

New returns a middleware that applies the given security header configuration.

func Strict

func Strict() func(http.Handler) http.Handler

Strict returns a middleware with opinionated, production-grade security header defaults. Override individual fields via New if you need customisation.

Headers set by Strict:

  • Strict-Transport-Security: max-age=31536000; includeSubDomains
  • X-Content-Type-Options: nosniff
  • X-Frame-Options: DENY
  • X-XSS-Protection: 1; mode=block
  • Referrer-Policy: strict-origin-when-cross-origin
  • Permissions-Policy: geolocation=(), microphone=(), camera=()
  • Cross-Origin-Embedder-Policy: require-corp
  • Cross-Origin-Opener-Policy: same-origin
  • Cross-Origin-Resource-Policy: same-origin
  • Removes Server and X-Powered-By headers

Types

type Config

type Config struct {
	// HSTS configures the Strict-Transport-Security header.
	// Zero MaxAge disables the header (useful behind a TLS-terminating proxy).
	HSTS HSTSConfig

	// ContentTypeOpts emits "X-Content-Type-Options: nosniff" when true.
	ContentTypeOpts bool

	// FrameOptions controls the X-Frame-Options header.
	// Accepted values: "DENY", "SAMEORIGIN". Empty disables the header.
	FrameOptions string

	// XSSProtection emits "X-XSS-Protection: 1; mode=block" when true.
	// Note: modern browsers ignore this header in favour of CSP, but it is
	// harmless and helps with older browsers.
	XSSProtection bool

	// CSP is the full value for the Content-Security-Policy header.
	// Example: "default-src 'self'; img-src *; script-src 'self'"
	// Empty disables the header.
	CSP string

	// ReferrerPolicy sets the Referrer-Policy header.
	// Recommended: "strict-origin-when-cross-origin".
	// Empty disables the header.
	ReferrerPolicy string

	// PermissionsPolicy sets the Permissions-Policy header.
	// Example: "geolocation=(), microphone=(), camera=()"
	// Empty disables the header.
	PermissionsPolicy string

	// COEP sets the Cross-Origin-Embedder-Policy header.
	// Use "require-corp" to enable cross-origin isolation.
	COEP string

	// COOP sets the Cross-Origin-Opener-Policy header.
	// Use "same-origin" to prevent cross-origin window references.
	COOP string

	// CORP sets the Cross-Origin-Resource-Policy header.
	// Use "same-origin" or "same-site" to restrict resource sharing.
	CORP string

	// RemoveServerHeader strips the "Server" response header when true.
	RemoveServerHeader bool

	// RemovePoweredBy strips the "X-Powered-By" response header when true.
	RemovePoweredBy bool
}

Config controls which security headers are emitted.

type HSTSConfig

type HSTSConfig struct {
	// MaxAge is how long browsers should remember to use HTTPS (default: 1 year).
	// Set to 0 to disable HSTS.
	MaxAge time.Duration
	// IncludeSubDomains extends HSTS to all subdomains.
	IncludeSubDomains bool
	// Preload signals intent to be included in browser HSTS preload lists.
	// Only set this if you have submitted your domain at https://hstspreload.org.
	Preload bool
}

HSTSConfig configures the Strict-Transport-Security header.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL