add Secure Header + add nonce on all script balise + update ViewServiceProvider
This commit is contained in:
parent
214ff79085
commit
8ac9932cc5
12 changed files with 100 additions and 21 deletions
|
|
@ -36,6 +36,7 @@ class Kernel extends HttpKernel
|
|||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
\App\Http\Middleware\SecureHeaders::class
|
||||
],
|
||||
|
||||
'api' => [
|
||||
|
|
|
|||
79
app/Http/Middleware/SecureHeaders.php
Normal file
79
app/Http/Middleware/SecureHeaders.php
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Illuminate\Support\Facades\View;
|
||||
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
|
||||
View::share('nonce', $nonce = \uniqid());
|
||||
// * For VueJS dev tool to execute
|
||||
$jsDev = config('app.debug') ? "'unsafe-eval' 'unsafe-inline'" : "'nonce-{$nonce}' 'strict-dynamic'";
|
||||
$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 = [
|
||||
"manifest-src 'self';",
|
||||
"connect-src 'self' blob:;",
|
||||
"img-src * data: blob:;",
|
||||
"font-src 'self' data: https://fonts.gstatic.com;",
|
||||
"style-src 'self' 'unsafe-inline';",
|
||||
"script-src 'self' {$jsDev} " . \implode(' ', [
|
||||
'https: blob:',
|
||||
]) . ';'
|
||||
];
|
||||
|
||||
$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)
|
||||
{
|
||||
foreach ($headerList as $header) {
|
||||
header_remove($header);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,11 +7,8 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
|||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
/**
|
||||
|
|
@ -58,12 +55,5 @@ class AppServiceProvider extends ServiceProvider
|
|||
if (!app()->runningInConsole()) {
|
||||
Paginator::useBootstrap();
|
||||
}
|
||||
|
||||
$appInfos = Cache::remember('composer', 360, function () {
|
||||
return json_decode(File::get(\app_path('../composer.json')));
|
||||
});
|
||||
View::share('name', $appInfos->name);
|
||||
View::share('version', $appInfos->version);
|
||||
View::share('license', $appInfos->license);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@
|
|||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ViewServiceProvider extends ServiceProvider
|
||||
{
|
||||
|
|
@ -15,9 +17,16 @@ class ViewServiceProvider extends ServiceProvider
|
|||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
// Set default view of the paginator.
|
||||
// * Set default view of the paginator.
|
||||
Paginator::defaultView('modules.pagination');
|
||||
|
||||
View::share('nonce', $nonce = \uniqid());
|
||||
// * Shares this data with all views.
|
||||
// Get data from composer.json.
|
||||
$appInfos = Cache::remember('composer', 360, function () {
|
||||
return json_decode(File::get(\app_path('../composer.json')));
|
||||
});
|
||||
View::share('name', $appInfos->name);
|
||||
View::share('version', $appInfos->version);
|
||||
View::share('license', $appInfos->license);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@
|
|||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Submit button clone.
|
||||
const submit = document.getElementById('formSubmit')
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@
|
|||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Submit button clone.
|
||||
const submit = document.getElementById('formSubmit')
|
||||
|
|
|
|||
|
|
@ -35,5 +35,5 @@
|
|||
@endpush
|
||||
|
||||
@if(config('app.debug'))
|
||||
<script nonce="{{ $nonce }}">window.vueDebug = true;</script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>window.vueDebug = true;</script>
|
||||
@endif
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
<i class="fas fa-backspace"></i>
|
||||
</button>
|
||||
@push('scripts')
|
||||
<script nonce="{{ $nonce }}">
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
document.getElementById('searchDelete').addEventListener('click', function (e) {
|
||||
document.getElementById('searchField').value = '';
|
||||
document.getElementById('searchForm').submit();
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@
|
|||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Submit button clone.
|
||||
const submit = document.getElementById('formSubmit')
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@
|
|||
</div>
|
||||
|
||||
@push('scripts')
|
||||
<script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
// Submit button clone.
|
||||
const submit = document.getElementById('formSubmit')
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@
|
|||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
<script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
Date.prototype.getFromFormat = function(format) {
|
||||
var yyyy = this.getFullYear().toString();
|
||||
format = format.replace(/yyyy/g, yyyy)
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@
|
|||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
<script>
|
||||
<script @if(isset($nonce)) nonce="{{ $nonce }}" @endif>
|
||||
Date.prototype.getFromFormat = function(format) {
|
||||
var yyyy = this.getFullYear().toString();
|
||||
format = format.replace(/yyyy/g, yyyy)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue