Skip to content

Conversation

Debatty-Tom
Copy link
Contributor

@Debatty-Tom Debatty-Tom commented Oct 8, 2025

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.

Comment on lines 58 to 67
/*
|--------------------------------------------------------------------------
| 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.
|
*/
Copy link
Member

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)

|
*/

'csp_enable' => env('CSP_ENABLE', false),
Copy link
Member

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).


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cookie as CookieFacade;
use Illuminate\Support\Str;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused facade.

Comment on lines 202 to 216
$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));
}

Copy link
Member

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.

@Debatty-Tom Debatty-Tom changed the base branch from main to 2.x October 21, 2025 14:05
@Debatty-Tom
Copy link
Contributor Author

Waiting for the #96 to be merged to add the csp nonce to the modal script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants