How to configure Laravel Pulse to work with CSP (Content-Security-Policy)? #55982
Replies: 1 comment
-
I just ran into this, and found helpful insight in a comment by @ziadoz. (Cheers to @valorin too for his comment) I think it's a fair solution to have separate CSPs, and I have had no issues with Livewire. My application uses a nonce-based CSP, but Pulse requires <?php
declare(strict_types=1);
namespace App\Http\CSP;
use Spatie\Csp\{
Directive,
Keyword,
Policy,
Preset
};
class PulseContentSecurityPolicy implements Preset
{
public function configure(Policy $policy): void
{
$policy
->add(Directive::BASE, Keyword::SELF)
->add(Directive::CONNECT, Keyword::SELF)
->add(Directive::DEFAULT, Keyword::NONE)
->add(Directive::FONT, [Keyword::SELF, 'data:', 'https://fonts.bunny.net/figtree/files/'])
->add(Directive::FORM_ACTION, Keyword::SELF)
->add(Directive::FRAME, Keyword::SELF)
->add(Directive::IMG, [Keyword::SELF, 'data: https://gravatar.com/avatar/ https://unavatar.io/'])
->add(Directive::MEDIA, Keyword::SELF)
->add(Directive::OBJECT, Keyword::NONE)
->add(Directive::SCRIPT, [Keyword::SELF, Keyword::UNSAFE_INLINE, Keyword::UNSAFE_EVAL])
->add(Directive::STYLE, [Keyword::SELF, Keyword::UNSAFE_INLINE, 'https://fonts.bunny.net/css'])
->add(Directive::MANIFEST, Keyword::SELF)
->add(Directive::FRAME_ANCESTORS, Keyword::NONE);
}
} Then in my <?php
use App\Http\CSP\PulseContentSecurityPolicy;
use Laravel\Pulse\Http\Middleware\Authorize;
use Laravel\Pulse\Pulse;
use Laravel\Pulse\Recorders;
use Spatie\Csp\AddCspHeaders;
return [
// ...
'middleware' => [
'pulse-web',
],
// ...
]; Within that group, I basically had to re-build the "web" group but without my nonce and other app-specific middleware, which for Laravel 11+ style configuration, in use App\Http\CSP\PulseContentSecurityPolicy;
use Illuminate\Cookie\Middleware\{
EncryptCookies,
AddQueuedCookiesToResponse
};
use Illuminate\Session\Middleware\StartSession,
use Illuminate\View\Middleware\ShareErrorsFromSession,
use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken,
use Illuminate\Routing\Middleware\SubstituteBindings,
use Laravel\Pulse\Http\Middleware\Authorize;
use Spatie\Csp\AddCspHeaders;
// ...
->withMiddleware(function (Middleware $middleware): void {
$middleware->group('pulse-web', [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
ShareErrorsFromSession::class,
ValidateCsrfToken::class,
SubstituteBindings::class,
'auth',
Authorize::class,
AddCspHeaders::class . ':' . PulseContentSecurityPolicy::class,
]);
})
// ... For Laravel <10.x, in use App\Http\CSP\PulseContentSecurityPolicy;
use Illuminate\Cookie\Middleware\{
EncryptCookies,
AddQueuedCookiesToResponse
};
use Illuminate\Session\Middleware\StartSession,
use Illuminate\View\Middleware\ShareErrorsFromSession,
use Illuminate\Foundation\Http\Middleware\ValidateCsrfToken,
use Illuminate\Routing\Middleware\SubstituteBindings,
use Laravel\Pulse\Http\Middleware\Authorize;
use Spatie\Csp\AddCspHeaders;
protected $middlewareGroups = [
'pulse-web' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
ShareErrorsFromSession::class,
ValidateCsrfToken::class,
SubstituteBindings::class,
'auth',
\Laravel\Pulse\Http\Middleware\Authorize::class,
\Spatie\Csp\AddCspHeaders::class . ':' . PulseContentSecurityPolicy::class,
],
]; Hopefully that helps someone else! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using Laravel Pulse to monitor active users and requests in production.
However, since I implemented a Content-Security-Policy (CSP) header with a nonce, Pulse's inline scripts are blocked. Livewire (used by Pulse) injects inline scripts without a way to customize nonce values.
Example error:
I've considered:
'unsafe-inline'
(not secure)/pulse
)My questions:
Any help or recommendation is appreciated.
Beta Was this translation helpful? Give feedback.
All reactions