如何修复CSP问题

你好!我的 CSP 遇到了问题,该如何解决?

screen

拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src https://mydomain.com/logs/ https://mydomain.com/sidekiq/ https://mydomain.com/mini-profiler-resources/ https://mydomain.com/assets/ https://mydomain.com/brotli_asset/ https://mydomain.com/extra-locales/ https://mydomain.com/highlight-js/ https://mydomain.com/javascripts/ https://mydomain.com/plugins/ https://mydomain.com/theme-javascripts/ https://mydomain.com/svg-sprite/ https://mydomain.com”。需要 'unsafe-inline' 关键字、哈希值('sha256-ScCk7JwXKy22dTO0VFM7aV1chV+yd/MUda4X6VnGans=')或随机数('nonce-...')才能启用内联执行。

为了进行测试,我进入了“安全模式”,在安全模式下存在相同的问题。

1 个赞

这不是我的强项,但我搜索了一下,你可能需要重写你正在尝试使用的内联脚本:

2 个赞

我不想禁用 CSP 保护,也没有外部脚本,我尝试禁用所有插件和主题组件,但没有帮助。

我尝试将所有这些链接添加到管理员设置中的 content security policy script src,问题就解决了 :frowning:

https://mydomain.com/logs/ https://mydomain.com/sidekiq/ https://mydomain.com/mini-profiler-resources/ https://mydomain.com/assets/ https://mydomain.com/brotli_asset/ https://mydomain.com/extra-locales/ https://mydomain.com/highlight-js/ https://mydomain.com/javascripts/ https://mydomain.com/plugins/ https://mydomain.com/theme-javascripts/ https://mydomain.com/svg-sprite/ https://mydomain.com`

1 个赞

您需要分享您的网站网址,以便我们能够帮助您。

3 个赞

我可以把它发到你的私人消息吗?

嗯……
看来我找到了原因,问题似乎是由于我在主题中使用了 CSP 元标记:

<meta http-equiv="Content-Security-Policy" content="img-src https://imgur.com https://giphy.com">

我需要使用 img-src 指令来限制显示来自禁止主机的图片。

那个 CSP 指令确实会彻底破坏 Discourse。

2 个赞

如何在不使用 meta 标签的情况下更改标头中的默认 CSP 指令,以添加 img-src?我在 Rails 中尝试过,但没有任何更改:

Rails.application.config.action_dispatch.default_headers.merge!({'Content-Security-Policy' => "upgrade-insecure-requests; base-uri 'self'; object-src 'none'; script-src https://mydomain.com/logs/ https://mydomain.com/sidekiq/ https://mydomain.com/mini-profiler-resources/ https://mydomain.com/assets/ https://mydomain.com/brotli_asset/ https://mydomain.com/extra-locales/ https://mydomain.com/highlight-js/ https://mydomain.com/javascripts/ https://mydomain.com/plugins/ https://mydomain.com/theme-javascripts/ https://mydomain.com/svg-sprite/; worker-src 'self' https://mydomain.com/assets/ https://mydomain.com/brotli_asset/ https://mydomain.com/javascripts/ https://mydomain.com/plugins/; frame-ancestors 'self'; manifest-src 'self'; img-src data: https://mydomain.com https://imgur.com"})

我删除了这个 meta 标签,重新构建后问题就解决了 :frowning: