我在现有 Laravel 6 项目上实现样式和脚本的 CSP 时遇到问题,
我已设法遵循其中一项命名指南:
但是,使用像“self”这样的默认标头值和我的本地主机域并不能解决 SCP 问题,而且它会不断破坏我的网站,除非我添加样式
unsafe-inline
、src unsafe-inline
和 unsafe-eval
,因为有很多内联样式和脚本。
我在这里使用的代码是:
$this->addDirective(Directive::BASE, Keyword::SELF)
->addDirective(Directive::CONNECT, Keyword::SELF)
->addDirective(Directive::DEFAULT, Keyword::SELF)
->addDirective(Directive::FORM_ACTION, Keyword::SELF)
->addDirective(Directive::IMG, Keyword::SELF)
->addDirective(Directive::MEDIA, Keyword::SELF)
->addDirective(Directive::OBJECT, Keyword::NONE)
->addDirective(Directive::STYLE, [
Keyword::SELF,
'mylocaldomain.test',
//'unsafe-inline' //Not safe i know. Without this, site breaks
])
->addDirective(Directive::SCRIPT, [
Keyword::SELF,
'mylocaldomain.test',
// 'unsafe-eval', //Not safe, finding a way to fix this
])
->addNonceForDirective(Directive::SCRIPT);
// ->addNonceForDirective(Directive::STYLE);//produced more errors, commented.
但我知道不应该实现不安全的值,事实上,当我针对我的 laravel 应用程序运行 owasp zap 时,会立即报告
unsafe
值。
目前我设法让 csp_nonce() 函数通过脚本传递,现在它不再抱怨,但由于许多内联样式和其他 js 库调用 eval(),对样式执行相同操作并不成功。
尽管我这边已经包含了“nonce”、“self”和“域名”,但我的网站仍然被破坏并抱怨内联样式(不告诉哪一行)。
一些内联样式已经包含了nonce参数,但它仍然抱怨。
Content-Security-Policy: The page’s settings blocked the loading of a resource at inline (“script-src”).
我阅读了许多其他论坛并在 Stackoverflow 上提出建议,因为每当我发现(尝试和错误)时我都会实施修复
当然,使用“unsafe-inline”值是不可能的,但是以尽可能短的时间处理 eval() 和内联样式的最佳实践是什么? (我有数百页不同的刀片,不确定将它们一一寻址是否符合逻辑)
我已经在我正在加载的所有脚本上实现了随机数(没有给我提供要解决的错误的正确位置),但它仍然抱怨 eval(),并且它没有为我提供错误位置
Content-Security-Policy: The page’s settings blocked the loading of a resource at eval (“script-src”).
Source: window.__cmp&&typeof __cmp("getCMPData")… login
我还尝试删除
style-src
,假设它可能不是像脚本那样的威胁,但页面在 default-src
上产生更多错误(以前从未抱怨过)
我已经为此工作了大约两周,没有具体的线索,因为不同的开发人员有不同的问题来解决以下问题:
感谢任何关于我哪里做错了以及我该如何解决的建议,因为我现在没有选择。
来源:window.__cmp&&typeof __cmp("getCMPData")…登录
我通过禁用 Ghostery Tracker 和广告拦截器解决了这个问题