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

Nonce replacement optimization. #5733

Merged
merged 1 commit into from
Feb 25, 2022
Merged
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
16 changes: 5 additions & 11 deletions system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -671,18 +671,12 @@ protected function generateNonces(ResponseInterface &$response)
return;
}

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

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

// Replace script placeholders with nonces
$pattern = '/' . preg_quote($this->scriptNonceTag, '/') . '/';
$body = preg_replace_callback($pattern, function () {
$nonce = $this->getScriptNonce();
$body = preg_replace_callback($pattern, function ($match) {
$nonce = $match[0] === $this->styleNonceTag ? $this->getStyleNonce() : $this->getScriptNonce();

return "nonce=\"{$nonce}\"";
}, $body);
Expand Down
14 changes: 12 additions & 2 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,16 @@ public function testBodyScriptNonce()
$this->response->setBody($body);
$this->csp->addScriptSrc('cdn.cloudy.com');

$result = $this->work($body);
$result = $this->work($body);
$nonceStyle = array_filter(
$this->getPrivateProperty($this->csp, 'styleSrc'),
static fn ($value) => strpos($value, 'nonce-') === 0
);

$this->assertStringContainsString('nonce=', $this->response->getBody());
$result = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertStringContainsString('nonce-', $result);
$this->assertSame([], $nonceStyle);
}

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

$result = $this->work($body);
$result = $this->work($body);
$nonceScript = array_filter(
$this->getPrivateProperty($this->csp, 'scriptSrc'),
static fn ($value) => strpos($value, 'nonce-') === 0
);

$this->assertStringContainsString('nonce=', $this->response->getBody());
$result = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertStringContainsString('nonce-', $result);
$this->assertSame([], $nonceScript);
}

public function testBodyStyleNonceCustomStyleTag()
Expand Down