Repository navigation
feat(app): add configurable security headers middleware - #140
Open
JoelAlphonso wants to merge 2 commits into
Open
JoelAlphonso wants to merge 2 commits into
JoelAlphonso wants to merge 2 commits into
Conversation
Add a SecurityHeadersMiddleware that applies configurable HTTP security headers (CSP, HSTS, framing, referrer, permissions, COOP/CORP, etc.) to responses. It supports per-path profiles, excluded paths, native header output for legacy exit-based responses, and a path-scoped Clear-Site-Data option. Register the middleware as 'middlewares/charcoal/app/middleware/security-headers' so applications can enable it through their middlewares configuration.
mcaskill
approved these changes
Nov 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a built-in, configurable HTTP security headers middleware to
charcoal-app.Applications can enable it from their
middlewaresconfiguration:{ "middlewares": { "charcoal/app/middleware/security-headers": { "active": true, "headers": { "Permissions-Policy": "camera=(), microphone=(), geolocation=()", "Cross-Origin-Opener-Policy": "same-origin", "Cross-Origin-Resource-Policy": "same-origin" }, "hsts": { "enabled": true, "max_age": 15552000, "include_subdomains": false, "preload": false }, "csp": { "enabled": "report-only", "directives": { "default-src": [ "'self'" ], "base-uri": [ "'self'" ], "object-src": [ "'none'" ], "frame-ancestors": [ "'self'" ], "script-src": [ "'self'", "https://www.googletagmanager.com" ] } } } } }Features
X-Content-Type-Options,X-Frame-Options,Referrer-Policy,X-Permitted-Cross-Domain-Policies.headersmap (null/falsedisables one).max_age,includeSubDomains,preload, and secure-request detection (includingX-Forwarded-Proto).report_uri.profiles(first matching regex wins) for admin/API exceptions.excluded_pathto skip specific paths entirely.Clear-Site-Datafor logout endpoints (not every response).use_native_headers(defaulttrue) sends the headers through PHP's native header list before the stack runs, so they still apply to legacyheader()+exitpaths. Set tofalsefor pure PSR-7 behaviour.Tests
Tests cover defaults, header application, HSTS over HTTP/proxy, CSP modes, exclusions, profiles,
Clear-Site-Datascoping, and disabling headers.Related
Documentation
Added a Security Headers section to
packages/app/docs/components.mdcovering the full configuration, defaults, options, profiles, and CSP rollout guidance.