Skip to content

Commit 2cb0fba

Browse files
committed
Merge pull request #223 from twitter/handle-frozen-hashes
Handle frozen hashes
2 parents 7958390 + 0c72911 commit 2cb0fba

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 if original.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)