diff --git a/src/Slim/Middleware/FrameAncestorsCspHeader.php b/src/Slim/Middleware/FrameAncestorsCspHeader.php new file mode 100644 index 0000000..92ea93d --- /dev/null +++ b/src/Slim/Middleware/FrameAncestorsCspHeader.php @@ -0,0 +1,36 @@ +withAddedHeader('Content-Security-Policy', "frame-ancestors 'none';"); + + return $response; + } +} diff --git a/tests/Slim/Middleware/FrameAncestorsCspHeaderTest.php b/tests/Slim/Middleware/FrameAncestorsCspHeaderTest.php new file mode 100644 index 0000000..f796b09 --- /dev/null +++ b/tests/Slim/Middleware/FrameAncestorsCspHeaderTest.php @@ -0,0 +1,72 @@ +assertNotFalse( + strpos($response->getHeaderLine('Content-Security-Policy'), self::CSP_VALUE) + ); + } + + /** + * Create a Request object. + * Create a Response object with a `Content-Security-Policy` header that contains an initial value. + * + * Execute the middleware and get the returned Response object. + * + * Confirm that the Response object has: + * + * - A `Content-Security-Policy` header whose value contains both the initial value and "frame-ancestors 'none'". + */ + public function testWithOriginRequestHeaderAndExistingCspResponseHeader() + { + $initialValue = "default-src 'none';"; + + $middleware = new FrameAncestorsCspHeader(); + + $response = new Response(); + $response = $middleware( + Request::createFromEnvironmentBuilder(EnvironmentBuilder::create()), + $response->withHeader('Content-Security-Policy', $initialValue), + new EmptyWare() + ); + + $this->assertNotFalse(strpos($response->getHeaderLine('Content-Security-Policy'), $initialValue)); + $this->assertNotFalse( + strpos($response->getHeaderLine('Content-Security-Policy'), self::CSP_VALUE) + ); + } +}