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.
report_only_presets).unsafe-inline fallback).script-src to self + specific domains).Response::header())."This package lets us lock down our web app’s security posture with minimal dev effort. By enforcing Content Security Policy (CSP) headers, we’ll block malicious scripts from stealing user data (e.g., passwords, payment details) while keeping integrations like Stripe, Google Analytics, and Hotjar functional. It’s a low-cost, high-impact way to meet compliance needs (GDPR, PCI-DSS) and reduce support tickets from security incidents. Implementation takes <1 day—we’ll start with core presets and expand as needed."
Key Metrics to Track:
*"Spatie’s
laravel-cspgives us a maintainable, scalable way to enforce CSP headers across the app. Here’s why it’s a no-brainer:
- Pre-built presets: No need to manually configure rules for Google, Stripe, or Hotjar—just enable the preset.
- Nonce support: Safely use inline scripts (e.g., for Vite/HMR) without
unsafe-inline.- Report-only mode: Test policies in production without breaking existing scripts.
- Middleware integration: Apply CSP globally or per-route (e.g., stricter rules for admin panels).
- MIT license + active maintenance: Trusted by 800+ repos; last updated June 2026.
Implementation Plan:
- Week 1: Add
Basic+Stripepresets; verify no UX breaks.- Week 2: Enable
report-onlyfor analytics tools; monitor violations via Report URI.- Ongoing: Add custom presets for new integrations (e.g., Intercom, Calendly).
Trade-offs:
- False positives: Some third-party scripts may need
unsafe-inlinetemporarily (track these).- Performance: Minimal overhead (~1ms per request for header generation).
Alternatives considered:
- Roll our own middleware: Higher maintenance risk.
- Nginx headers: Not flexible enough for per-route rules.
- Cloudflare CSP: Overkill if we’re not using their CDN."*
Dev-Focused Next Steps:
php artisan vendor:publish --tag=csp-config).AddCspHeaders to global middleware (or per-route for sensitive pages).report-only mode first (set enabled: false in config).How can I help you explore Laravel packages today?