-
-
Notifications
You must be signed in to change notification settings - Fork 55
Added csp option on default script tag #98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Conversation
config/cookieconsent.php
Outdated
/* | ||
|-------------------------------------------------------------------------- | ||
| CSP configuration | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Most cookie notices display a link to a dedicated page explaining | ||
| the extended cookies usage policy. If your application has such a page | ||
| you can add its route name here. | ||
| | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a specific description for the CSP configuration, including:
- What it relates to (you may add a link to Spatie's CSP package)
- How to configure it for Spatie's CSP package:
env('CSP_ENABLE', false) && env('CSP_NONCE_ENABLED', true)
config/cookieconsent.php
Outdated
| | ||
*/ | ||
|
||
'csp_enable' => env('CSP_ENABLE', false), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just set it to false
by default. People using spatie's package should refer to the updated configuration description (see previous comment).
src/CookiesManager.php
Outdated
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Cookie as CookieFacade; | ||
use Illuminate\Support\Str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused facade.
src/CookiesManager.php
Outdated
$csp_enable = config('cookieconsent.csp_enable', false); | ||
|
||
return '<script ' | ||
. 'src="' . route('cookieconsent.script') . '?id=' | ||
. md5(\filemtime(LCC_ROOT . '/dist/script.js')) . '" ' | ||
. ($csp_enable ? 'nonce="' . $this->generateCspNonce() . '" ' : '') | ||
. 'defer' | ||
. '></script>'; | ||
} | ||
|
||
protected function generateCspNonce(): string | ||
{ | ||
return bin2hex(random_bytes(16)); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Developers should be able to plug into this nonce generating behavior since their CSP setup could be completely different. For instance, when using spatie's package, one should be able to use the package's nonce_generator
. In fact, I can't think of a single use case where the nonce generated by this package would be used.
… tag and added explanations in the readme
Waiting for the #96 to be merged to add the csp nonce to the modal script. |
Cover #46 by adding csp option on default script tag.
By setting CSP_ENABLE=true in your .env file, this package provides access to a nonce in the default script tag. The CSP_ENABLE flag is also used by Spatie’s laravel-csp package.
A CSP nonce should consist of at least 16 bytes of cryptographically secure random data, and be safe for use in HTML attributes and HTTP headers. I chose to use bin2hex() instead of base64_encode() because it can generate characters that are unsafe for these contexts.
Since a csp_nonce() helper does not exist by default, I implemented a custom one.