Skip to content

Commit 3f8e56b

Browse files
committed
Stop nonces from being added in HTML
1 parent 2caca5b commit 3f8e56b

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

system/HTTP/ContentSecurityPolicy.php

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,17 @@ public function enabled(): bool
411411
*/
412412
public function getStyleNonce(): string
413413
{
414+
if (! $this->enableStyleNonce) {
415+
$this->styleNonce = null;
416+
return '';
417+
}
418+
414419
if ($this->styleNonce === null) {
415420
$this->styleNonce = base64_encode(random_bytes(12));
421+
$this->addStyleSrc('nonce-' . $this->styleNonce);
416422

417-
if ($this->enableStyleNonce) {
418-
$this->addStyleSrc('nonce-' . $this->styleNonce);
419-
420-
if ($this->styleSrcElem !== []) {
421-
$this->addStyleSrcElem('nonce-' . $this->styleNonce);
422-
}
423+
if ($this->styleSrcElem !== []) {
424+
$this->addStyleSrcElem('nonce-' . $this->styleNonce);
423425
}
424426
}
425427

@@ -431,15 +433,17 @@ public function getStyleNonce(): string
431433
*/
432434
public function getScriptNonce(): string
433435
{
436+
if (! $this->enableScriptNonce) {
437+
$this->scriptNonce = null;
438+
return '';
439+
}
440+
434441
if ($this->scriptNonce === null) {
435442
$this->scriptNonce = base64_encode(random_bytes(12));
443+
$this->addScriptSrc('nonce-' . $this->scriptNonce);
436444

437-
if ($this->enableScriptNonce) {
438-
$this->addScriptSrc('nonce-' . $this->scriptNonce);
439-
440-
if ($this->scriptSrcElem !== []) {
441-
$this->addScriptSrcElem('nonce-' . $this->scriptNonce);
442-
}
445+
if ($this->scriptSrcElem !== []) {
446+
$this->addScriptSrcElem('nonce-' . $this->scriptNonce);
443447
}
444448
}
445449

@@ -963,7 +967,20 @@ protected function generateNonces(ResponseInterface $response)
963967
return '';
964968
}
965969

966-
$nonce = $match[0] === $this->styleNonceTag ? $this->getStyleNonce() : $this->getScriptNonce();
970+
if ($match[0] === $this->styleNonceTag) {
971+
if (! $this->enableStyleNonce) {
972+
return '';
973+
}
974+
975+
$nonce = $this->getStyleNonce();
976+
} else {
977+
if (! $this->enableScriptNonce) {
978+
return '';
979+
}
980+
981+
$nonce = $this->getScriptNonce();
982+
}
983+
967984
$attr = 'nonce="' . $nonce . '"';
968985

969986
return $jsonEscape ? str_replace('"', '\\"', $attr) : $attr;

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,12 @@ public function testDisabledScriptNonce(): void
739739
$this->csp->addScriptSrc('self');
740740
$this->csp->addScriptSrc('cdn.cloudy.com');
741741

742-
$this->assertTrue($this->work());
742+
$this->assertTrue($this->work('<script {csp-script-nonce}></script>'));
743743

744744
$header = $this->response->getHeaderLine('Content-Security-Policy');
745+
$body = $this->response->getBody();
746+
747+
$this->assertStringNotContainsString('nonce=', $body);
745748

746749
$this->assertStringContainsString("script-src 'self' cdn.cloudy.com", $header);
747750
$this->assertStringNotContainsString("script-src 'self' cdn.cloudy.com nonce-", $header);
@@ -834,9 +837,12 @@ public function testDisabledStyleNonce(): void
834837
$this->csp->addStyleSrc('self');
835838
$this->csp->addStyleSrc('cdn.cloudy.com');
836839

837-
$this->assertTrue($this->work());
840+
$this->assertTrue($this->work("<style {csp-style-nonce}></style>"));
838841

839842
$header = $this->response->getHeaderLine('Content-Security-Policy');
843+
$body = $this->response->getBody();
844+
845+
$this->assertStringNotContainsString('nonce=', $body);
840846

841847
$this->assertStringContainsString("style-src 'self' cdn.cloudy.com", $header);
842848
$this->assertStringNotContainsString("style-src 'self' cdn.cloudy.com nonce-", $header);

0 commit comments

Comments
 (0)