Skip to content

feat(app): add configurable security headers middleware - #140

Open
JoelAlphonso wants to merge 2 commits into
mainfrom
feat/security-headers-middleware
Open

JoelAlphonso wants to merge 2 commits into
mainfrom
feat/security-headers-middleware

Conversation

@JoelAlphonso

@JoelAlphonso JoelAlphonso commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds a built-in, configurable HTTP security headers middleware to charcoal-app.

Applications can enable it from their middlewares configuration:

{
    "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

  • Safe defaults: X-Content-Type-Options, X-Frame-Options, Referrer-Policy, X-Permitted-Cross-Domain-Policies.
  • Custom headers through the headers map (null/false disables one).
  • HSTS with max_age, includeSubDomains, preload, and secure-request detection (including X-Forwarded-Proto).
  • CSP (enforced or report-only) with directive lists and optional report_uri.
  • Per-path profiles (first matching regex wins) for admin/API exceptions.
  • excluded_path to skip specific paths entirely.
  • Path-scoped Clear-Site-Data for logout endpoints (not every response).
  • use_native_headers (default true) sends the headers through PHP's native header list before the stack runs, so they still apply to legacy header() + exit paths. Set to false for pure PSR-7 behaviour.

Tests

./vendor/bin/phpunit --bootstrap tests/bootstrap.php packages/app/tests/Charcoal/App/Middleware/SecurityHeadersMiddlewareTest.php
./vendor/bin/phpcs --standard=phpcs.xml.dist packages/app/src/Charcoal/App/Middleware/SecurityHeadersMiddleware.php packages/app/src/Charcoal/App/ServiceProvider/AppServiceProvider.php packages/app/tests/Charcoal/App/Middleware/SecurityHeadersMiddlewareTest.php
./vendor/bin/phpstan analyse -c phpstan.neon.dist packages/app/src/Charcoal/App/Middleware/SecurityHeadersMiddleware.php

Tests cover defaults, header application, HSTS over HTTP/proxy, CSP modes, exclusions, profiles, Clear-Site-Data scoping, and disabling headers.

Related

Documentation

Added a Security Headers section to packages/app/docs/components.md covering the full configuration, defaults, options, profiles, and CSP rollout guidance.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants