PHP code example of jeffersongoncalves / laravel-security-headers
1. Go to this page and download the library: Download jeffersongoncalves/laravel-security-headers library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jeffersongoncalves / laravel-security-headers example snippets
Route::middleware('security-headers')->group(function () {
// ...
});
use Illuminate\Foundation\Configuration\Middleware;
use JeffersonGoncalves\SecurityHeaders\Middleware\SecurityHeaders;
->withMiddleware(function (Middleware $middleware) {
$middleware->web(prepend: [
SecurityHeaders::class,
]);
})
protected $middlewareGroups = [
'web' => [
\JeffersonGoncalves\SecurityHeaders\Middleware\SecurityHeaders::class,
// ...
],
];
'headers' => [
'X-Content-Type-Options' => 'nosniff',
'X-Frame-Options' => 'SAMEORIGIN',
'Referrer-Policy' => 'strict-origin-when-cross-origin',
'Permissions-Policy' => 'camera=(), microphone=(), geolocation=(), payment=(), usb=(), browsing-topics=()',
'Cross-Origin-Opener-Policy' => 'same-origin-allow-popups',
// Disable a header by setting it to null:
'X-Permitted-Cross-Domain-Policies' => null,
],
'csp' => [
'enabled' => true,
'directives' => [
'default-src' => "'self'",
'script-src' => "'self'",
'style-src' => "'self'",
'img-src' => "'self' data:",
'object-src' => "'none'",
'base-uri' => "'self'",
'form-action' => "'self'",
'frame-ancestors' => "'self'",
],
],
'script-src' => "'self' 'nonce-{nonce}'",
'csp' => [
'enabled' => true,
'report-only' => true,
'report-uri' => 'https://example.com/csp-report', // legacy endpoint
'report-to' => 'csp-endpoint', // Reporting-API group name
// ...
],
'directives' => [
'default-src' => "'self'",
'script-src' => "'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://www.google-analytics.com https://static.cloudflareinsights.com",
'style-src' => "'self' 'unsafe-inline'",
'img-src' => "'self' data: https:",
'font-src' => "'self' data:",
'connect-src' => "'self' https://www.google-analytics.com https://*.google-analytics.com https://*.analytics.google.com https://www.googletagmanager.com https://cloudflareinsights.com",
'frame-src' => "'self' https://www.googletagmanager.com",
'frame-ancestors' => "'self'",
'base-uri' => "'self'",
'form-action' => "'self'",
'object-src' => "'none'",
'upgrade-insecure-requests' => null,
],
'hsts' => [
'enabled' => true,
'max-age' => 31536000,
'r stamped (even over HTTPS).
// Set to [] to stamp HSTS in every environment.
'exclude_environments' => ['local'],
],
bash
php artisan vendor:publish --tag="security-headers-config"