Skip to content

Commit 5f6b810

Browse files
committed
fix: make setDefaultSrc() add directives instead of overwriting
1 parent 8e2db88 commit 5f6b810

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎system/HTTP/ContentSecurityPolicy.php‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,30 @@ 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+
* Adds a new value to the `default-src` directive.
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+
return $this->addDefaultSrc($uri, $explicitReporting);
549+
}
550+
534551
/**
535552
* Adds a new value to the `font-src` directive.
536553
*

‎tests/system/HTTP/ContentSecurityPolicyTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,37 @@ public function testDefaultSrc(): void
614614

615615
$header = $this->getHeaderEmitted('Content-Security-Policy');
616616
$this->assertIsString($header);
617+
$this->assertContains('default-src maybe.com iffy.com', $this->getCspDirectives($header));
618+
}
619+
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);
617643
$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));
618648
}
619649

620650
#[PreserveGlobalState(false)]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bugs Fixed
3131
**********
3232

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

0 commit comments

Comments
 (0)