|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\HTTP; |
15 | 15 |
|
| 16 | +use CodeIgniter\Config\Services; |
16 | 17 | use CodeIgniter\Exceptions\InvalidArgumentException; |
17 | 18 | use CodeIgniter\Test\CIUnitTestCase; |
18 | 19 | use CodeIgniter\Test\TestResponse; |
@@ -90,15 +91,68 @@ public function testExistence(): void |
90 | 91 | { |
91 | 92 | $this->assertTrue($this->work()); |
92 | 93 | $this->assertHeaderEmitted('Content-Security-Policy:'); |
| 94 | + $this->assertHeaderNotEmitted('Content-Security-Policy-Report-Only:'); |
| 95 | + $this->assertHeaderNotEmitted('Reporting-Endpoints:'); |
93 | 96 | } |
94 | 97 |
|
95 | 98 | #[PreserveGlobalState(false)] |
96 | 99 | #[RunInSeparateProcess] |
97 | 100 | public function testReportOnly(): void |
98 | 101 | { |
99 | | - $this->csp->reportOnly(false); |
| 102 | + $config = new CSPConfig(); |
| 103 | + $config->reportOnly = true; |
| 104 | + |
| 105 | + Services::injectMock('csp', new ContentSecurityPolicy($config)); |
| 106 | + |
| 107 | + $this->response = new Response(config(App::class)); |
| 108 | + $this->response->pretend(false); |
| 109 | + $this->csp = $this->response->getCSP(); |
| 110 | + |
100 | 111 | $this->assertTrue($this->work()); |
101 | | - $this->assertHeaderEmitted('Content-Security-Policy:'); |
| 112 | + $this->assertHeaderEmitted('Content-Security-Policy-Report-Only:'); |
| 113 | + $this->assertHeaderNotEmitted('Content-Security-Policy:'); |
| 114 | + $this->assertHeaderNotEmitted('Reporting-Endpoints:'); |
| 115 | + } |
| 116 | + |
| 117 | + #[PreserveGlobalState(false)] |
| 118 | + #[RunInSeparateProcess] |
| 119 | + public function testExistingCSPHeaderStringIsPreserved(): void |
| 120 | + { |
| 121 | + $this->response->setHeader('Content-Security-Policy', "frame-src 'none'"); |
| 122 | + |
| 123 | + $this->assertTrue($this->work()); |
| 124 | + |
| 125 | + $header = $this->getHeaderEmitted('Content-Security-Policy:'); |
| 126 | + $this->assertIsString($header); |
| 127 | + $this->assertStringContainsString("frame-src 'none', base-uri 'self'", $header); |
| 128 | + } |
| 129 | + |
| 130 | + #[PreserveGlobalState(false)] |
| 131 | + #[RunInSeparateProcess] |
| 132 | + public function testExistingCSPHeaderArrayIsPreserved(): void |
| 133 | + { |
| 134 | + $this->response->setHeader('Content-Security-Policy', [ |
| 135 | + "frame-src 'none'", |
| 136 | + "media-src 'none'", |
| 137 | + ]); |
| 138 | + |
| 139 | + $this->assertTrue($this->work()); |
| 140 | + |
| 141 | + $header = $this->getHeaderEmitted('Content-Security-Policy:'); |
| 142 | + $this->assertIsString($header); |
| 143 | + $this->assertStringContainsString("frame-src 'none', media-src 'none', base-uri 'self'", $header); |
| 144 | + } |
| 145 | + |
| 146 | + #[PreserveGlobalState(false)] |
| 147 | + #[RunInSeparateProcess] |
| 148 | + public function testExistingReportOnlyHeaderIsPreserved(): void |
| 149 | + { |
| 150 | + $this->response->setHeader('Content-Security-Policy-Report-Only', "frame-src 'none'"); |
| 151 | + |
| 152 | + $this->assertTrue($this->work()); |
| 153 | + |
| 154 | + $header = $this->getHeaderEmitted('Content-Security-Policy-Report-Only:'); |
| 155 | + $this->assertSame("Content-Security-Policy-Report-Only: frame-src 'none'", $header); |
102 | 156 | } |
103 | 157 |
|
104 | 158 | #[PreserveGlobalState(false)] |
@@ -609,6 +663,23 @@ public function testReportTo(): void |
609 | 663 | $this->assertSame('Reporting-Endpoints: default="http://example.com/csp-reports"', $header); |
610 | 664 | } |
611 | 665 |
|
| 666 | + #[PreserveGlobalState(false)] |
| 667 | + #[RunInSeparateProcess] |
| 668 | + public function testExistingReportingEndpointsHeaderIsPreserved(): void |
| 669 | + { |
| 670 | + $this->response->setHeader('Reporting-Endpoints', 'existing="http://example.com/existing-reports"'); |
| 671 | + $this->csp->addReportingEndpoints(['default' => 'http://example.com/csp-reports']); |
| 672 | + $this->csp->setReportToEndpoint('default'); |
| 673 | + |
| 674 | + $this->assertTrue($this->work()); |
| 675 | + |
| 676 | + $header = $this->getHeaderEmitted('Reporting-Endpoints:'); |
| 677 | + $this->assertSame( |
| 678 | + 'Reporting-Endpoints: existing="http://example.com/existing-reports", default="http://example.com/csp-reports"', |
| 679 | + $header, |
| 680 | + ); |
| 681 | + } |
| 682 | + |
612 | 683 | #[PreserveGlobalState(false)] |
613 | 684 | #[RunInSeparateProcess] |
614 | 685 | public function testReportToMultipleEndpoints(): void |
|
0 commit comments