Skip to content

Commit 67da259

Browse files
committed
fix: fix array handling in ContentSecurityPolicy::setDefaultSrc() and add addDefaultSrc()
1 parent 6ddf104 commit 67da259

3 files changed

Lines changed: 66 additions & 1 deletion

File tree

‎system/HTTP/ContentSecurityPolicy.php‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,30 @@ public function addConnectSrc($uri, ?bool $explicitReporting = null)
521521
* @see http://www.w3.org/TR/CSP/#directive-default-src
522522
*
523523
* @param list<string>|string $uri
524+
*/
525+
public function addDefaultSrc(array|string $uri, ?bool $explicitReporting = null): static
526+
{
527+
$this->addOption($uri, 'defaultSrc', $explicitReporting ?? $this->reportOnly);
528+
529+
return $this;
530+
}
531+
532+
/**
533+
* Sets the `default-src` directive, replacing any existing sources.
534+
*
535+
* `default-src` is the URI that is used for many of the settings when
536+
* no other source has been set.
537+
*
538+
* @see http://www.w3.org/TR/CSP/#directive-default-src
539+
*
540+
* @param list<string>|string $uri
524541
*
525542
* @return $this
526543
*/
527544
public function setDefaultSrc($uri, ?bool $explicitReporting = null)
528545
{
529-
$this->defaultSrc = [(string) $uri => $explicitReporting ?? $this->reportOnly];
546+
$this->defaultSrc = [];
547+
$this->addDefaultSrc($uri, $explicitReporting);
530548

531549
return $this;
532550
}

‎tests/system/HTTP/ContentSecurityPolicyTest.php‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,50 @@ public function testDefaultSrc(): void
617617
$this->assertContains('default-src iffy.com', $this->getCspDirectives($header));
618618
}
619619

620+
#[PreserveGlobalState(false)]
621+
#[RunInSeparateProcess]
622+
public function testDefaultSrcRich(): void
623+
{
624+
$this->csp->reportOnly(false);
625+
$this->csp->setDefaultSrc(['self', 'maybe.com', 'iffy.com']);
626+
$this->assertTrue($this->work());
627+
628+
$header = $this->getHeaderEmitted('Content-Security-Policy');
629+
$this->assertIsString($header);
630+
$this->assertContains("default-src 'self' maybe.com iffy.com", $this->getCspDirectives($header));
631+
}
632+
633+
#[PreserveGlobalState(false)]
634+
#[RunInSeparateProcess]
635+
public function testAddDefaultSrc(): void
636+
{
637+
$this->csp->addDefaultSrc('maybe.com');
638+
$this->csp->addDefaultSrc('iffy.com', true);
639+
$this->assertTrue($this->work());
640+
641+
$header = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
642+
$this->assertIsString($header);
643+
$this->assertContains('default-src iffy.com', $this->getCspDirectives($header));
644+
645+
$header = $this->getHeaderEmitted('Content-Security-Policy');
646+
$this->assertIsString($header);
647+
$this->assertContains('default-src maybe.com', $this->getCspDirectives($header));
648+
}
649+
650+
#[PreserveGlobalState(false)]
651+
#[RunInSeparateProcess]
652+
public function testAddDefaultSrcAppends(): void
653+
{
654+
$this->csp->reportOnly(false);
655+
$this->csp->setDefaultSrc('maybe.com');
656+
$this->csp->addDefaultSrc('iffy.com');
657+
$this->assertTrue($this->work());
658+
659+
$header = $this->getHeaderEmitted('Content-Security-Policy');
660+
$this->assertIsString($header);
661+
$this->assertContains('default-src maybe.com iffy.com', $this->getCspDirectives($header));
662+
}
663+
620664
#[PreserveGlobalState(false)]
621665
#[RunInSeparateProcess]
622666
public function testReportURI(): void

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Message Changes
2424
Changes
2525
*******
2626

27+
- **HTTP:** Added ``ContentSecurityPolicy::addDefaultSrc()`` to allow appending sources to the ``default-src`` directive.
28+
2729
************
2830
Deprecations
2931
************
@@ -35,6 +37,7 @@ Bugs Fixed
3537
- **Commands:** Fixed a bug where generator commands did not sort the imports of a template with CRLF line endings.
3638
- **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``.
3739
- **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.
40+
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::setDefaultSrc()`` where passing an array of URIs resulted in an invalid ``'Array'`` directive.
3841
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
3942
- **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.
4043
- **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.

0 commit comments

Comments
 (0)