spatie/laravel-csp
Set Content Security Policy (CSP) headers in Laravel to control which scripts, styles, and resources can load and where they can send data. Helps prevent XSS and malicious third-party scripts. Includes report-only mode, nonces, and easy config.
Basic, Google, Stripe) enables modular security policies, reducing coupling between CSP logic and business logic. Custom presets can be added without modifying core functionality.RandomString) and configurable nonce_enabled setting provide flexibility for dynamic content (e.g., inline scripts/styles) while mitigating CSP risks.AddCspHeaders) and config publishing, requiring minimal boilerplate. Works with Laravel 10+ (based on last release date).@cspMetaTag, useful for hybrid server-side/SPA apps (e.g., Inertia.js).report_uri/report_to config, enabling violation monitoring.script-src) could break functionality. Mitigation: Use report_only_presets for testing and validate with browser dev tools.report_uri) may introduce latency if misconfigured or overloaded.RandomString generator sufficient, or does the app need a custom solution (e.g., UUID-based)?report_uri/report_to integrate with them?enabled_while_hot_reloading needed for development? If so, how will it interact with tools like Vite/Laravel Mix?unsafe-inline)?laravel-permission).@cspMetaTag).php artisan vendor:publish --tag=csp-config.bootstrap/app.php or route files.presets and directives in config/csp.php.report_only_presets for initial testing (no enforcement).CSP_ENABLED=true).report_uri (e.g., Report URI dashboard).composer.json constraints).Cache-Control headers.config/csp.php.report_only_presets first.MyCustomPreset::class).<script nonce="{{ Spatie\Csp\Csp::scriptNonce() }}">...</script>
report_uri and validate violations are logged.CSP_ENABLED) for toggling.composer update spatie/laravel-csp and test thoroughly.report_uri) may scale horizontally if using a service like Report URI.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Misconfigured CSP directives | Broken frontend (e.g., blocked scripts) | Use report_only_presets in testing; validate with browser tools. |
| Nonce generation failure | Inline scripts/styles blocked | Fallback to unsafe-inline temporarily; fix nonce injection in templates. |
| Reporting endpoint downtime | Lost violation data | Use a backup report_uri or local logging. |
| Middleware conflict | CSP headers not applied | Test middleware order; ensure AddCspHeaders runs after auth/CORS. |
| Third-party service changes | CSP violations from new domains | Update presets or directives; monitor report_uri. |
config/csp.php and preset usage.curl -I https://app.example.com).CSP_ENABLED=false if violations disrupt core functionality.report_only_presets to debug issues before enforcing policies.How can I help you explore Laravel packages today?