Skip to content
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

Make Content Security Policy (CSP) nonces optional #6845

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/Config/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,12 @@ class ContentSecurityPolicy extends BaseConfig
* @var bool
*/
public $autoNonce = true;

/**
* When enabled will add nonce to script, style and headers.
*
* @var bool
*/
public $nonceEnabled = true;

}
15 changes: 13 additions & 2 deletions system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ class ContentSecurityPolicy
* @var bool
*/
protected $autoNonce = true;

/**
* When enabled will add nonce to script, style and headers otherwise nonces will be disabled.
*
* @var boolean
*/
protected $nonceEnabled = false;

/**
* An array of header info since we have
Expand Down Expand Up @@ -677,7 +684,7 @@ protected function generateNonces(ResponseInterface $response)
$body = preg_replace_callback($pattern, function ($match) {
$nonce = $match[0] === $this->styleNonceTag ? $this->getStyleNonce() : $this->getScriptNonce();

return "nonce=\"{$nonce}\"";
return $this->nonceEnabled === false ? "" : "nonce=\"{$nonce}\"";
}, $body);

$response->setBody($body);
Expand Down Expand Up @@ -788,7 +795,11 @@ protected function addToHeader(string $name, $values = null)
}

if (strpos($value, 'nonce-') === 0) {
$value = "'{$value}'";
if ($this->nonceEnabled === false) {
$value = str_replace('nonce-', '', $value);
} else {
$value = "'{$value}'";
}
}

if ($reportOnly === true) {
Expand Down