跳至內容 跳至搜尋

Action Dispatch 網頁內容安全政策

設定 HTTP 網頁內容安全政策 (Content-Security-Policy) 回應標頭,以協助防範 XSS 和注入式攻擊。

全域政策範例

Rails.application.config.content_security_policy do |policy|
  policy.default_src :self, :https
  policy.font_src    :self, :https, :data
  policy.img_src     :self, :https, :data
  policy.object_src  :none
  policy.script_src  :self, :https
  policy.style_src   :self, :https

  # Specify URI for violation reports
  policy.report_uri "/csp-violation-report-endpoint"
end
命名空間
方法
B
I
N
P
R
S
U

屬性

[R] directives(指令)

類別公開方法

new()

# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 177
def initialize
  @directives = {}
  yield self if block_given?
end

實例公開方法

block_all_mixed_content(enabled = true)

指定當頁面使用 HTTPS 時,是否要防止使用者代理程式透過 HTTP 載入任何資源。

policy.block_all_mixed_content

傳遞 false 以再次允許。

policy.block_all_mixed_content false
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 205
def block_all_mixed_content(enabled = true)
  if enabled
    @directives["block-all-mixed-content"] = true
  else
    @directives.delete("block-all-mixed-content")
  end
end

build(context = nil, nonce = nil, nonce_directives = nil)

# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 294
def build(context = nil, nonce = nil, nonce_directives = nil)
  nonce_directives = DEFAULT_NONCE_DIRECTIVES if nonce_directives.nil?
  build_directives(context, nonce, nonce_directives).compact.join("; ")
end

initialize_copy(other)

# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 182
def initialize_copy(other)
  @directives = other.directives.deep_dup
end

plugin_types(*types)

限制可以嵌入的插件類型。

policy.plugin_types "application/x-shockwave-flash"

留空以允許所有插件類型。

policy.plugin_types
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 221
def plugin_types(*types)
  if types.first
    @directives["plugin-types"] = types
  else
    @directives.delete("plugin-types")
  end
end

report_uri(uri)

啟用 report-uri 指令。違規報告將會發送到指定的 URI。

policy.report_uri "/csp-violation-report-endpoint"
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 235
def report_uri(uri)
  @directives["report-uri"] = [uri]
end

require_sri_for(*types)

指定需要 子資源完整性 (Subresource Integrity) 的資源類型。

policy.require_sri_for :script, :style

留空則不需要子資源完整性。

policy.require_sri_for
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 247
def require_sri_for(*types)
  if types.first
    @directives["require-sri-for"] = types
  else
    @directives.delete("require-sri-for")
  end
end

sandbox(*values)

指定是否要為請求的資源啟用 沙盒 (sandbox)

policy.sandbox

可以將值作為參數傳遞。

policy.sandbox "allow-scripts", "allow-modals"

傳遞 false 以停用沙盒。

policy.sandbox false
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 268
def sandbox(*values)
  if values.empty?
    @directives["sandbox"] = true
  elsif values.first
    @directives["sandbox"] = values
  else
    @directives.delete("sandbox")
  end
end

upgrade_insecure_requests(enabled = true)

指定使用者代理程式是否應將所有透過 HTTP 的資源視為 HTTPS。

policy.upgrade_insecure_requests

傳遞 false 以停用它。

policy.upgrade_insecure_requests false
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 286
def upgrade_insecure_requests(enabled = true)
  if enabled
    @directives["upgrade-insecure-requests"] = true
  else
    @directives.delete("upgrade-insecure-requests")
  end
end