Skip to content

Commit 3de87f9

Browse files
committed
handle some edge cases around policy merging
1 parent 78ecd02 commit 3de87f9

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,11 @@ def combine_policies(original, additions)
213213
# when each hash contains a value for a given key.
214214
original.merge(additions) do |directive, lhs, rhs|
215215
if source_list?(directive)
216-
lhs | rhs
216+
(lhs.to_a + rhs).uniq.compact
217217
else
218218
rhs
219219
end
220-
end
220+
end.reject { |_, value| value.nil? || value == [] } # this mess prevents us from adding empty directives.
221221
end
222222

223223
private

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ module SecureHeaders
117117

118118
specify { expect(ContentSecurityPolicy.idempotent_additions?({script_src: %w(a.com b.com)}, script_src: %w(b.com))).to be true }
119119
specify { expect(ContentSecurityPolicy.idempotent_additions?({script_src: %w(a.com b.com)}, script_src: %w(b.com a.com))).to be true }
120+
specify { expect(ContentSecurityPolicy.idempotent_additions?({script_src: %w(a.com b.com)}, script_src: %w())).to be true }
121+
specify { expect(ContentSecurityPolicy.idempotent_additions?({script_src: %w(a.com b.com)}, script_src: [nil])).to be true }
122+
specify { expect(ContentSecurityPolicy.idempotent_additions?({script_src: %w(a.com b.com)}, style_src: [nil])).to be true }
120123
end
121124

122125
describe "#value" do

0 commit comments

Comments
 (0)