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
21 changes: 19 additions & 2 deletions system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,30 @@ public function addConnectSrc($uri, ?bool $explicitReporting = null)
*
* @return $this
*/
public function setDefaultSrc($uri, ?bool $explicitReporting = null)
public function addDefaultSrc($uri, ?bool $explicitReporting = null)
{
$this->defaultSrc = [(string) $uri => $explicitReporting ?? $this->reportOnly];
$this->addOption($uri, 'defaultSrc', $explicitReporting ?? $this->reportOnly);

return $this;
}

/**
* Adds a new value to the `default-src` directive.
*
* `default-src` is the URI that is used for many of the settings when
* no other source has been set.
*
* @see http://www.w3.org/TR/CSP/#directive-default-src
*
* @param list<string>|string $uri
*
* @return $this
*/
public function setDefaultSrc($uri, ?bool $explicitReporting = null)
{
return $this->addDefaultSrc($uri, $explicitReporting);
}

/**
* Adds a new value to the `font-src` directive.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,37 @@ public function testDefaultSrc(): void

$header = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertIsString($header);
$this->assertContains('default-src maybe.com iffy.com', $this->getCspDirectives($header));
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testDefaultSrcRich(): void
{
$this->csp->reportOnly(false);
$this->csp->setDefaultSrc(['self', 'maybe.com', 'iffy.com']);
$this->assertTrue($this->work());

$header = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertIsString($header);
$this->assertContains("default-src 'self' maybe.com iffy.com", $this->getCspDirectives($header));
}

#[PreserveGlobalState(false)]
#[RunInSeparateProcess]
public function testAddDefaultSrc(): void
{
$this->csp->addDefaultSrc('maybe.com');
$this->csp->addDefaultSrc('iffy.com', true);
$this->assertTrue($this->work());

$header = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
$this->assertIsString($header);
$this->assertContains('default-src iffy.com', $this->getCspDirectives($header));

$header = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertIsString($header);
$this->assertContains('default-src maybe.com', $this->getCspDirectives($header));
}

#[PreserveGlobalState(false)]
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::setDefaultSrc()`` where it overwrote existing directives instead of adding them, and failed when passed an array of URIs. Added ``ContentSecurityPolicy::addDefaultSrc()`` for consistency with other directive methods.
- **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