-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from gsteel/x-frame-options
Add X-Frame-Options: SAMEORIGIN to all response payloads.
- Loading branch information
Showing
4 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
final readonly class XFrameOptionsMiddleware implements MiddlewareInterface | ||
{ | ||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
return $handler->handle($request)->withHeader('X-Frame-Options', 'SAMEORIGIN'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AppTest\Unit\Middleware; | ||
|
||
use App\Middleware\XFrameOptionsMiddleware; | ||
use AppTest\Unit\Framework\TestCase; | ||
use AppTest\Unit\Framework\TestHandler; | ||
use Laminas\Diactoros\Response\HtmlResponse; | ||
|
||
class XFrameOptionsMiddlewareTest extends TestCase | ||
{ | ||
private XFrameOptionsMiddleware $middleware; | ||
private TestHandler $handler; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->middleware = new XFrameOptionsMiddleware(); | ||
$this->handler = new TestHandler(new HtmlResponse('Foo')); | ||
} | ||
|
||
public function testThatTheResponseWillHaveTheXFrameOptionsHeaderAppended(): void | ||
{ | ||
$response = $this->middleware->process($this->serverRequest('/'), $this->handler); | ||
|
||
self::assertTrue($this->handler->didHandle()); | ||
self::assertMessageHasHeader($response, 'X-Frame-Options', 'SAMEORIGIN'); | ||
} | ||
} |