Skip to content

Commit 65b1a04

Browse files
Apply code suggestions
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
1 parent e42537f commit 65b1a04

File tree

4 files changed

+57
-72
lines changed

4 files changed

+57
-72
lines changed

system/HTTP/ContentSecurityPolicy.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,10 @@ protected function generateNonces(ResponseInterface $response)
902902
$pattern = sprintf('/(%s|%s)/', preg_quote($this->styleNonceTag, '/'), preg_quote($this->scriptNonceTag, '/'));
903903

904904
$body = preg_replace_callback($pattern, function ($match): string {
905+
if (! $this->enabled()) {
906+
return '';
907+
}
908+
905909
$nonce = $match[0] === $this->styleNonceTag ? $this->getStyleNonce() : $this->getScriptNonce();
906910

907911
return "nonce=\"{$nonce}\"";
@@ -923,6 +927,10 @@ protected function buildHeaders(ResponseInterface $response)
923927
$response->setHeader('Content-Security-Policy-Report-Only', []);
924928
$response->setHeader('Reporting-Endpoints', []);
925929

930+
if (! $this->enabled()) {
931+
return;
932+
}
933+
926934
if (in_array($this->baseURI, ['', null, []], true)) {
927935
$this->baseURI = 'self';
928936
}

system/HTTP/ResponseTrait.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,7 @@ public function send()
367367
{
368368
// If we're enforcing a Content Security Policy,
369369
// we need to give it a chance to build out it's headers.
370-
if ($this->CSP->enabled()) {
371-
$this->CSP->finalize($this);
372-
} else {
373-
$this->body = $this->CSP->clearNoncePlaceholders($this->body ?? '');
374-
}
370+
$this->CSP->finalize($this);
375371

376372
$this->sendHeaders();
377373
$this->sendCookies();

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -937,67 +937,4 @@ public function testClearDirective(): void
937937
$this->assertNotContains('report-uri http://example.com/csp/reports', $directives);
938938
$this->assertNotContains('report-to default', $directives);
939939
}
940-
941-
public function testClearNoncePlaceholdersWithDefaultTags(): void
942-
{
943-
$config = new CSPConfig();
944-
$csp = new ContentSecurityPolicy($config);
945-
946-
$body = 'Test {csp-script-nonce} and {csp-style-nonce} here';
947-
$cleaned = $csp->clearNoncePlaceholders($body);
948-
949-
$this->assertSame('Test and here', $cleaned);
950-
$this->assertStringNotContainsString('{csp-script-nonce}', $cleaned);
951-
$this->assertStringNotContainsString('{csp-style-nonce}', $cleaned);
952-
}
953-
954-
public function testClearNoncePlaceholdersWithCustomTags(): void
955-
{
956-
$config = new CSPConfig();
957-
$config->scriptNonceTag = '{custom-script-nonce}';
958-
$config->styleNonceTag = '{custom-style-nonce}';
959-
$csp = new ContentSecurityPolicy($config);
960-
961-
$body = 'Test {custom-script-nonce} and {custom-style-nonce} here';
962-
$cleaned = $csp->clearNoncePlaceholders($body);
963-
964-
$this->assertSame('Test and here', $cleaned);
965-
$this->assertStringNotContainsString('{custom-script-nonce}', $cleaned);
966-
$this->assertStringNotContainsString('{custom-style-nonce}', $cleaned);
967-
}
968-
969-
public function testClearNoncePlaceholdersWithEmptyBody(): void
970-
{
971-
$config = new CSPConfig();
972-
$csp = new ContentSecurityPolicy($config);
973-
974-
$body = '';
975-
$cleaned = $csp->clearNoncePlaceholders($body);
976-
977-
$this->assertSame('', $cleaned);
978-
}
979-
980-
public function testClearNoncePlaceholdersWithNoPlaceholders(): void
981-
{
982-
$config = new CSPConfig();
983-
$csp = new ContentSecurityPolicy($config);
984-
985-
$body = 'Test body with no placeholders';
986-
$cleaned = $csp->clearNoncePlaceholders($body);
987-
988-
$this->assertSame($body, $cleaned);
989-
}
990-
991-
public function testClearNoncePlaceholdersWithMultiplePlaceholders(): void
992-
{
993-
$config = new CSPConfig();
994-
$csp = new ContentSecurityPolicy($config);
995-
996-
$body = '<script {csp-script-nonce}>a</script><script {csp-script-nonce}>b</script><style {csp-style-nonce}>c</style>';
997-
$cleaned = $csp->clearNoncePlaceholders($body);
998-
999-
$this->assertStringNotContainsString('{csp-script-nonce}', $cleaned);
1000-
$this->assertStringNotContainsString('{csp-style-nonce}', $cleaned);
1001-
$this->assertSame('<script >a</script><script >b</script><style >c</style>', $cleaned);
1002-
}
1003940
}

tests/system/HTTP/ResponseTest.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -635,23 +635,67 @@ public function testSendRemovesCustomNoncePlaceholdersWhenCSPDisabled(): void
635635
$this->assertStringContainsString('<style >.x{}</style>', (string) $actual);
636636
}
637637

