Skip to content

Commit 5c3f928

Browse files
committed
Handle the case where the hash is frozen and we're adding a directive
Previously, this would cause an exception: RuntimeError: can't modify frozen Hash ./lib/secure_headers/headers/content_security_policy.rb:216:in `block in combine_policies' ./lib/secure_headers/headers/content_security_policy.rb:214:in `each' ./lib/secure_headers/headers/content_security_policy.rb:214:in `combine_policies' ./spec/lib/secure_headers/headers/content_security_policy_spec.rb:127:in `block (3 levels) in <module:SecureHeaders>'
1 parent 180d6fa commit 5c3f928

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ def combine_policies(original, additions)
210210
raise ContentSecurityPolicyConfigError.new("Attempted to override an opt-out CSP config.")
211211
end
212212

213+
original = original.dup # in case the hash is frozen
214+
213215
# in case we would be appending to an empty directive, fill it with the default-src value
214216
additions.keys.each do |directive|
215217
unless original[directive] || !source_list?(directive)

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ module SecureHeaders
117117
expect(csp.value).to eq("default-src https:; script-src https: anothercdn.com")
118118
end
119119

120+
it "combines directives where the original value is nil and the hash is frozen" do
121+
Configuration.default do |config|
122+
config.csp = {
123+
default_src: %w('self'),
124+
report_only: false
125+
}.freeze
126+
end
127+
combined_config = CSP.combine_policies(Configuration.get.csp, report_uri: %w(https://report-uri.io/asdf))
128+
expect(combined_config[:report_uri]).to_not be_nil
129+
end
130+
120131
it "overrides the report_only flag" do
121132
Configuration.default do |config|
122133
config.csp = {

0 commit comments

Comments
 (0)