Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
PanelFraming
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 process
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Middleware;
6
7use Override;
8use Psr\Http\Message\ResponseInterface as Response;
9use Psr\Http\Message\ServerRequestInterface as Request;
10use Psr\Http\Server\MiddlewareInterface as Middleware;
11use Psr\Http\Server\RequestHandlerInterface as Handler;
12
13/**
14 * Only the panel itself and the origins in `panel.embed` may frame panel
15 * pages. Every panel response says so, because an embedded editor frames
16 * further panel pages and the policy applies to each of their ancestors.
17 */
18final class PanelFraming implements Middleware
19{
20    /** @param list<string> $origins */
21    public function __construct(
22        private readonly array $origins,
23    ) {}
24
25    #[Override]
26    public function process(Request $request, Handler $handler): Response
27    {
28        return $handler
29            ->handle($request)
30            ->withHeader('Content-Security-Policy', 'frame-ancestors ' . implode(' ', ["'self'", ...$this->origins]));
31    }
32}