2

我希望在我的应用程序中实现 Content-Security-Policy 并通过向 Content-Security-Policy-Report-Only 标头添加规则来迭代策略,这样我就可以在生产环境中测试新策略的同时保持现有的安全性.

根据W3C CSP2 建议,这是 Content-Security-Policy-Report-Only 标头的预期用途。但是,当尝试配置 Rails 来实现这一点时,您似乎可以让 CSP 处于report only模式或“强制”模式:

Rails.application.config.content_security_policy_report_only = true

我已经尝试查看 Rails 源代码,看看是否有办法实现这一点,但看起来好像是非此即彼的行为:

ActionDispatch::ContentSecurityPolicy::Middleware#Line:40

def call(env)
  ...
  return response if policy_present?(headers)

  if policy = request.content_security_policy
    ...
    headers[header_name(request)] = policy.build(context, nonce, nonce_directives)
  end

  response
end

...

def header_name(request)
  if request.content_security_policy_report_only
    POLICY_REPORT_ONLY
  else
    POLICY
  end
end

def policy_present?(headers)
  headers[POLICY] || headers[POLICY_REPORT_ONLY]
end

有没有办法配置 Rails 来处理同一个请求中的两个标头?或者我将不得不手动设置我的标题而不使用 Rails 的内置 DSL?

4

0 回答 0