Skip to content

Commit 950d45b

Browse files
committed
Nonce generation optimization.
Signed-off-by: Andrey Pyzhikov <5071@mail.ru>
1 parent 2179c95 commit 950d45b

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

system/HTTP/ContentSecurityPolicy.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -671,18 +671,12 @@ protected function generateNonces(ResponseInterface &$response)
671671
return;
672672
}
673673

674-
// Replace style placeholders with nonces
675-
$pattern = '/' . preg_quote($this->styleNonceTag, '/') . '/';
676-
$body = preg_replace_callback($pattern, function () {
677-
$nonce = $this->getStyleNonce();
674+
// Replace style and script placeholders with nonces
675+
$pattern = '/(' . preg_quote($this->styleNonceTag, '/')
676+
. '|' . preg_quote($this->scriptNonceTag, '/') . ')/';
678677

679-
return "nonce=\"{$nonce}\"";
680-
}, $body);
681-
682-
// Replace script placeholders with nonces
683-
$pattern = '/' . preg_quote($this->scriptNonceTag, '/') . '/';
684-
$body = preg_replace_callback($pattern, function () {
685-
$nonce = $this->getScriptNonce();
678+
$body = preg_replace_callback($pattern, function ($match) {
679+
$nonce = $match[0] === $this->styleNonceTag ? $this->getStyleNonce() : $this->getScriptNonce();
686680

687681
return "nonce=\"{$nonce}\"";
688682
}, $body);

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,16 @@ public function testBodyScriptNonce()
459459
$this->response->setBody($body);
460460
$this->csp->addScriptSrc('cdn.cloudy.com');
461461

462-
$result = $this->work($body);
462+
$result = $this->work($body);
463+
$nonceStyle = array_filter(
464+
$this->getPrivateProperty($this->csp, 'styleSrc'),
465+
static fn ($value) => strpos($value, 'nonce-') === 0
466+
);
463467

464468
$this->assertStringContainsString('nonce=', $this->response->getBody());
465469
$result = $this->getHeaderEmitted('Content-Security-Policy');
466470
$this->assertStringContainsString('nonce-', $result);
471+
$this->assertSame([], $nonceStyle);
467472
}
468473

469474
public function testBodyScriptNonceCustomScriptTag()
@@ -525,11 +530,16 @@ public function testBodyStyleNonce()
525530
$this->response->setBody($body);
526531
$this->csp->addStyleSrc('cdn.cloudy.com');
527532

528-
$result = $this->work($body);
533+
$result = $this->work($body);
534+
$nonceScript = array_filter(
535+
$this->getPrivateProperty($this->csp, 'scriptSrc'),
536+
static fn ($value) => strpos($value, 'nonce-') === 0
537+
);
529538

530539
$this->assertStringContainsString('nonce=', $this->response->getBody());
531540
$result = $this->getHeaderEmitted('Content-Security-Policy');
532541
$this->assertStringContainsString('nonce-', $result);
542+
$this->assertSame([], $nonceScript);
533543
}
534544

535545
public function testBodyStyleNonceCustomStyleTag()

0 commit comments

Comments
 (0)