638-
public function testSendWithCSPDisabledDoesNotAffectBodyWithoutNonceTags(): void
638+
public function testSendNoEffectWhenBodyEmptyAndCSPDisabled(): void
639639
{
640640
$config = new App();
641641
$config->CSPEnabled = false;
642642

643643
$response = new Response($config);
644644
$response->pretend(true);
645645

646-
$body = '<html><script>console.log("test")</script></html>';
646+
$body = '';
647647
$response->setBody($body);
648648

649649
ob_start();
650650
$response->send();
651651
$actual = ob_get_contents();
652652
ob_end_clean();
653653

654-
// Body without nonce tags should remain unchanged
655-
$this->assertSame($body, $actual);
654+
$this->assertSame('', (string) $actual);
655+
}
656+
657+
public function testSendNoEffectWithNoPlaceholdersAndCSPDisabled(): void
658+
{
659+
$config = new App();
660+
$config->CSPEnabled = false;
661+
662+
$response = new Response($config);
663+
$response->pretend(true);
664+
665+
$body = '<html><head><title>Test</title></head><body><p>No placeholders here</p></body></html>';
666+
$response->setBody($body);
667+
668+
ob_start();
669+
$response->send();
670+
$actual = ob_get_contents();
671+
ob_end_clean();
672+
673+
// Body should be unchanged when there are no placeholders and CSP is disabled
674+
$this->assertSame($body, (string) $actual);
675+
}
676+
677+
public function testSendRemovesMultiplePlaceholdersWhenCSPDisabled(): void
678+
{
679+
$config = new App();
680+
$config->CSPEnabled = false;
681+
682+
$response = new Response($config);
683+
$response->pretend(true);
684+
685+
$body = '<html><script {csp-script-nonce}>console.log("test")</script><script {csp-script-nonce}>console.log("test2")</script><style {csp-style-nonce}>.test{}</style><style {csp-style-nonce}>.test2{}</style></html>';
686+
$response->setBody($body);
687+
688+
ob_start();
689+
$response->send();
690+
$actual = ob_get_contents();
691+
ob_end_clean();
692+
693+
// All nonce placeholders should be removed when CSP is disabled
694+
$this->assertStringNotContainsString('{csp-script-nonce}', (string) $actual);
695+
$this->assertStringNotContainsString('{csp-style-nonce}', (string) $actual);
696+
$this->assertStringContainsString('<script >console.log("test")</script>', (string) $actual);
697+
$this->assertStringContainsString('<script >console.log("test2")</script>', (string) $actual);
698+
$this->assertStringContainsString('<style >.test{}</style>', (string) $actual);
699+
$this->assertStringContainsString('<style >.test2{}</style>', (string) $actual);
656700
}
657701
}

0 commit comments

Comments
 (0)