-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: Hardcoded CSP Nonce Tags in ResponseTrait #9937
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
base: develop
Are you sure you want to change the base?
Changes from all commits
19380aa
489d201
466930a
ca867f7
e42537f
65b1a04
ea893a9
77d5eed
030f4a0
db3d0bd
10c90dc
6808429
40836bd
655e6b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| use DateTimeZone; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
| use ReflectionClass; | ||
|
|
||
| /** | ||
| * @internal | ||
|
|
@@ -577,4 +578,156 @@ public function testPretendOutput(): void | |
|
|
||
| $this->assertSame('Happy days', $actual); | ||
| } | ||
|
|
||
| public function testSendRemovesDefaultNoncePlaceholdersWhenCSPDisabled(): void | ||
| { | ||
| $config = new App(); | ||
| $config->CSPEnabled = false; | ||
|
|
||
| $response = new Response($config); | ||
| $response->pretend(true); | ||
|
|
||
| $body = '<html><script {csp-script-nonce}>console.log("test")</script><style {csp-style-nonce}>.test{}</style></html>'; | ||
| $response->setBody($body); | ||
|
|
||
| ob_start(); | ||
| $response->send(); | ||
| $actual = ob_get_clean(); | ||
|
|
||
| // Nonce placeholders should be removed when CSP is disabled | ||
| $this->assertIsString($actual); | ||
| $this->assertStringNotContainsString('{csp-script-nonce}', $actual); | ||
| $this->assertStringNotContainsString('{csp-style-nonce}', $actual); | ||
| $this->assertStringContainsString('<script >console.log("test")</script>', $actual); | ||
| $this->assertStringContainsString('<style >.test{}</style>', $actual); | ||
| } | ||
|
|
||
| public function testSendRemovesCustomNoncePlaceholdersWhenCSPDisabled(): void | ||
| { | ||
| $appConfig = new App(); | ||
| $appConfig->CSPEnabled = false; | ||
|
|
||
| // Create custom CSP config with custom nonce tags | ||
| $cspConfig = new \Config\ContentSecurityPolicy(); | ||
| $cspConfig->scriptNonceTag = '{custom-script-tag}'; | ||
| $cspConfig->styleNonceTag = '{custom-style-tag}'; | ||
|
|
||
| $response = new Response($appConfig); | ||
| $response->pretend(true); | ||
|
|
||
| // Inject the custom CSP config | ||
| $reflection = new ReflectionClass($response); | ||
| $cspProperty = $reflection->getProperty('CSP'); | ||
| $cspProperty->setValue($response, new ContentSecurityPolicy($cspConfig)); | ||
|
Comment on lines
+618
to
+621
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of these lines, call |
||
|
|
||
| $body = '<html><script {custom-script-tag}>test()</script><style {custom-style-tag}>.x{}</style></html>'; | ||
| $response->setBody($body); | ||
|
|
||
| ob_start(); | ||
| $response->send(); | ||
| $actual = ob_get_clean(); | ||
|
|
||
| // Custom nonce placeholders should be removed when CSP is disabled | ||
| $this->assertIsString($actual); | ||
| $this->assertStringNotContainsString('{custom-script-tag}', $actual); | ||
| $this->assertStringNotContainsString('{custom-style-tag}', $actual); | ||
| $this->assertStringContainsString('<script >test()</script>', $actual); | ||
| $this->assertStringContainsString('<style >.x{}</style>', $actual); | ||
| } | ||
|
|
||
| public function testSendNoEffectWhenBodyEmptyAndCSPDisabled(): void | ||
| { | ||
| $config = new App(); | ||
| $config->CSPEnabled = false; | ||
|
|
||
| $response = new Response($config); | ||
| $response->pretend(true); | ||
|
|
||
| $body = ''; | ||
| $response->setBody($body); | ||
|
|
||
| ob_start(); | ||
| $response->send(); | ||
| $actual = ob_get_clean(); | ||
|
|
||
| $this->assertIsString($actual); | ||
| $this->assertSame('', $actual); | ||
| } | ||
|
|
||
| public function testSendNoEffectWithNoPlaceholdersAndCSPDisabled(): void | ||
| { | ||
| $config = new App(); | ||
| $config->CSPEnabled = false; | ||
|
|
||
| $response = new Response($config); | ||
| $response->pretend(true); | ||
|
|
||
| $body = '<html><head><title>Test</title></head><body><p>No placeholders here</p></body></html>'; | ||
| $response->setBody($body); | ||
|
|
||
| ob_start(); | ||
| $response->send(); | ||
| $actual = ob_get_clean(); | ||
|
|
||
| // Body should be unchanged when there are no placeholders and CSP is disabled | ||
| $this->assertIsString($actual); | ||
| $this->assertSame($body, $actual); | ||
| } | ||
|
|
||
| public function testSendRemovesMultiplePlaceholdersWhenCSPDisabled(): void | ||
| { | ||
| $config = new App(); | ||
| $config->CSPEnabled = false; | ||
|
|
||
| $response = new Response($config); | ||
| $response->pretend(true); | ||
|
|
||
| $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>'; | ||
| $response->setBody($body); | ||
|
|
||
| ob_start(); | ||
| $response->send(); | ||
| $actual = ob_get_clean(); | ||
|
|
||
| // All nonce placeholders should be removed when CSP is disabled | ||
| $this->assertIsString($actual); | ||
| $this->assertStringNotContainsString('{csp-script-nonce}', $actual); | ||
| $this->assertStringNotContainsString('{csp-style-nonce}', $actual); | ||
| $this->assertStringContainsString('<script >console.log("test")</script>', $actual); | ||
| $this->assertStringContainsString('<script >console.log("test2")</script>', $actual); | ||
| $this->assertStringContainsString('<style >.test{}</style>', $actual); | ||
| $this->assertStringContainsString('<style >.test2{}</style>', $actual); | ||
| } | ||
|
|
||
| public function testSendRemovesPlaceholdersWhenBothCSPAndAutoNonceAreDisabled(): void | ||
| { | ||
| $appConfig = new App(); | ||
| $appConfig->CSPEnabled = false; | ||
|
|
||
| // Create custom CSP config with custom nonce tags | ||
| $cspConfig = new \Config\ContentSecurityPolicy(); | ||
| $cspConfig->autoNonce = false; | ||
|
|
||
| $response = new Response($appConfig); | ||
| $response->pretend(true); | ||
|
|
||
| // Inject the custom CSP config | ||
| $reflection = new ReflectionClass($response); | ||
| $cspProperty = $reflection->getProperty('CSP'); | ||
| $cspProperty->setValue($response, new ContentSecurityPolicy($cspConfig)); | ||
|
Comment on lines
+714
to
+717
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
|
|
||
| $body = '<html><script {csp-script-nonce}>test()</script><style {csp-style-nonce}>.x{}</style></html>'; | ||
| $response->setBody($body); | ||
|
|
||
| ob_start(); | ||
| $response->send(); | ||
| $actual = ob_get_clean(); | ||
|
|
||
| // Custom nonce placeholders should be removed when CSP is disabled | ||
| $this->assertIsString($actual); | ||
| $this->assertStringNotContainsString('{csp-script-nonce}', $actual); | ||
| $this->assertStringNotContainsString('{csp-style-nonce}', $actual); | ||
| $this->assertStringContainsString('<script >test()</script>', $actual); | ||
| $this->assertStringContainsString('<style >.x{}</style>', $actual); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.