Technical article
Security headers for Laravel: what do you really need?
Use HSTS, CSP, nosniff, frame protection, referrer and permissions policies with a clear understanding of hosting and subdomains.
- Security & privacy
- Security
- headers
- Laravel
- really
- need

In this article
Choose security headers for specific browser risks and inspect the actual HTTP response behind the webserver and CDN. Laravel middleware can help, but does not automatically cover static files or errors produced before Laravel. A high scanner score is not a complete security assessment.
A small central starting point
$response->headers->set('X-Content-Type-Options', 'nosniff');
$response->headers->set('Referrer-Policy', 'strict-origin-when-cross-origin');
$response->headers->set('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
$response->headers->set('X-Frame-Options', 'DENY');Place this fragment in middleware after obtaining the response and before returning it. nosniff requires correct content types. The referrer policy limits referral information. The permissions policy disables named features; adapt it when the application genuinely needs them.
Make framing an explicit choice
DENY blocks framing and may break a deliberate embed. Decide whether to allow no framing, the same origin or a limited partner list. Align X-Frame-Options with CSP frame-ancestors. frame-ancestors belongs in an HTTP header, not solely a meta CSP.
Add HSTS after verifying HTTPS
HSTS tells browsers to use HTTPS on subsequent visits. Check all affected hosts first. includeSubDomains and preload widen the impact and are not defaults to copy blindly. An incorrect long-lived policy can block necessary access.
Illustrative response checks
- Request ordinary HTML, a 404 and a protected endpoint in a test environment.
- Compare application, webserver and CDN configuration for missing or conflicting headers.
- Check a deliberate embed, external navigation and any camera feature.
- Check script and stylesheet content types after enabling nosniff.
CSP and application security keep their own scope
CSP requires an inventory of scripts, styles, external services and necessary sources. Develop it using the separate CSP guide. Authorisation, CSRF, sessions and safe output remain application responsibilities; headers cannot fix an incorrect permission check.
Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.
Last updated:


