2026-02-14 11:18:17 -08:00
|
|
|
<?php
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-02-14 11:18:17 -08:00
|
|
|
namespace Pterodactyl\Http\Middleware;
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
use Closure;
|
2026-02-14 11:18:17 -08:00
|
|
|
use Illuminate\Http\Request;
|
2026-10-05 18:10:18 -04:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2026-02-14 11:18:17 -08:00
|
|
|
|
|
|
|
|
class SetSecurityHeaders
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Ideally we move away from X-Frame-Options/X-XSS-Protection and implement a
|
|
|
|
|
* proper standard CSP, but I can guarantee that will break for a lot of folks
|
|
|
|
|
* using custom plugins and who knows what image embeds.
|
|
|
|
|
*
|
|
|
|
|
* We'll circle back to that at a later date when it can be more fully controlled
|
|
|
|
|
* by the admin to support those cases without too much trouble.
|
2026-10-05 18:10:18 -04:00
|
|
|
*
|
|
|
|
|
* @var array<string, string>
|
2026-02-14 11:18:17 -08:00
|
|
|
*/
|
|
|
|
|
private static array $headers = [
|
|
|
|
|
'X-Frame-Options' => 'DENY',
|
|
|
|
|
'X-Content-Type-Options' => 'nosniff',
|
|
|
|
|
'X-XSS-Protection' => '1; mode=block',
|
|
|
|
|
'Referrer-Policy' => 'no-referrer-when-downgrade',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enforces some basic security headers on all responses returned by the software.
|
|
|
|
|
* If a header has already been set in another location within the code it will be
|
|
|
|
|
* skipped over here.
|
|
|
|
|
*
|
2026-10-05 18:10:18 -04:00
|
|
|
* @param (Closure(mixed): Response) $next
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* @param Closure(Request):Response $next
|
|
|
|
|
* @return Response
|
2026-02-14 11:18:17 -08:00
|
|
|
*/
|
2026-10-05 18:10:18 -04:00
|
|
|
public function handle(Request $request, Closure $next): mixed
|
2026-02-14 11:18:17 -08:00
|
|
|
{
|
|
|
|
|
$response = $next($request);
|
|
|
|
|
|
2026-10-05 18:10:18 -04:00
|
|
|
foreach (self::$headers as $key => $value) {
|
2026-02-14 11:18:17 -08:00
|
|
|
if (! $response->headers->has($key)) {
|
|
|
|
|
$response->headers->set($key, $value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
}
|