Untuk memeriksa apakah security header telah diterapkan dengan benar di aplikasi Laravel kamu, kamu bisa menggunakan beberapa alat dan layanan online yang tersedia. Berikut adalah beberapa rekomendasi alat yang dapat kamu gunakan:Security Headers (Scott Helme)
curl
di terminal untuk memeriksa header yang diterapkan. Contoh perintah:curl -I https://example.com
F12
atau klik kanan dan pilih "Inspect". Lalu pergi ke tab "Network", reload halaman dan pilih permintaan utama (biasanya GET
). Di sana kamu akan melihat semua header respons di tab "Headers".php artisan make:middleware SecurityHeaders
app/Http/Middleware/SecurityHeaders.php
dan tambahkan header keamanan di metode handle
:<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SecurityHeaders
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$response->headers->set('X-Frame-Options', 'SAMEORIGIN');
$response->headers->set('X-XSS-Protection', '1; mode=block');
$response->headers->set('X-Content-Type-Options', 'nosniff');
$response->headers->set('Referrer-Policy', 'no-referrer-when-downgrade');
$response->headers->set('Feature-Policy', "geolocation 'self'; microphone 'none'");
$response->headers->set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains; preload');
$response->headers->set('Expect-CT', 'max-age=0, enforce, report-uri="https://example.com/report"');
$response->headers->set('Content-Security-Policy', "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; frame-ancestors 'self'");
return $response;
}
}
app/Http/Kernel.php
. Tambahkan \App\Http\Middleware\SecurityHeaders::class
ke array middleware.protected $middleware = [
\App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\SecurityHeaders::class, // Tambahkan baris ini
];