Using Vite with laravel-csp #101
-
I tried installing laravel-csp on my laravel 9 project which uses vite, but i'm unable to add the nonce to the scripts and style. Is there a better approach? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
We are having the same problem after migrating an existing codebase to Laravel 9. Moving from Laravel to Vite has been throwing up a number of CSP related errors. I am interested in the solution to handle this. Thanks! |
Beta Was this translation helpful? Give feedback.
-
In readme I've added some notes on how to use this package with Vite. |
Beta Was this translation helpful? Give feedback.
-
I added this policy to use Vite in development: $this->addDirective(Directive::CONNECT, Scheme::WSS); |
Beta Was this translation helpful? Give feedback.
-
Hi, sorry to resurrect an old thread, but I didn't feel it warranted an entire new thread, as it is more on an extension of this discussion... which I had already used to fix the wss, as per the suggestion by @hofmannsven (thank you). My issue is that I now have everything working perfectly as we need... as in when using this in production, or locally with the assets build by Vite (vite build) it works fine. However, when trying to use hot module reloading functionality with Vite dev, we are plagued with the dreaded "Refused to apply inline style because it violates the following Content Security Policy directive: "style-src ..." error. This is due to the way Vite seems to want to inject resources for the HMR function. I have tried pretty much everything I can think of, but nothing works at all. For reference, heres the base policy in use: class BasePolicy extends Basic
{
public function configure()
{
parent::configure();
if (app()->environment() === 'local') {
$this
->addDirective(Directive::BASE, 'vite.newwebsite.test:*')
->addDirective(Directive::CONNECT, 'vite.newwebsite.test:*')
->addDirective(Directive::DEFAULT, 'vite.newwebsite.test:*')
->addDirective(Directive::FORM_ACTION, 'vite.newwebsite.test:*')
->addDirective(Directive::IMG, 'vite.newwebsite.test:*')
->addDirective(Directive::MEDIA, 'vite.newwebsite.test:*')
->addDirective(Directive::OBJECT, 'vite.newwebsite.test:*')
->addDirective(Directive::SCRIPT, 'vite.newwebsite.test:*')
->addDirective(Directive::STYLE, 'vite.newwebsite.test:*');
}
$this
->addDirective(Directive::CONNECT, Scheme::WSS)
->addDirective(Directive::IMG, Scheme::DATA)
->addDirective(Directive::SCRIPT, [
'fonts.googleapis.com',
'fonts.gstatic.com',
])
->addDirective(Directive::STYLE, [
'fonts.googleapis.com',
'fonts.gstatic.com',
])
->addDirective(Directive::FONT, [
'fonts.googleapis.com',
'fonts.gstatic.com',
])
->addNonceForDirective(Directive::SCRIPT)
->addNonceForDirective(Directive::STYLE);
if (app()->environment() === 'local') {
$this
->addDirective(Directive::SCRIPT, Keyword::UNSAFE_INLINE)
->addDirective(Directive::STYLE, Keyword::UNSAFE_INLINE);
}
}
/**
* @param Request $request
* @param Response $response
*
* @return bool
*/
public function shouldBeApplied(Request $request, Response $response): bool
{
if (config('app.debug') && ($response->isClientError() || $response->isServerError())) {
return false;
}
return parent::shouldBeApplied($request, $response);
}
} and the Nonce generator: class LaravelViteNonceGenerator implements NonceGenerator
{
public function generate(): string
{
return Vite::useCspNonce();
}
} I have also ensured that both are referenced within the csp.php config file, and have added While I know in theory we could simply deactivate it when working locally, I don't like to as it obviously removes our ability to detect if there are issues during the development process. I feel like this should be a simple issue, particularly around the inclusion of the nonce or not, but after trying every possible combo, I feel like I could use the help of the hive mind :) Very grateful of any help pointing out where I am being stupid, lol. |
Beta Was this translation helpful? Give feedback.
In readme I've added some notes on how to use this package with Vite.