]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/ControlIframeSecurity.php
cc8034413b8ed3651c66ab7df202521c8d62253d
[bookstack] / app / Http / Middleware / ControlIframeSecurity.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use Closure;
6 use Symfony\Component\HttpFoundation\Response;
7
8 /**
9  * Sets CSP headers to restrict the hosts that BookStack can be
10  * iframed within. Also adjusts the cookie samesite options
11  * so that cookies will operate in the third-party context.
12  */
13 class ControlIframeSecurity
14 {
15     /**
16      * Handle an incoming request.
17      *
18      * @param  \Illuminate\Http\Request  $request
19      * @param  \Closure  $next
20      * @return mixed
21      */
22     public function handle($request, Closure $next)
23     {
24         $iframeHosts = collect(explode(' ', config('app.iframe_hosts', '')))->filter();
25         if ($iframeHosts->count() > 0) {
26             config()->set('session.same_site', 'none');
27         }
28
29         $iframeHosts->prepend("'self'");
30
31         $response = $next($request);
32         $cspValue = 'frame-ancestors ' . $iframeHosts->join(' ');
33         $response->headers->set('Content-Security-Policy', $cspValue);
34         return $response;
35     }
36 }