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
20 changes: 19 additions & 1 deletion system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,30 @@ public function addConnectSrc($uri, ?bool $explicitReporting = null)
* @see http://www.w3.org/TR/CSP/#directive-default-src
*
* @param list<string>|string $uri
*/
public function addDefaultSrc(array|string $uri, ?bool $explicitReporting = null): static
{
$this->addOption($uri, 'defaultSrc', $explicitReporting ?? $this->reportOnly);

return $this;
}

/**
* Sets the `default-src` directive, replacing any existing sources.
*
* `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)
{
$this->defaultSrc = [(string) $uri => $explicitReporting ?? $this->reportOnly];
$this->defaultSrc = [];
$this->addDefaultSrc($uri, $explicitReporting);

return $this;
}
Expand Down
44 changes: 44 additions & 0 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,50 @@ public function testDefaultSrc(): void
$this->assertContains('default-src 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)]
#[RunInSeparateProcess]
public function testAddDefaultSrcAppends(): void
{
$this->csp->reportOnly(false);
$this->csp->setDefaultSrc('maybe.com');
$this->csp->addDefaultSrc('iffy.com');
$this->assertTrue($this->work());

$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 testReportURI(): void
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/changelogs/v4.7.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Message Changes
Changes
*******

- **HTTP:** Added ``ContentSecurityPolicy::addDefaultSrc()`` to allow appending sources to the ``default-src`` directive.

************
Deprecations
************
Expand All @@ -35,6 +37,7 @@ Bugs Fixed
- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
- **Format:** Fixed ``XMLFormatter`` so that an array key that becomes empty after sanitization no longer triggers a ``ValueError``; the fallback tag name ``item`` is used instead. Values that cannot be converted to a string, such as objects not implementing ``Stringable``, now throw a ``FormatException`` instead of a fatal ``Error``.
- **Helpers:** Fixed a regression in v4.7.5 where ``set_value()`` threw a ``TypeError`` when the default value was not a string or an array, such as an integer, a float, ``null``, or a ``Stringable`` object. Such values are now converted to a string.
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::setDefaultSrc()`` where passing an array of URIs resulted in an invalid ``'Array'`` directive.
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
- **HTTP:** Fixed a bug where ``IncomingRequest::isSecure()`` did not recognize ``https`` in the ``X-Forwarded-Proto`` header from a trusted proxy when the value contained uppercase letters or surrounding whitespace, or was represented as a single-element array.
- **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