Skip to content

Commit a37fe7c

Browse files
committed
fix: preserve empty source lists and reporting mode markers in ContentSecurityPolicy
1 parent 303699c commit a37fe7c

2 files changed

Lines changed: 123 additions & 39 deletions

File tree

‎system/HTTP/ContentSecurityPolicy.php‎

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -878,43 +878,38 @@ protected function addOption($options, string $target, ?bool $explicitReporting
878878
{
879879
// Ensure we have an array to work with...
880880
if (is_string($this->{$target})) {
881-
$targetValue = trim($this->{$target});
882-
883-
$this->{$target} = [];
884-
885-
if ($targetValue !== '') {
886-
foreach (preg_split('/\s+/', $targetValue) as $opt) {
887-
$this->{$target}[$opt] = $this->reportOnly;
888-
}
889-
} elseif (in_array($target, ['sandbox', 'frameAncestors'], true)) {
890-
$this->{$target}[''] = $this->reportOnly;
891-
}
881+
$this->{$target} = $this->stringToOptions($this->{$target});
892882
} elseif (! is_array($this->{$target})) {
893883
$this->{$target} = [];
894884
}
895885

896886
$options = is_array($options) ? $options : [$options];
897887

898888
foreach ($options as $option) {
899-
$option = (string) $option;
889+
$this->{$target}[trim((string) $option)] = $explicitReporting ?? $this->reportOnly;
890+
}
891+
}
900892

901-
if ($option === '' && ! in_array($target, ['sandbox', 'frameAncestors'], true)) {
902-
continue;
903-
}
893+
/**
894+
* Converts a string directive value into an array of options.
895+
*
896+
* @return array<string, bool>
897+
*/
898+
private function stringToOptions(string $values): array
899+
{
900+
$valueStr = trim($values);
904901

905-
$this->{$target}[$option] = $explicitReporting ?? $this->reportOnly;
902+
if ($valueStr === '') {
903+
return ['' => $this->reportOnly];
906904
}
907905

908-
if (in_array($target, ['sandbox', 'frameAncestors'], true) && isset($this->{$target}[''])) {
909-
$emptyReportOnly = $this->{$target}[''];
906+
$options = [];
910907

911-
foreach ($this->{$target} as $val => $reportOnly) {
912-
if ($val !== '' && $reportOnly === $emptyReportOnly) {
913-
unset($this->{$target}['']);
914-
break;
915-
}
916-
}
908+
foreach (preg_split('/\s+/', $valueStr) as $val) {
909+
$options[$val] = $this->reportOnly;
917910
}
911+
912+
return $options;
918913
}
919914

920915
/**
@@ -1041,22 +1036,12 @@ protected function buildHeaders(ResponseInterface $response)
10411036
*/
10421037
protected function addToHeader(string $name, $values = null)
10431038
{
1044-
if (is_string($values)) {
1045-
$valueStr = trim($values);
1046-
1047-
if ($valueStr === '') {
1048-
if (! in_array($name, ['sandbox', 'frame-ancestors'], true)) {
1049-
return;
1050-
}
1051-
1052-
$values = ['' => $this->reportOnly];
1053-
} else {
1054-
$values = [];
1039+
if ($values === null) {
1040+
return;
1041+
}
10551042

1056-
foreach (preg_split('/\s+/', $valueStr) as $val) {
1057-
$values[$val] = $this->reportOnly;
1058-
}
1059-
}
1043+
if (is_string($values)) {
1044+
$values = $this->stringToOptions($values);
10601045
}
10611046

10621047
$sources = [];

‎tests/system/HTTP/ContentSecurityPolicyTest.php‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,13 +868,45 @@ public function testSandboxMixedModeEnforcedAndReportOnly(): void
868868
$this->assertContains('sandbox allow-scripts', $this->getCspDirectives($reportOnly));
869869
}
870870

871+
#[PreserveGlobalState(false)]
872+
#[RunInSeparateProcess]
873+
public function testOptionMovedBetweenReportingModesPreservesEmptyMarker(): void
874+
{
875+
$this->csp->addSandbox('', false);
876+
$this->csp->addSandbox('allow-scripts', false);
877+
$this->csp->addSandbox('allow-scripts', true);
878+
879+
$this->csp->addFrameAncestor('', false);
880+
$this->csp->addFrameAncestor('https://example.com', false);
881+
$this->csp->addFrameAncestor('https://example.com', true);
882+
883+
$this->assertTrue($this->work());
884+
885+
$enforced = $this->getHeaderEmitted('Content-Security-Policy');
886+
$this->assertIsString($enforced);
887+
$enforcedDirectives = $this->getCspDirectives($enforced);
888+
$this->assertContains('sandbox', $enforcedDirectives);
889+
$this->assertContains('frame-ancestors', $enforcedDirectives);
890+
$this->assertNotContains('sandbox allow-scripts', $enforcedDirectives);
891+
$this->assertNotContains('frame-ancestors https://example.com', $enforcedDirectives);
892+
893+
$reportOnly = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
894+
$this->assertIsString($reportOnly);
895+
$reportOnlyDirectives = $this->getCspDirectives($reportOnly);
896+
$this->assertContains('sandbox allow-scripts', $reportOnlyDirectives);
897+
$this->assertContains('frame-ancestors https://example.com', $reportOnlyDirectives);
898+
}
899+
871900
#[PreserveGlobalState(false)]
872901
#[RunInSeparateProcess]
873902
public function testConfiguredEmptyDirectivesEmitted(): void
874903
{
875904
$config = new CSPConfig();
876905
$config->sandbox = '';
877906
$config->frameAncestors = '';
907+
$config->formAction = '';
908+
$config->fontSrc = '';
909+
$config->connectSrc = '';
878910

879911
$csp = new ContentSecurityPolicy($config);
880912

@@ -886,6 +918,73 @@ public function testConfiguredEmptyDirectivesEmitted(): void
886918
$directives = $this->getCspDirectives($response->getHeaderLine('Content-Security-Policy'));
887919
$this->assertContains('sandbox', $directives);
888920
$this->assertContains('frame-ancestors', $directives);
921+
$this->assertContains('form-action', $directives);
922+
$this->assertContains('font-src', $directives);
923+
$this->assertContains('connect-src', $directives);
924+
}
925+
926+
#[PreserveGlobalState(false)]
927+
#[RunInSeparateProcess]
928+
public function testConfiguredEmptyFormActionWithAddFormActionNoop(): void
929+
{
930+
$config = new CSPConfig();
931+
$config->formAction = '';
932+
933+
$csp = new ContentSecurityPolicy($config);
934+
935+
$response = new Response(config(App::class));
936+
$response->pretend(true);
937+
938+
$csp->addFormAction([]);
939+
$csp->finalize($response);
940+
941+
$directives = $this->getCspDirectives($response->getHeaderLine('Content-Security-Policy'));
942+
$this->assertContains('form-action', $directives);
943+
}
944+
945+
#[PreserveGlobalState(false)]
946+
#[RunInSeparateProcess]
947+
public function testClearDirectiveThenAddEmptySource(): void
948+
{
949+
$this->csp->clearDirective('form-action');
950+
$this->csp->addFormAction('');
951+
$this->csp->clearDirective('connect-src');
952+
$this->csp->addConnectSrc('');
953+
$this->assertTrue($this->work());
954+
955+
$header = $this->getHeaderEmitted('Content-Security-Policy');
956+
$this->assertIsString($header);
957+
$directives = $this->getCspDirectives($header);
958+
$this->assertContains('form-action', $directives);
959+
$this->assertContains('connect-src', $directives);
960+
}
961+
962+
#[PreserveGlobalState(false)]
963+
#[RunInSeparateProcess]
964+
public function testAddEmptySourceToFontSrc(): void
965+
{
966+
$this->csp->addFontSrc('');
967+
$this->assertTrue($this->work());
968+
969+
$header = $this->getHeaderEmitted('Content-Security-Policy');
970+
$this->assertIsString($header);
971+
$directives = $this->getCspDirectives($header);
972+
$this->assertContains('font-src', $directives);
973+
}
974+
975+
#[PreserveGlobalState(false)]
976+
#[RunInSeparateProcess]
977+
public function testRedundantEmptySourceFilteredWhenOtherSourcesPresent(): void
978+
{
979+
// connectSrc defaults to 'self'
980+
$this->csp->addConnectSrc('');
981+
$this->assertTrue($this->work());
982+
983+
$header = $this->getHeaderEmitted('Content-Security-Policy');
984+
$this->assertIsString($header);
985+
$directives = $this->getCspDirectives($header);
986+
$this->assertContains("connect-src 'self'", $directives);
987+
$this->assertNotContains('connect-src', $directives);
889988
}
890989

891990
#[PreserveGlobalState(false)]

0 commit comments

Comments
 (0)