2018-08-25 00:55:32 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SecureHeaders.php
|
2020-01-31 00:32:04 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-08-25 00:55:32 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-08-25 00:55:32 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2018-08-25 00:55:32 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2018-08-25 00:55:32 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-08-25 00:55:32 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-08-25 00:55:32 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
2020-03-17 09:02:57 -05:00
|
|
|
use Exception;
|
2018-08-25 00:55:32 -05:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Class SecureHeaders
|
|
|
|
*/
|
|
|
|
class SecureHeaders
|
|
|
|
{
|
|
|
|
/**
|
2020-01-04 04:00:44 -06:00
|
|
|
* Handle an incoming request.
|
2018-08-25 00:55:32 -05:00
|
|
|
*
|
2023-06-21 05:34:58 -05:00
|
|
|
* @param Request $request
|
|
|
|
* @param Closure $next
|
2018-08-25 00:55:32 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
2021-03-21 03:15:40 -05:00
|
|
|
* @throws Exception
|
2018-08-25 00:55:32 -05:00
|
|
|
*/
|
|
|
|
public function handle(Request $request, Closure $next)
|
|
|
|
{
|
2020-01-10 22:28:20 -06:00
|
|
|
// generate and share nonce.
|
2020-01-04 04:00:44 -06:00
|
|
|
$nonce = base64_encode(random_bytes(16));
|
|
|
|
app('view')->share('JS_NONCE', $nonce);
|
|
|
|
|
2021-03-28 04:46:23 -05:00
|
|
|
$response = $next($request);
|
2020-01-16 21:30:44 -06:00
|
|
|
$trackingScriptSrc = $this->getTrackingScriptSource();
|
2021-03-28 04:46:23 -05:00
|
|
|
$csp = [
|
2021-04-08 10:41:19 -05:00
|
|
|
"default-src 'none'",
|
|
|
|
"object-src 'none'",
|
2022-01-28 14:19:05 -06:00
|
|
|
sprintf("script-src 'unsafe-eval' 'strict-dynamic' 'self' 'unsafe-inline' 'nonce-%1s' %2s", $nonce, $trackingScriptSrc),
|
2021-04-08 10:41:19 -05:00
|
|
|
"style-src 'unsafe-inline' 'self'",
|
|
|
|
"base-uri 'self'",
|
|
|
|
"font-src 'self' data:",
|
2022-01-28 14:19:05 -06:00
|
|
|
sprintf("connect-src 'self' %s", $trackingScriptSrc),
|
2021-04-08 10:41:19 -05:00
|
|
|
sprintf("img-src data: 'strict-dynamic' 'self' *.tile.openstreetmap.org %s", $trackingScriptSrc),
|
|
|
|
"manifest-src 'self'",
|
2018-08-25 03:36:27 -05:00
|
|
|
];
|
2019-01-27 11:24:11 -06:00
|
|
|
|
2022-02-09 04:14:00 -06:00
|
|
|
$route = $request->route();
|
|
|
|
$customUrl = '';
|
2022-12-29 12:42:26 -06:00
|
|
|
$authGuard = (string)config('firefly.authentication_guard');
|
|
|
|
$logoutUrl = (string)config('firefly.custom_logout_url');
|
2022-02-09 04:14:00 -06:00
|
|
|
if ('remote_user_guard' === $authGuard && '' !== $logoutUrl) {
|
|
|
|
$customUrl = $logoutUrl;
|
|
|
|
}
|
|
|
|
|
2019-02-13 10:38:41 -06:00
|
|
|
if (null !== $route && 'oauth/authorize' !== $route->uri) {
|
2022-02-09 04:14:00 -06:00
|
|
|
$csp[] = sprintf("form-action 'self' %s", $customUrl);
|
2019-01-27 10:15:40 -06:00
|
|
|
}
|
2018-08-25 00:55:32 -05:00
|
|
|
|
2018-08-25 03:49:52 -05:00
|
|
|
$featurePolicies = [
|
|
|
|
"geolocation 'none'",
|
|
|
|
"midi 'none'",
|
2018-08-27 11:59:30 -05:00
|
|
|
//"notifications 'none'",
|
|
|
|
//"push 'self'",
|
2018-08-25 03:49:52 -05:00
|
|
|
"sync-xhr 'self'",
|
|
|
|
"microphone 'none'",
|
|
|
|
"camera 'none'",
|
|
|
|
"magnetometer 'none'",
|
|
|
|
"gyroscope 'none'",
|
2021-04-08 10:41:19 -05:00
|
|
|
//"speaker 'none'",
|
2018-08-27 11:59:30 -05:00
|
|
|
//"vibrate 'none'",
|
2018-08-25 03:49:52 -05:00
|
|
|
"fullscreen 'self'",
|
|
|
|
"payment 'none'",
|
|
|
|
];
|
|
|
|
|
2018-12-15 00:59:02 -06:00
|
|
|
$disableFrameHeader = config('firefly.disable_frame_header');
|
2020-01-10 22:28:20 -06:00
|
|
|
$disableCSP = config('firefly.disable_csp_header');
|
|
|
|
if (false === $disableFrameHeader) {
|
2018-11-24 00:24:32 -06:00
|
|
|
$response->header('X-Frame-Options', 'deny');
|
|
|
|
}
|
2020-01-10 22:28:20 -06:00
|
|
|
if (false === $disableCSP && !$response->headers->has('Content-Security-Policy')) {
|
2019-07-16 12:21:58 -05:00
|
|
|
$response->header('Content-Security-Policy', implode('; ', $csp));
|
|
|
|
}
|
2018-08-25 03:49:52 -05:00
|
|
|
$response->header('X-XSS-Protection', '1; mode=block');
|
|
|
|
$response->header('X-Content-Type-Options', 'nosniff');
|
|
|
|
$response->header('Referrer-Policy', 'no-referrer');
|
2021-04-08 05:27:54 -05:00
|
|
|
$response->header('X-Permitted-Cross-Domain-Policies', 'none');
|
|
|
|
$response->header('X-Robots-Tag', 'none');
|
2018-08-25 03:49:52 -05:00
|
|
|
$response->header('Feature-Policy', implode('; ', $featurePolicies));
|
2018-08-25 00:55:32 -05:00
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2020-01-10 22:28:20 -06:00
|
|
|
|
2022-01-18 12:16:12 -06:00
|
|
|
/**
|
|
|
|
* Return part of a CSP header allowing scripts from Matomo.
|
2020-01-10 22:28:20 -06:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-01-16 21:30:44 -06:00
|
|
|
private function getTrackingScriptSource(): string
|
2020-01-10 22:28:20 -06:00
|
|
|
{
|
2022-12-29 12:42:26 -06:00
|
|
|
if ('' !== (string)config('firefly.tracker_site_id') && '' !== (string)config('firefly.tracker_url')) {
|
|
|
|
return (string)config('firefly.tracker_url');
|
2020-01-10 22:28:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
2018-09-03 00:19:38 -05:00
|
|
|
}
|