secure

Standard net/http middleware that adds HSTS, CSP, X-Frame-Options, and other security headers to any Go web app in a few lines.

Library
Go
vv1.17.0
2,357 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
48 /100 Fair
Development Activity 8
Maintenance 20
Community 64
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
61 /100 Good
Architecture 75
Code Quality 85
Innovation 40
Learning Curve 45

Secure is a lightweight HTTP middleware for Go’s standard net/http package that helps developers apply common security headers to their web applications with minimal configuration. It implements the http.Handler interface directly, so it can be dropped into net/http, chi, Negroni, Gin, or virtually any router/framework that accepts http.Handler middleware. Rather than requiring hand-written boilerplate for each header, Secure exposes a single Options struct covering HSTS, X-Frame-Options, Content-Type sniffing protection, XSS filtering, CSP, referrer policy, and several newer cross-origin isolation headers.

Beyond header injection, Secure handles allowed-host validation (with optional regex matching), forced HTTPS redirects with configurable proxy header support, and per-request CSP nonce generation via a dedicated cspbuilder helper package. Because it ships as a single dependency-free Go module with a stable v1 API since 2014, teams add it once and rarely touch it again, making it a common first line of defense in production Go services behind reverse proxies like Nginx or Traefik.

What You Get

  • Strict-Transport-Security, X-Frame-Options, X-Content-Type-Options, X-XSS-Protection, and CSP headers configurable through one Options struct
  • Automatic HTTP-to-HTTPS redirection with proxy-header awareness for apps behind load balancers
  • Allowed-host validation with optional regex matching to block Host-header spoofing
  • A dedicated cspbuilder sub-package for composing Content-Security-Policy directives programmatically
  • Per-request CSP nonce generation retrievable from the request context

Common Use Cases

  • Adding baseline OWASP security headers to a new Go API or web service without hand-rolling each one
  • Enforcing HTTPS and blocking Host-header attacks for services sitting behind Nginx, Traefik, or another reverse proxy
  • Building and rotating per-request CSP nonces for inline scripts in server-rendered Go applications
  • Retrofitting security headers onto an existing net/http, chi, or Negroni application with a single middleware wrap

Under The Hood

Architecture Secure is a single-file middleware (secure.go) implementing http.Handler through a functional wrapper pattern — Handler, HandlerForRequestOnly, HandlerFuncWithNext, and HandlerFuncWithNextForRequestOnly all delegate to a private processRequest method that returns computed headers, a possibly-modified request, and an error. Headers are either written immediately or stashed in the request context under a typed key (ctxSecureHeaderKey) for deferred application, letting the same core logic serve plain net/http, Negroni-style next-func middleware, and reverse-proxy response rewriting (ModifyResponseHeaders) without duplicating header logic. CSP nonce generation lives in its own file (csp.go) behind a typed context key, and CSP directive-string assembly is factored out entirely into the cspbuilder sub-package so consumers who want programmatic CSP construction don’t need to hand-format strings. There’s no external state beyond the Options struct and a couple of derived fields (compiled regex allowed-hosts, resolved context key), so extending the middleware with a new header means adding a field, a constant, and a branch inside processRequest.

Tech Stack go.mod declares go 1.13 with zero third-party dependencies (go.sum is empty) — the package uses only the standard library (net/http, regexp, context, crypto/rand, encoding/base64, strings, fmt). CI runs a golangci-lint job plus a test matrix spanning Go 1.21 through 1.26 on Ubuntu via a small Makefile (make ci). Distribution is purely as a Go module (import path github.com/unrolled/secure) alongside the cspbuilder sub-package for CSP directive composition.

Code Quality secure_test.go alone contains dozens of test functions covering nearly every Options permutation (allowed hosts, regex hosts, SSL redirect variants, proxy headers, STS combinations, CSP with and without nonce, AllowRequestFunc, custom bad-request/bad-host handlers), and the cspbuilder sub-package carries its own extensive test suite — an unusually high test-to-code ratio for a codebase this size. Error handling is explicit: processRequest returns a plain error on every rejection path (bad host, SSL redirect, disallowed request) rather than panicking or swallowing, with the single panic() in New() reserved for a genuinely unrecoverable case (an invalid regex supplied by the caller at startup). golangci-lint runs in CI on every push and pull request alongside the Go-version test matrix, and //nolint suppressions are used sparingly with inline justification.

What Makes It Unique Secure doesn’t invent new headers or protocols — every header it sets (HSTS, CSP, X-Frame-Options, COOP/COEP/CORP, Permissions-Policy) is a standard browser security header. What’s distinctive is the deferred-header pattern that lets identical core logic serve plain net/http, Negroni-style next() middleware, and proxied-response rewriting via ModifyResponseHeaders for httputil.ReverseProxy — a clever but narrow generalization rather than a novel technique overall.

Used by 6 apps in this directory

Go
75%
AGPL 3.0

Coder

Code Editors · Developer Tools · Devops

16,920

Self-hosted cloud development environments and AI coding agents — defined in Terraform, connected via WireGuard, automatically shut down when idle.

View details
91
Repo Health
90
Technical
65
Dependency
Built with
Go 75%
TypeScript 23%
Updated today
Go
62%
Apache 2.0

Harness Open Source

Code Editors · Developer Tools · Devops

38,509

A unified open source DevOps platform combining Git hosting, CI/CD pipelines, cloud development environments, and artifact registries in a single self-hosted system.

View details
90
Repo Health
79
Technical
64
Dependency
Built with
Go 62%
TypeScript 33%
Updated yesterday
Go
82%
GPL 3.0

Navidrome

File Storage · Music Audio

24,068

Run your own personal Spotify — stream your entire music collection from any device, anywhere, forever.

View details
91
Repo Health
81
Technical
69
Dependency
Built with
Go 82%
JavaScript 15%
Updated yesterday
Go
51%
Apache 2.0

opencloud

File Storage

6,120

Open source file management and collaboration platform that keeps your data under your control, no database required.

View details
86
Repo Health
80
Technical
68
Dependency
Built with
Go 51%
Gherkin 34%
PHP 11%
Updated yesterday
Go
90%
BSD 3

tau

Devops

5,176

Open-source, Git-native platform-as-a-service for building, deploying, and scaling fullstack apps on your own infrastructure with no DevOps required.

View details
79
Repo Health
82
Technical
64
Dependency
Built with
Go 90%
Updated 1 months ago
Go
94%
MIT

Traefik

Automation · Devops · Security

65,130

A cloud-native reverse proxy and load balancer that auto-configures itself from Docker, Kubernetes, and other orchestrators — zero manual routing required.

View details
92
Repo Health
85
Technical
64
Dependency
Built with
Go 94%
Updated 2 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers