diff --git a/src/CSPBuilder.php b/src/CSPBuilder.php index e5b6213..591aa3e 100644 --- a/src/CSPBuilder.php +++ b/src/CSPBuilder.php @@ -149,7 +149,7 @@ public function compile(): string $compiled []= 'upgrade-insecure-requests'; } - $this->compiled = implode('', $compiled); + $this->compiled = rtrim(implode('', $compiled), '; '); $this->needsCompile = false; return $this->compiled; } diff --git a/test/BasicTest.php b/test/BasicTest.php index 6a62790..20f1797 100644 --- a/test/BasicTest.php +++ b/test/BasicTest.php @@ -70,6 +70,23 @@ public function testBasicFromData() ); } + /** + * @throws \Exception + */ + public function testNoTrailingSemicolon() + { + $csp = (new CSPBuilder()) + ->setSelfAllowed('default-src', true) + ->addSource('img-src', 'ytimg.com') + ->disableOldBrowserSupport() + ; + + $this->assertEquals( + "default-src 'self'; img-src ytimg.com", + $csp->getCompiledHeader() + ); + } + /** * @throws \Exception */