Skip to content

Commit

Permalink
Add X-Frame-Options: SAMEORIGIN to all response payloads.
Browse files Browse the repository at this point in the history
It's pretty shitty to embed someone else's work and pass it off as your own. So this is telling "Elite Customer Solutions" to fuck off.
  • Loading branch information
gsteel committed Apr 22, 2024
1 parent 8458a4a commit 6d079a4
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Middleware\XFrameOptionsMiddleware;
use Laminas\Stratigility\Middleware\ErrorHandler;
use Mezzio\Application;
use Mezzio\Handler\NotFoundHandler;
Expand All @@ -24,6 +25,7 @@
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(MethodNotAllowedMiddleware::class);
$app->pipe(UrlHelperMiddleware::class);
$app->pipe(XFrameOptionsMiddleware::class);
$app->pipe(DispatchMiddleware::class);
$app->pipe(NotFoundHandler::class);
};
1 change: 1 addition & 0 deletions src/App/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private function dependencies(): array
Log\ErrorHandlerLoggingListener::class => Log\Container\ErrorHandlerLoggingListenerFactory::class,
Middleware\CalculationMiddleware::class => Middleware\Container\CalculationMiddlewareFactory::class,
Middleware\TemplateRenderer::class => Middleware\Container\TemplateRendererFactory::class,
Middleware\XFrameOptionsMiddleware::class => Laminas\ServiceManager\Factory\InvokableFactory::class,
Money\Currency::class => static fn (): Money\Currency => new Money\Currency('GBP'),
Psr\Http\Client\ClientInterface::class => Container\HttpClientFactory::class,

Expand Down
18 changes: 18 additions & 0 deletions src/App/Middleware/XFrameOptionsMiddleware.php
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');
}
}
30 changes: 30 additions & 0 deletions test/Unit/Middleware/XFrameOptionsMiddlewareTest.php
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');
}
}

0 comments on commit 6d079a4

Please sign in to comment.