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