Skip to content

Commit 0d20f87

Browse files
committed
ensure that dynamic policies are associated with the correct variations. this was a bug, but things still 'worked' externally
1 parent 4fcc462 commit 0d20f87

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/secure_headers/headers/policy_management.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,9 @@ def combine_policies(original, additions)
224224
end
225225

226226
def ua_to_variation(user_agent)
227-
if family = user_agent.browser && VARIATIONS.key?(family)
228-
VARIATIONS[family]
227+
family = user_agent.browser
228+
if family && VARIATIONS.key?(family)
229+
family
229230
else
230231
OTHER
231232
end

spec/lib/secure_headers_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@ module SecureHeaders
6161
expect(hash[XFrameOptions::HEADER_NAME]).to eq(XFrameOptions::SAMEORIGIN)
6262
end
6363

64+
it "produces a UA-specific CSP when overriding (and busting the cache)" do
65+
config = Configuration.default do |config|
66+
config.csp = {
67+
default_src: %w('self'),
68+
child_src: %w('self'), #unsupported by firefox
69+
frame_src: %w('self')
70+
}
71+
end
72+
firefox_request = Rack::Request.new(request.env.merge("HTTP_USER_AGENT" => USER_AGENTS[:firefox]))
73+
74+
# append an unsupported directive
75+
SecureHeaders.override_content_security_policy_directives(firefox_request, plugin_types: %w(flash))
76+
# append a supported directive
77+
SecureHeaders.override_content_security_policy_directives(firefox_request, script_src: %w('self'))
78+
79+
hash = SecureHeaders.header_hash_for(firefox_request)
80+
expect(hash[CSP::HEADER_NAME]).to eq("default-src 'self'; frame-src 'self'; script-src 'self'")
81+
end
82+
6483
it "produces a hash of headers with default config" do
6584
Configuration.default
6685
hash = SecureHeaders.header_hash_for(request)

0 commit comments

Comments
 (0)