-
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
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,124 @@ 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_contents(); | ||
| ob_end_clean(); | ||
|
Comment on lines
+595
to
+596
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. Can you do |
||
|
|
||
| // Nonce placeholders should be removed when CSP is disabled | ||
| $this->assertStringNotContainsString('{csp-script-nonce}', (string) $actual); | ||
|
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. Add an |
||
| $this->assertStringNotContainsString('{csp-style-nonce}', (string) $actual); | ||
| $this->assertStringContainsString('<script >console.log("test")</script>', (string) $actual); | ||
| $this->assertStringContainsString('<style >.test{}</style>', (string) $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)); | ||
|
|
||
| $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_contents(); | ||
| ob_end_clean(); | ||
|
Comment on lines
+628
to
+629
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. Use |
||
|
|
||
| // Custom nonce placeholders should be removed when CSP is disabled | ||
| $this->assertStringNotContainsString('{custom-script-tag}', (string) $actual); | ||
|
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. Add |
||
| $this->assertStringNotContainsString('{custom-style-tag}', (string) $actual); | ||
| $this->assertStringContainsString('<script >test()</script>', (string) $actual); | ||
| $this->assertStringContainsString('<style >.x{}</style>', (string) $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_contents(); | ||
| ob_end_clean(); | ||
|
|
||
| $this->assertSame('', (string) $actual); | ||
|
Comment on lines
+651
to
+654
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. Use |
||
| } | ||
|
|
||
| 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_contents(); | ||
| ob_end_clean(); | ||
|
|
||
| // Body should be unchanged when there are no placeholders and CSP is disabled | ||
| $this->assertSame($body, (string) $actual); | ||
|
Comment on lines
+670
to
+674
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. Use |
||
| } | ||
|
|
||
| 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_contents(); | ||
| ob_end_clean(); | ||
|
|
||
| // All nonce placeholders should be removed when CSP is disabled | ||
| $this->assertStringNotContainsString('{csp-script-nonce}', (string) $actual); | ||
|
Comment on lines
+690
to
+694
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. Use |
||
| $this->assertStringNotContainsString('{csp-style-nonce}', (string) $actual); | ||
| $this->assertStringContainsString('<script >console.log("test")</script>', (string) $actual); | ||
| $this->assertStringContainsString('<script >console.log("test2")</script>', (string) $actual); | ||
| $this->assertStringContainsString('<style >.test{}</style>', (string) $actual); | ||
| $this->assertStringContainsString('<style >.test2{}</style>', (string) $actual); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, if CSP is disabled and auto-nonce is also disabled, then replacement with
''would not happen. Can you add a test where$CSPEnabledis false and$autoNonceis also false where the response body will still be correctly replaced with empty strings on the tags?