diff --git a/src/CSPBuilder.php b/src/CSPBuilder.php index 18aa112..b8821a8 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 9f0ae31..8319db9 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 */