Files
Toiminnanohjaus/app/Modules/Shared/Presentation/Http/Middleware/SetSecurityHeaders.php
T
petriandClaude Opus 5.5 1acdaa5b19 Harden the app: block project files, add rate limits and security headers
- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 06:25:26 +02:00

46 lines
1.5 KiB
PHP

<?php
namespace App\Modules\Shared\Presentation\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Browser security headers on every response of the application.
*
* - No framing by other sites (clickjacking).
* - No MIME sniffing.
* - Only the origin goes to other sites in Referer, so set-password tokens
* in the URL never leave the application.
* - HSTS when the request came over HTTPS.
*
* A full Content-Security-Policy is left out: Alpine and Livewire need
* 'unsafe-eval', which would take away most of its protection.
*/
class SetSecurityHeaders
{
public const int HSTS_MAX_AGE = 31536000;
public function handle(Request $request, Closure $next): Response
{
$response = $next($request);
$response->headers->set('X-Frame-Options', 'DENY');
$response->headers->set('Content-Security-Policy', "frame-ancestors 'none'; base-uri 'self'; object-src 'none'");
$response->headers->set('X-Content-Type-Options', 'nosniff');
$response->headers->set('Referrer-Policy', 'strict-origin-when-cross-origin');
$response->headers->set('Permissions-Policy', 'geolocation=(), microphone=(), payment=(), usb=()');
if ($request->isSecure()) {
$response->headers->set('Strict-Transport-Security', 'max-age='.self::HSTS_MAX_AGE.'; includeSubDomains');
}
if (! headers_sent()) {
header_remove('X-Powered-By');
}
return $response;
}
}