Security Headers

SecurityHeadersMiddleware adds browser policies to responses after the handler runs. Apply it where the same policy should cover a route or a group of routes. It preserves headers already set by the response.

Attach the Middleware

Attach it to one route. Here $showProfile is your existing handler.

use Atom\Router\Route;
use Atom\Security\SecurityHeadersMiddleware;

Route::get("/profile", $showProfile)
    ->middleware(SecurityHeadersMiddleware::class);

Attach it to a group to share the policy across its routes.

Route::group("/account", static function () use ($showProfile, $showSettings): void {
    Route::get("/profile", $showProfile);
    Route::get("/settings", $showSettings);
})->middleware(SecurityHeadersMiddleware::class);

Defaults

With the middleware attached, Atom adds these headers by default:

Header Default What it controls
X-Content-Type-Options nosniff Tells browsers to use the declared content type.
X-Frame-Options SAMEORIGIN Allows framing only from the same origin.
Referrer-Policy strict-origin-when-cross-origin Limits the referrer details sent across origins.
Permissions-Policy camera=(), microphone=(), geolocation=() Disables these browser features for the document.

Content Security Policy (CSP) and HTTP Strict Transport Security (HSTS) are opt-in.

Configuration

Set SECURITY_HEADERS_ options in the application environment. For example, require that the page never be framed and limit referrer information to same-origin requests:

.env
SECURITY_HEADERS_FRAME_OPTIONS=DENY
SECURITY_HEADERS_REFERRER_POLICY=same-origin

Set SECURITY_HEADERS_NO_SNIFF=false to omit X-Content-Type-Options. An empty value disables a string option, such as SECURITY_HEADERS_PERMISSIONS_POLICY=.

Use SECURITY_HEADERS_CONTENT_SECURITY_POLICY to enforce a CSP. Use SECURITY_HEADERS_CONTENT_SECURITY_POLICY_REPORT_ONLY to try a policy without enforcing it. Choose a policy that matches the resources your pages load.

Set SECURITY_HEADERS_HSTS_MAX_AGE to a positive number of seconds after confirming the site always works over HTTPS. Atom sends HSTS only on requests it recognizes as HTTPS. If TLS terminates at a reverse proxy, register Trusted Proxies before this middleware. Check SECURITY_HEADERS_HSTS_INCLUDE_SUB_DOMAINS before enabling HSTS. It defaults to true, so set it to false if subdomains are outside your HTTPS policy.