内容安全策略包含无效的源



我正在尝试在我的网站上使用内容安全策略,我已经做得很好了,一切都正常,但这个错误仍然出现在控制台中

内容安全策略指令"default src"的源列表包含无效的源:"data:frame src"。它将被忽略。

我哪里出错了?这里怎么了?

<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
class ContentSecurityPolicy
{
public $resources = [
'default-src' => [
"'self'",
"'unsafe-inline'",
'cdn.jsdelivr.net',
'*.googletagmanager.com',
'fonts.googleapis.com',
'cdnjs.cloudflare.com',
'fonts.gstatic.com',
'code.jquery.com',
],
'img-src' => [
"data:",
],
'frame-src' => [
'youtube.com www.youtube.com',
],
];
public function handle(Request $request, Closure $next)
{
$response = $next($request);
$contentSecurityPolicy = '';
foreach ($this->resources as $key => $values) {
$contentSecurityPolicy .= $key . ' ' . implode(' ', $values);
}
$response->header("Content-Security-Policy", $contentSecurityPolicy);
return $response;
}
}

还有另一个错误

拒绝加载图像'https://ssl.gstatic.com/ui/v1/icons/mail/images/2/openhand.cur',因为它违反了以下内容安全策略指令:";默认src"self"unsafe inline"cdn.jsdelivr.net*.googletagmanager.com fonts.googleapis.com cdnjs.cloudflare.com fonts.gstatic.com code.jquery.comimg-src数据:frame src youtube.com www.youtube.com"。请注意,没有显式设置"img src",因此使用"default src"作为回退。

  1. 查看您生成的CSP:

默认src"self"unsafe-inline"cdn.jsdelivr.net*.googletagmanager.com fonts.googleapis.com cdnjs.cloudflare.com fonts.gstatic.com code.jquery。comimg src数据:frame srcyoutube.com www.youtube.com

但通常应该是:

默认src"self"unsafe-inline"cdn.jsdelivr.net*.googletagmanager.com fonts.googleapis.com cdnjs.cloudflare.com fonts.gstatic.com code.jquery.com;img src数据:;框架srcyoutube.com www.youtube.com

指令之间缺少分号;。更改线路:

$contentSecurityPolicy .= $key . ' ' . implode(' ', $values);

至:

$contentSecurityPolicy .= $key . ' ' . implode(' ', $values) .';';
  1. 'img-src' => [ "data:", ],表示您不从自己的站点加载图像。这是一种极为罕见的情况,因此'img-src' => [ "'self'", "data:", ],要好得多。

  2. 使用default-src作为隐式初始化其他指令的源是一种糟糕的做法。这可以用于简单的情况,但不能用于您的情况——稍后您将希望在script-src指令中去掉'unsafe-inline',因为它的使用不能防止XSS
    因此,您必须对style-src使用'unsafe-inline',对script-src使用'nonce-value',但未能在default-src中混合这些令牌
    default-src中使用'nonce-value'也会导致Firefox中存在漏洞。

根据指令分配源,这将使您在未来不再头疼。

相关内容

最新更新