class BridgetownContentSecurityPolicy::Policy

Constants

DIRECTIVES
MAPPINGS

Attributes

directives[R]

Public Class Methods

new(directives = nil) { |self| ... } click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 49
def initialize(directives = nil)
  if directives
    @directives = directives
  else
    @directives = {}
    yield self if block_given?
  end
end

Public Instance Methods

block_all_mixed_content(enabled = true) click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 68
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() click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 114
def build
  build_directives.compact.join("; ")
end
merge(policy) click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 118
def merge(policy)
  if policy
    self.class.new(@directives.merge(policy.directives))
  else
    self
  end
end
plugin_types(*types) click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 76
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/bridgetown-content-security-policy/policy.rb, line 84
def report_uri(uri)
  @directives["report-uri"] = [uri]
end
require_sri_for(*types) click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 88
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/bridgetown-content-security-policy/policy.rb, line 96
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/bridgetown-content-security-policy/policy.rb, line 106
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/bridgetown-content-security-policy/policy.rb, line 141
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/bridgetown-content-security-policy/policy.rb, line 128
def apply_mappings(sources)
  sources.map do |source|
    case source
    when Symbol
      apply_mapping(source)
    when String
      source
    else
      raise ArgumentError, "Invalid content security policy source: #{source.inspect}"
    end
  end
end
build_directive(sources) click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 157
def build_directive(sources)
  sources.map { |source| resolve_source(source) }
end
build_directives() click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 147
def build_directives
  @directives.map do |directive, sources|
    if sources.is_a?(Array)
      "#{directive} #{build_directive(sources).join(" ")}"
    elsif sources
      directive
    end
  end
end
resolve_source(source) click to toggle source
# File lib/bridgetown-content-security-policy/policy.rb, line 161
def resolve_source(source)
  case source
  when String
    source
  when Symbol
    source.to_s
  else
    raise "Unexpected content security policy source: #{source.inspect}"
  end
end