Skip to content

Commit 9f759b5

Browse files
committed
fix: fix array handling in ContentSecurityPolicy::setDefaultSrc() and add addDefaultSrc()
1 parent da4e508 commit 9f759b5

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎system/HTTP/ContentSecurityPolicy.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,32 @@ public function addConnectSrc($uri, ?bool $explicitReporting = null)
524524
*
525525
* @return $this
526526
*/
527-
public function setDefaultSrc($uri, ?bool $explicitReporting = null)
527+
public function addDefaultSrc($uri, ?bool $explicitReporting = null)
528528
{
529-
$this->defaultSrc = [(string) $uri => $explicitReporting ?? $this->reportOnly];
529+
$this->addOption($uri, 'defaultSrc', $explicitReporting ?? $this->reportOnly);
530530

531531
return $this;
532532
}
533533

534+
/**
535+
* Sets the `default-src` directive, replacing any existing sources.
536+
*
537+
* `default-src` is the URI that is used for many of the settings when
538+
* no other source has been set.
539+
*
540+
* @see http://www.w3.org/TR/CSP/#directive-default-src
541+
*
542+
* @param list<string>|string $uri
543+
*
544+
* @return $this
545+
*/
546+
public function setDefaultSrc($uri, ?bool $explicitReporting = null)
547+
{
548+
$this->defaultSrc = [];
549+
550+
return $this->addDefaultSrc($uri, $explicitReporting);
551+
}
552+
534553
/**
535554
* Adds a new value to the `font-src` directive.
536555
*

‎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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Bugs Fixed
3838
is used instead. Values that cannot be converted to a string, such as objects
3939
not implementing ``Stringable``, now throw a ``FormatException`` instead of a
4040
fatal ``Error``.
41+
- **HTTP:** Fixed a bug in ``ContentSecurityPolicy::setDefaultSrc()`` where passing an array of URIs resulted in an invalid ``'Array'`` directive. Added ``ContentSecurityPolicy::addDefaultSrc()`` to allow appending sources to the ``default-src`` directive.
4142
- **Security:** Fixed a bug where a valid JSON request body that is not an object
4243
(for example a JSON array) was corrupted after CSRF verification when the
4344
token was sent via the ``X-CSRF-TOKEN`` header.

0 commit comments

Comments
 (0)