![](/img/trans.png)
[英]Understanding connect-src, script-src and style-src when everything is loaded dynamically
[英]nonce for style-src in Rails is added to script-src by mistake
我在 Rails 应用程序中有一个内联样式标签,我正在尝试使用 nonce 将其列入白名单。
错误: Content Security Policy: The page's settings observed the loading of a resource at inline (“style-src”). A CSP report is being sent.
Content Security Policy: The page's settings observed the loading of a resource at inline (“style-src”). A CSP report is being sent.
使用invisible_captcha gem 时,样式由助手注入:
<%= invisible_captcha nonce: true %>
比较视图和 header 显示随机数被添加到两者中,但它被添加到script-src
而不是style-src
。
查看: <style media="screen" nonce="5Cq/QyoJ5Co+LdarO1uvrg==">
Header: Content-Security-Policy-Report-Only script-src 'self' https: 'nonce-5Cq/QyoJ5Co+LdarO1uvrg=='; style-src 'self' https:;
Content-Security-Policy-Report-Only script-src 'self' https: 'nonce-5Cq/QyoJ5Co+LdarO1uvrg=='; style-src 'self' https:;
这是我的content_security_policy.rb :
Rails.application.config.content_security_policy do |config|
config.style_src :self, :https
end
Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
Rails.application.config.content_security_policy_nonce_directives = %w(style-src script-src)
为什么它会出现错误的指令,我怎样才能使它 go 到style-src
?
是的,这会干扰您的 CSP(或者更准确地说,您的 CSP 会干扰您的<script>
标签)。
When using a hash, you need to compute the hash of every <script>
tag, and output the hash in the CSP header as a script-src
. 如果您在 ERB 文件中放置文字<script>
标签,Rails 将无法知道该标签,并且它不会计算 hash 或将其添加到 header 中。
Rails(据我所知)不支持将哈希作为来源。 您可以使用secure-headers gem,它提供了一个hashed_javascript_tag
方法来代替<script>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.