2023-10-17 14:58:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Route;
|
|
|
|
|
use Illuminate\Support\Facades\View;
|
|
|
|
|
use Illuminate\Support\Facades\Vite;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
|
|
|
|
|
|
class SecureHeaders
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Enumerate headers which you do not want in your application's responses.
|
|
|
|
|
* Great starting point would be to go check out @Scott_Helme's:
|
|
|
|
|
* https://securityheaders.com/
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
private $unwantedHeaderList = [
|
|
|
|
|
'X-Powered-By',
|
|
|
|
|
'Server',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle an incoming request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
|
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
|
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
|
|
|
|
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
|
|
|
|
|
*/
|
|
|
|
|
public function handle(Request $request, Closure $next): Response
|
|
|
|
|
{
|
|
|
|
|
// phpcs:enable
|
2023-10-27 14:22:03 +02:00
|
|
|
// * For Vite DEV
|
2023-10-20 15:01:02 +02:00
|
|
|
$host = \parse_url(\config('app.url'), \PHP_URL_HOST);
|
2023-10-17 14:58:21 +02:00
|
|
|
View::share('nonce', $nonce = Vite::useCspNonce());
|
|
|
|
|
// * For VueJS dev tool to execute
|
|
|
|
|
$jsDev = config('app.debug') ? "'unsafe-eval' 'unsafe-inline'" : "'nonce-{$nonce}' 'strict-dynamic'";
|
|
|
|
|
$scheme = config('app.env') === 'local' ? 'http' : 'https';
|
|
|
|
|
$reportUri = Route::has('cspReportUri') ?
|
|
|
|
|
\sprintf(' report-uri %s ;', \parse_url(route('cspReportUri'), \PHP_URL_PATH)) : '';
|
|
|
|
|
$this->removeUnwantedHeaders($this->unwantedHeaderList);
|
|
|
|
|
$response = $next($request);
|
|
|
|
|
$response->headers->set('Referrer-Policy', 'no-referrer-when-downgrade');
|
|
|
|
|
$response->headers->set('X-Content-Type-Options', 'nosniff');
|
|
|
|
|
$response->headers->set('X-XSS-Protection', '1; mode=block');
|
|
|
|
|
$response->headers->set('X-Frame-Options', 'SAMEORIGIN');
|
|
|
|
|
$response->headers->set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains;');
|
|
|
|
|
$cspPolicies = [
|
|
|
|
|
"default-src 'none';",
|
|
|
|
|
"manifest-src 'self';",
|
|
|
|
|
// * CKEDITOR Specific https://ckeditor.com/docs/ckeditor5/latest/installation/advanced/csp.html
|
2023-10-20 15:01:02 +02:00
|
|
|
"connect-src 'self' " . implode(' ', [
|
|
|
|
|
// * VITE DEV
|
2023-10-27 14:22:03 +02:00
|
|
|
config('app.debug') ? "wss://{$host}:4443" : '',
|
|
|
|
|
config('app.debug') ? "https://{$host}:4443" : '',
|
2023-10-24 16:11:24 +02:00
|
|
|
'https://yl.s-web.fr',
|
2023-10-20 15:01:02 +02:00
|
|
|
]) . ';',
|
2023-10-24 16:11:24 +02:00
|
|
|
"img-src * https://yl.s-web.fr data: blob:;",
|
2023-10-17 14:58:21 +02:00
|
|
|
'frame-src ' . implode(' ', [
|
|
|
|
|
'https://www.google.com/recaptcha/',
|
|
|
|
|
'https://recaptcha.google.com/recaptcha/',
|
|
|
|
|
'https://www.google.com/maps/'
|
|
|
|
|
]) . ';',
|
2023-10-23 10:09:25 +02:00
|
|
|
"font-src 'self' data: " . \implode(' ', [
|
|
|
|
|
// * VITE DEV
|
2023-10-27 14:22:03 +02:00
|
|
|
config('app.debug') ? "https://{$host}:4443" : '',
|
2023-10-23 10:09:25 +02:00
|
|
|
'https://fonts.gstatic.com;'
|
|
|
|
|
]) . ';',
|
2023-10-17 14:58:21 +02:00
|
|
|
// ! Using CKEditor will must have unsafe-inline style
|
|
|
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
|
|
|
"style-src 'self' 'unsafe-inline' " . \implode(' ', [
|
|
|
|
|
'https://cdnjs.cloudflare.com',
|
|
|
|
|
// * File Manager
|
|
|
|
|
'https://fonts.googleapis.com',
|
|
|
|
|
'https://cdn.jsdelivr.net',
|
|
|
|
|
'https://use.fontawesome.com',
|
2023-10-27 14:22:03 +02:00
|
|
|
'https://fonts.googleapis.com',
|
|
|
|
|
// * VITE DEV
|
|
|
|
|
config('app.debug') ? "https://{$host}:4443" : '',
|
2023-10-17 14:58:21 +02:00
|
|
|
]) . ';',
|
|
|
|
|
// ! If you dont use ckeditor use this
|
|
|
|
|
// "style-src 'self' 'nonce-$nonce';",
|
|
|
|
|
// phpcs:ignore Generic.Files.LineLength.TooLong
|
|
|
|
|
"script-src 'self' {$jsDev} " . \implode(' ', [
|
|
|
|
|
'https: blob:',
|
|
|
|
|
'https://www.googletagmanager.com',
|
|
|
|
|
'https://cdnjs.cloudflare.com',
|
|
|
|
|
"$scheme://www.google-analytics.com",
|
2023-10-27 15:42:59 +02:00
|
|
|
'https://www.google.com/recaptcha/',
|
|
|
|
|
'https://www.gstatic.com/recaptcha/',
|
2023-10-27 14:22:03 +02:00
|
|
|
// * VITE DEV
|
|
|
|
|
config('app.debug') ? "https://{$host}:4443" : '',
|
2023-10-17 14:58:21 +02:00
|
|
|
]) . ';'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$response->headers->set(
|
|
|
|
|
'Content-Security-Policy',
|
|
|
|
|
implode(' ', $cspPolicies) . $reportUri
|
|
|
|
|
);
|
|
|
|
|
$response->headers->set('Feature-Policy', 'fullscreen \'self\';');
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove unwanted headers
|
|
|
|
|
*
|
|
|
|
|
* @param array $headerList
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function removeUnwantedHeaders(array $headerList): void
|
|
|
|
|
{
|
|
|
|
|
foreach ($headerList as $header) {
|
|
|
|
|
header_remove($header);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|