Skip to content

Commit 74c4afa

Browse files
committed
fix: normalize string values and keys in ContentSecurityPolicy::addOption()
1 parent 8e2db88 commit 74c4afa

3 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎system/HTTP/ContentSecurityPolicy.php‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,14 +878,34 @@ 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-
$this->{$target} = [$this->{$target} => $this->reportOnly];
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+
}
890+
} elseif (! is_array($this->{$target})) {
891+
$this->{$target} = [];
882892
}
883893

884894
$options = is_array($options) ? $options : [$options];
885895

886896
foreach ($options as $option) {
897+
$option = (string) $option;
898+
899+
if ($option === '' && $target !== 'sandbox') {
900+
continue;
901+
}
902+
887903
$this->{$target}[$option] = $explicitReporting ?? $this->reportOnly;
888904
}
905+
906+
if ($target === 'sandbox' && count($this->{$target}) > 1 && isset($this->{$target}[''])) {
907+
unset($this->{$target}['']);
908+
}
889909
}
890910

891911
/**
@@ -1013,13 +1033,25 @@ protected function buildHeaders(ResponseInterface $response)
10131033
protected function addToHeader(string $name, $values = null)
10141034
{
10151035
if (is_string($values)) {
1016-
$values = [$values => $this->reportOnly];
1036+
$valueStr = trim($values);
1037+
1038+
if ($valueStr === '') {
1039+
return;
1040+
}
1041+
1042+
$values = [];
1043+
1044+
foreach (preg_split('/\s+/', $valueStr) as $val) {
1045+
$values[$val] = $this->reportOnly;
1046+
}
10171047
}
10181048

10191049
$sources = [];
10201050
$reportSources = [];
10211051

10221052
foreach ($values as $value => $reportOnly) {
1053+
$value = (string) $value;
1054+
10231055
if (
10241056
in_array($value, $this->validSources, true)
10251057
|| str_starts_with($value, 'nonce-')

‎tests/system/HTTP/ContentSecurityPolicyTest.php‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,70 @@ public function testSandboxFlags(): void
756756
$this->assertContains('sandbox allow-popups allow-top-navigation', $this->getCspDirectives($header));
757757
}
758758

759+
#[PreserveGlobalState(false)]
760+
#[RunInSeparateProcess]
761+
public function testSandboxEmptyFlagThenFlags(): void
762+
{
763+
$this->csp->reportOnly(false);
764+
$this->csp->addSandbox('');
765+
$this->csp->addSandbox('allow-scripts');
766+
$this->assertTrue($this->work());
767+
768+
$header = $this->getHeaderEmitted('Content-Security-Policy');
769+
$this->assertIsString($header);
770+
$this->assertContains('sandbox allow-scripts', $this->getCspDirectives($header));
771+
}
772+
773+
#[PreserveGlobalState(false)]
774+
#[RunInSeparateProcess]
775+
public function testAddOptionWithSpaceSeparatedString(): void
776+
{
777+
$config = new CSPConfig();
778+
$config->baseURI = 'self example.com';
779+
780+
$csp = new ContentSecurityPolicy($config);
781+
782+
$response = new Response(config(App::class));
783+
$response->pretend(true);
784+
785+
$csp->addBaseURI('another.com');
786+
$csp->finalize($response);
787+
788+
$directives = $this->getCspDirectives($response->getHeaderLine('Content-Security-Policy'));
789+
$this->assertContains("base-uri 'self' example.com another.com", $directives);
790+
}
791+
792+
#[PreserveGlobalState(false)]
793+
#[RunInSeparateProcess]
794+
public function testAddOptionWithEmptyStringTarget(): void
795+
{
796+
$config = new CSPConfig();
797+
$config->baseURI = '';
798+
799+
$csp = new ContentSecurityPolicy($config);
800+
801+
$response = new Response(config(App::class));
802+
$response->pretend(true);
803+
804+
$csp->addBaseURI('example.com');
805+
$csp->finalize($response);
806+
807+
$directives = $this->getCspDirectives($response->getHeaderLine('Content-Security-Policy'));
808+
$this->assertContains('base-uri example.com', $directives);
809+
}
810+
811+
#[PreserveGlobalState(false)]
812+
#[RunInSeparateProcess]
813+
public function testAddOptionNumericString(): void
814+
{
815+
$this->csp->addBaseURI('0');
816+
$this->assertTrue($this->work());
817+
818+
$header = $this->getHeaderEmitted('Content-Security-Policy');
819+
$this->assertIsString($header);
820+
$this->assertContains('base-uri 0', $this->getCspDirectives($header));
821+
}
822+
759823
#[PreserveGlobalState(false)]
760824
#[RunInSeparateProcess]
761825
public function testUpgradeInsecureRequests(): void

‎user_guide_src/source/changelogs/v4.7.6.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bugs Fixed
3131
**********
3232

3333
- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
34+
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::addOption()`` where converting string directive values to array keys resulted in invalid entries for empty strings, unquoted values for space-separated lists, and ``TypeError`` for numeric strings.
3435
- **Security:** Fixed a bug where a valid JSON request body that is not an object
3536
(for example a JSON array) was corrupted after CSRF verification when the
3637
token was sent via the ``X-CSRF-TOKEN`` header.

0 commit comments

Comments
 (0)