Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,14 +878,34 @@ protected function addOption($options, string $target, ?bool $explicitReporting
{
// Ensure we have an array to work with...
if (is_string($this->{$target})) {
$this->{$target} = [$this->{$target} => $this->reportOnly];
$targetValue = trim($this->{$target});

$this->{$target} = [];

if ($targetValue !== '') {
foreach (preg_split('/\s+/', $targetValue) as $opt) {
$this->{$target}[$opt] = $this->reportOnly;
}
}
} elseif (! is_array($this->{$target})) {
$this->{$target} = [];
}

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

foreach ($options as $option) {
$option = (string) $option;

if ($option === '' && $target !== 'sandbox') {
continue;
}

$this->{$target}[$option] = $explicitReporting ?? $this->reportOnly;
}

if ($target === 'sandbox' && count($this->{$target}) > 1 && isset($this->{$target}[''])) {
unset($this->{$target}['']);
}
}

/**
Expand Down Expand Up @@ -1013,13 +1033,25 @@ protected function buildHeaders(ResponseInterface $response)
protected function addToHeader(string $name, $values = null)
{
if (is_string($values)) {
$values = [$values => $this->reportOnly];
$valueStr = trim($values);

if ($valueStr === '') {
return;
}

$values = [];

foreach (preg_split('/\s+/', $valueStr) as $val) {
$values[$val] = $this->reportOnly;
}
}

$sources = [];
$reportSources = [];

foreach ($values as $value => $reportOnly) {
$value = (string) $value;

if (
in_array($value, $this->validSources, true)
|| str_starts_with($value, 'nonce-')
Expand Down
64 changes: 64 additions & 0 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,70 @@ public function testSandboxFlags(): void
$this->assertContains('sandbox allow-popups allow-top-navigation', $this->getCspDirectives($header));
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testSandboxEmptyFlagThenFlags(): void
{
$this->csp->reportOnly(false);
$this->csp->addSandbox('');
$this->csp->addSandbox('allow-scripts');
$this->assertTrue($this->work());

$header = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertIsString($header);
$this->assertContains('sandbox allow-scripts', $this->getCspDirectives($header));
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testAddOptionWithSpaceSeparatedString(): void
{
$config = new CSPConfig();
$config->baseURI = 'self example.com';

$csp = new ContentSecurityPolicy($config);

$response = new Response(config(App::class));
$response->pretend(true);

$csp->addBaseURI('another.com');
$csp->finalize($response);

$directives = $this->getCspDirectives($response->getHeaderLine('Content-Security-Policy'));
$this->assertContains("base-uri 'self' example.com another.com", $directives);
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testAddOptionWithEmptyStringTarget(): void
{
$config = new CSPConfig();
$config->baseURI = '';

$csp = new ContentSecurityPolicy($config);

$response = new Response(config(App::class));
$response->pretend(true);

$csp->addBaseURI('example.com');
$csp->finalize($response);

$directives = $this->getCspDirectives($response->getHeaderLine('Content-Security-Policy'));
$this->assertContains('base-uri example.com', $directives);
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testAddOptionNumericString(): void
{
$this->csp->addBaseURI('0');
$this->assertTrue($this->work());

$header = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertIsString($header);
$this->assertContains('base-uri 0', $this->getCspDirectives($header));
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testUpgradeInsecureRequests(): void
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Bugs Fixed
**********

- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
- **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.
- **Security:** Fixed a bug where a valid JSON request body that is not an object
(for example a JSON array) was corrupted after CSRF verification when the
token was sent via the ``X-CSRF-TOKEN`` header.
Expand Down
Loading