класс ActionDispatch::ContentSecurityPolicy
Политика безопасности содержимого 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
Константы
- HASH_SOURCE_ALGORITHM_PREFIXES
Атрибуты
Публичные методы класса
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 182
def initialize
@directives = {}
yield self if block_given?
end Публичные методы экземпляра
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 210
def block_all_mixed_content(enabled = true)
if enabled
@directives["block-all-mixed-content"] = true
else
@directives.delete("block-all-mixed-content")
end
end Укажите, следует ли запретить пользовательскому агенту загружать ресурсы по HTTP, если страница использует HTTPS:
policy.block_all_mixed_content
Передайте false, чтобы снова разрешить это:
policy.block_all_mixed_content false
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 299
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 # File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 187 def initialize_copy(other) @directives = other.directives.deep_dup end
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 226
def plugin_types(*types)
if types.first
@directives["plugin-types"] = types
else
@directives.delete("plugin-types")
end
end Ограничивает набор встраиваемых плагинов:
policy.plugin_types "application/x-shockwave-flash"
Оставьте пустым, чтобы разрешить все плагины:
policy.plugin_types
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 240 def report_uri(uri) @directives["report-uri"] = [uri] end
Включает директиву report-uri. Отчёты о нарушениях будут отправляться по указанному URI:
policy.report_uri "/csp-violation-report-endpoint"
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 252
def require_sri_for(*types)
if types.first
@directives["require-sri-for"] = types
else
@directives.delete("require-sri-for")
end
end Укажите типы ресурсов, для которых требуется проверка целостности подресурсов:
policy.require_sri_for :script, :style
Оставьте пустым, чтобы не требовать проверку целостности подресурсов:
policy.require_sri_for
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 273
def sandbox(*values)
if values.empty?
@directives["sandbox"] = true
elsif values.first
@directives["sandbox"] = values
else
@directives.delete("sandbox")
end
end Укажите, следует ли включить песочницу для запрошенного ресурса:
policy.sandbox
Значения можно передать в виде аргументов:
policy.sandbox "allow-scripts", "allow-modals"
Передайте false, чтобы отключить песочницу:
policy.sandbox false
# File actionpack/lib/action_dispatch/http/content_security_policy.rb, line 291
def upgrade_insecure_requests(enabled = true)
if enabled
@directives["upgrade-insecure-requests"] = true
else
@directives.delete("upgrade-insecure-requests")
end
end Укажите, следует ли пользовательским агентам считать ресурсы, загружаемые по HTTP, ресурсами HTTPS:
policy.upgrade_insecure_requests
Передайте false, чтобы отключить это:
policy.upgrade_insecure_requests false
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.