class ActionDispatch::ContentSecurityPolicy
Constants
- DEFAULT_NONCE_DIRECTIVES
- DIRECTIVES
- MAPPINGS
Attributes
directives[R]
Public Class Methods
new() { |self| ... }
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 154 def initialize @directives = {} yield self if block_given? end
Public Instance Methods
block_all_mixed_content(enabled = true)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 173 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)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 219 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)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 159 def initialize_copy(other) @directives = other.directives.deep_dup end
plugin_types(*types)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 181 def plugin_types(*types) if types.first @directives["plugin-types"] = types else @directives.delete("plugin-types") end end
report_uri(uri)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 189 def report_uri(uri) @directives["report-uri"] = [uri] end
require_sri_for(*types)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 193 def require_sri_for(*types) if types.first @directives["require-sri-for"] = types else @directives.delete("require-sri-for") end end
sandbox(*values)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 201 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)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 211 def upgrade_insecure_requests(enabled = true) if enabled @directives["upgrade-insecure-requests"] = true else @directives.delete("upgrade-insecure-requests") end end
Private Instance Methods
apply_mapping(source)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 238 def apply_mapping(source) MAPPINGS.fetch(source) do raise ArgumentError, "Unknown content security policy source mapping: #{source.inspect}" end end
apply_mappings(sources)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 225 def apply_mappings(sources) sources.map do |source| case source when Symbol apply_mapping(source) when String, Proc source else raise ArgumentError, "Invalid content security policy source: #{source.inspect}" end end end
build_directive(sources, context)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 260 def build_directive(sources, context) sources.map { |source| resolve_source(source, context) } end
build_directives(context, nonce, nonce_directives)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 244 def build_directives(context, nonce, nonce_directives) @directives.map do |directive, sources| if sources.is_a?(Array) if nonce && nonce_directive?(directive, nonce_directives) "#{directive} #{build_directive(sources, context).join(' ')} 'nonce-#{nonce}'" else "#{directive} #{build_directive(sources, context).join(' ')}" end elsif sources directive else nil end end end
nonce_directive?(directive, nonce_directives)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 282 def nonce_directive?(directive, nonce_directives) nonce_directives.include?(directive) end
resolve_source(source, context)
click to toggle source
# File lib/action_dispatch/http/content_security_policy.rb, line 264 def resolve_source(source, context) case source when String source when Symbol source.to_s when Proc if context.nil? raise RuntimeError, "Missing context for the dynamic content security policy source: #{source.inspect}" else resolved = context.instance_exec(&source) resolved.is_a?(Symbol) ? apply_mapping(resolved) : resolved end else raise RuntimeError, "Unexpected content security policy source: #{source.inspect}" end end