Class: BridgetownContentSecurityPolicy::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/bridgetown-content-security-policy/policy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directives = nil) ⇒ Policy



49
50
51
52
53
54
55
56
# 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

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



47
48
49
# File 'lib/bridgetown-content-security-policy/policy.rb', line 47

def directives
  @directives
end

Instance Method Details

#block_all_mixed_content(enabled = true) ⇒ Object



68
69
70
71
72
73
74
# 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

#buildObject



114
115
116
# File 'lib/bridgetown-content-security-policy/policy.rb', line 114

def build
  build_directives.compact.join("; ")
end

#merge(policy) ⇒ Object



118
119
120
121
122
123
124
# 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) ⇒ Object



76
77
78
79
80
81
82
# 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) ⇒ Object



84
85
86
# File 'lib/bridgetown-content-security-policy/policy.rb', line 84

def report_uri(uri)
  @directives["report-uri"] = [uri]
end

#require_sri_for(*types) ⇒ Object



88
89
90
91
92
93
94
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
# 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) ⇒ Object



106
107
108
109
110
111
112
# 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