Laravel Csp

spatie/laravel-csp image

Laravel Csp stats

Downloads
2.6M
Stars
639
Open Issues
0
Forks
57

View on GitHub →

Add CSP headers to the responses of a Laravel app

Laravel CSP: Secure Your App with Content Security Policies

The spatie/laravel-csp package is a powerful tool for managing Content Security Policy (CSP) headers in Laravel applications, enhancing the security by controlling the sources from which your application can load resources.

Key Features

  • Flexible Policy Definitions: Define and manage CSP directives easily using policy classes.
  • Dynamic Nonce Support: Automatically handles nonces for inline scripts and styles, ensuring they align with CSP requirements.
  • Configurable Response Headers: Choose between standard CSP headers or meta tags within HTML documents.
  • Development and Production Modes: Supports different policies for development and production, with options to report only mode for testing.
  • Integration with Laravel Vite: Seamlessly integrates with Laravel Vite for nonce generation during asset compilation.

Installation

Install the package via composer:

composer require spatie/laravel-csp

Publish the configuration file:

php artisan vendor:publish --tag=csp-config

Register the middleware in your HTTP kernel or on specific routes to apply CSP headers globally or conditionally.

Usage Overview

  1. Define CSP Policies: Extend the basic policy class to customize the sources your application should accept scripts, styles, images, etc., from.
  2. Apply Middleware: Attach the Spatie\Csp\AddCspHeaders middleware to your web middleware group or specific routes.
  3. Test Policies: Set policies in report-only mode to monitor potential issues without impacting users.
  4. Handle Nonces: Utilize the built-in nonce generator or integrate with Laravel Vite for managing script and style tags securely.

Policy Customization

You can extend the basic policy class to specify your own directives:

namespace App\Support;
 
use Spatie\Csp\Directive;
use Spatie\Csp\Policies\Policy;
 
class MyCustomPolicy extends Policy
{
public function configure()
{
$this->addDirective(Directive::SCRIPT, 'www.google.com');
}
}

Set your custom policy in the csp.php config file to activate it across your application.

Inline Scripts and Styles

Use nonces generated by the package to include inline scripts and styles safely:

<!-- in a Blade view -->
<script nonce="{{ csp_nonce() }}">
// Your inline script here
</script>

Integration with Vite

Configure a custom nonce generator using Laravel Vite's CSP support for a streamlined workflow between development and production environments.

Reporting and Debugging

Configure a reporting URI to capture and analyze policy violations, helping you refine your CSP strategy.

Conclusion

spatie/laravel-csp is an essential package for developers looking to enforce security practices through content security policies, providing robust tools for configuration and management tailored to modern web applications.

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Laravel Csp Related Articles

How to Improve Your Laravel Application's Security Using a CSP image

How to Improve Your Laravel Application's Security Using a CSP

Read article
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media