From cee9ac5b65e573d2572c95fabdf0f1d4e3c74850 Mon Sep 17 00:00:00 2001 From: Mark Unwin Date: Tue, 16 Sep 2025 10:21:46 +1000 Subject: [PATCH 1/3] feat: Add script-src-elem option to CSP options. Signed-off-by: Mark Unwin --- app/Config/ContentSecurityPolicy.php | 7 +++++++ system/HTTP/ContentSecurityPolicy.php | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/app/Config/ContentSecurityPolicy.php b/app/Config/ContentSecurityPolicy.php index 8096f6cff95e..b720ef54f19d 100644 --- a/app/Config/ContentSecurityPolicy.php +++ b/app/Config/ContentSecurityPolicy.php @@ -56,6 +56,13 @@ class ContentSecurityPolicy extends BaseConfig */ public $scriptSrc = 'self'; + /** + * Lists allowed scripts' URLs. + * + * @var list|string + */ + public $scriptSrcElem = 'self'; + /** * Lists allowed stylesheets' URLs. * diff --git a/system/HTTP/ContentSecurityPolicy.php b/system/HTTP/ContentSecurityPolicy.php index db1db04e0fdf..cae4fa429bb7 100644 --- a/system/HTTP/ContentSecurityPolicy.php +++ b/system/HTTP/ContentSecurityPolicy.php @@ -42,6 +42,7 @@ class ContentSecurityPolicy 'object-src' => 'objectSrc', 'plugin-types' => 'pluginTypes', 'script-src' => 'scriptSrc', + 'script-src-elem' => 'scriptSrcElem', 'style-src' => 'styleSrc', 'manifest-src' => 'manifestSrc', 'sandbox' => 'sandbox', @@ -153,6 +154,13 @@ class ContentSecurityPolicy */ protected $scriptSrc = []; + /** + * Used for security enforcement + * + * @var array|string + */ + protected $scriptSrcElem = []; + /** * The `style-src` directive restricts which styles the user may applies to the protected resource. * @@ -649,6 +657,23 @@ public function addScriptSrc($uri, ?bool $explicitReporting = null) return $this; } + /** + * Adds a new valid endpoint for javascript file sources. Can be either + * a URI class or a simple string. + * + * @see https://www.w3.org/TR/CSP/#directive-script-src-elem + * + * @param array|string $uri + * + * @return $this + */ + public function addScriptSrcElem($uri, ?bool $explicitReporting = null) + { + $this->addOption($uri, 'scriptSrcElem', $explicitReporting ?? $this->reportOnly); + + return $this; + } + /** * Adds a new value to the `style-src` directive. * From 221b20fae2a7b981f05f198dc72d55fd50a04523 Mon Sep 17 00:00:00 2001 From: Mark Unwin Date: Tue, 16 Sep 2025 10:34:26 +1000 Subject: [PATCH 2/3] fix: Add missing unit test for script-src-elem. Signed-off-by: Mark Unwin --- tests/system/HTTP/ContentSecurityPolicyTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/system/HTTP/ContentSecurityPolicyTest.php b/tests/system/HTTP/ContentSecurityPolicyTest.php index e8db11eed9a4..bf618ec3dd81 100644 --- a/tests/system/HTTP/ContentSecurityPolicyTest.php +++ b/tests/system/HTTP/ContentSecurityPolicyTest.php @@ -374,6 +374,21 @@ public function testScriptSrc(): void $this->assertContains("script-src 'self' cdn.cloudy.com", $this->getCspDirectives($header)); } + #[PreserveGlobalState(false)] + #[RunInSeparateProcess] + public function testScriptSrcElem(): void + { + $this->prepare(); + $this->csp->addScriptSrcElem('cdn.cloudy.com'); + $this->csp->addScriptSrcElem('them.com', true); + $result = $this->work(); + + $result = $this->getHeaderEmitted('Content-Security-Policy-Report-Only'); + $this->assertStringContainsString('script-src-elem them.com;', (string) $result); + $result = $this->getHeaderEmitted('Content-Security-Policy'); + $this->assertStringContainsString("script-src-elem 'self' cdn.cloudy.com;", (string) $result); + } + #[PreserveGlobalState(false)] #[RunInSeparateProcess] public function testStyleSrc(): void From 6442b90310004ea8d924db112f6c0ea55cfae21d Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Sun, 1 Feb 2026 18:20:45 +0800 Subject: [PATCH 3/3] Fixes --- app/Config/ContentSecurityPolicy.php | 2 +- system/HTTP/ContentSecurityPolicy.php | 23 +++++++++---------- .../system/HTTP/ContentSecurityPolicyTest.php | 14 ++++++----- user_guide_src/source/changelogs/v4.7.0.rst | 12 ++++++++-- 4 files changed, 30 insertions(+), 21 deletions(-) diff --git a/app/Config/ContentSecurityPolicy.php b/app/Config/ContentSecurityPolicy.php index b720ef54f19d..95022eb7ee43 100644 --- a/app/Config/ContentSecurityPolicy.php +++ b/app/Config/ContentSecurityPolicy.php @@ -57,7 +57,7 @@ class ContentSecurityPolicy extends BaseConfig public $scriptSrc = 'self'; /** - * Lists allowed scripts' URLs. + * Specifies valid sources for JavaScript