Skip to content

Commit 6442b90

Browse files
committed
Fixes
1 parent 221b20f commit 6442b90

4 files changed

Lines changed: 30 additions & 21 deletions

File tree

app/Config/ContentSecurityPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class ContentSecurityPolicy extends BaseConfig
5757
public $scriptSrc = 'self';
5858

5959
/**
60-
* Lists allowed scripts' URLs.
60+
* Specifies valid sources for JavaScript <script> elements.
6161
*
6262
* @var list<string>|string
6363
*/

system/HTTP/ContentSecurityPolicy.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ class ContentSecurityPolicy
4242
'object-src' => 'objectSrc',
4343
'plugin-types' => 'pluginTypes',
4444
'script-src' => 'scriptSrc',
45-
'script-src-elem' => 'scriptSrcElem',
4645
'style-src' => 'styleSrc',
47-
'manifest-src' => 'manifestSrc',
4846
'sandbox' => 'sandbox',
47+
'manifest-src' => 'manifestSrc',
48+
'script-src-elem' => 'scriptSrcElem',
4949
];
5050

5151
/**
@@ -154,13 +154,6 @@ class ContentSecurityPolicy
154154
*/
155155
protected $scriptSrc = [];
156156

157-
/**
158-
* Used for security enforcement
159-
*
160-
* @var array|string
161-
*/
162-
protected $scriptSrcElem = [];
163-
164157
/**
165158
* The `style-src` directive restricts which styles the user may applies to the protected resource.
166159
*
@@ -193,6 +186,13 @@ class ContentSecurityPolicy
193186
*/
194187
protected $manifestSrc = [];
195188

189+
/**
190+
* The `script-src-elem` directive applies to all script requests and script blocks.
191+
*
192+
* @var array<string, bool>|string
193+
*/
194+
protected $scriptSrcElem = [];
195+
196196
/**
197197
* Instructs user agents to rewrite URL schemes by changing HTTP to HTTPS.
198198
*
@@ -658,12 +658,11 @@ public function addScriptSrc($uri, ?bool $explicitReporting = null)
658658
}
659659

660660
/**
661-
* Adds a new valid endpoint for javascript file sources. Can be either
662-
* a URI class or a simple string.
661+
* Adds a new value to the `script-src-elem` directive.
663662
*
664663
* @see https://www.w3.org/TR/CSP/#directive-script-src-elem
665664
*
666-
* @param array|string $uri
665+
* @param list<string>|string $uri
667666
*
668667
* @return $this
669668
*/

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,15 +378,17 @@ public function testScriptSrc(): void
378378
#[RunInSeparateProcess]
379379
public function testScriptSrcElem(): void
380380
{
381-
$this->prepare();
382381
$this->csp->addScriptSrcElem('cdn.cloudy.com');
383382
$this->csp->addScriptSrcElem('them.com', true);
384-
$result = $this->work();
383+
$this->assertTrue($this->work());
385384

386-
$result = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
387-
$this->assertStringContainsString('script-src-elem them.com;', (string) $result);
388-
$result = $this->getHeaderEmitted('Content-Security-Policy');
389-
$this->assertStringContainsString("script-src-elem 'self' cdn.cloudy.com;", (string) $result);
385+
$header = $this->getHeaderEmitted('Content-Security-Policy-Report-Only');
386+
$this->assertIsString($header);
387+
$this->assertContains('script-src-elem them.com', $this->getCspDirectives($header));
388+
389+
$header = $this->getHeaderEmitted('Content-Security-Policy');
390+
$this->assertIsString($header);
391+
$this->assertContains("script-src-elem 'self' cdn.cloudy.com", $this->getCspDirectives($header));
390392
}
391393

392394
#[PreserveGlobalState(false)]

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,16 @@ Others
331331
Model
332332
=====
333333

334-
Helpers and Functions
335-
=====================
334+
HTTP
335+
====
336+
337+
Content Security Policy
338+
-----------------------
339+
340+
- Added support for the following CSP Level 3 directives:
341+
- ``script-src-elem``
342+
343+
Update your CSP configuration in **app/Config/ContentSecurityPolicy.php** to include these new directives as needed.
336344

337345
Others
338346
======

0 commit comments

Comments
 (0)