Skip to content

Commit b37c6fa

Browse files
authored
Merge pull request #301 from stve/useragent
handle null useragent version
2 parents 2b82324 + c222597 commit b37c6fa

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ContentSecurityPolicy
99
# constants to be used for version-specific UA sniffing
1010
VERSION_46 = ::UserAgent::Version.new("46")
1111
VERSION_10 = ::UserAgent::Version.new("10")
12+
FALLBACK_VERSION = ::UserAgent::Version.new("0")
1213

1314
def initialize(config = nil, user_agent = OTHER)
1415
@config = if config.is_a?(Hash)
@@ -213,7 +214,7 @@ def strip_source_schemes(source_list)
213214
# Returns an array of symbols representing the directives.
214215
def supported_directives
215216
@supported_directives ||= if VARIATIONS[@parsed_ua.browser]
216-
if @parsed_ua.browser == "Firefox" && @parsed_ua.version >= VERSION_46
217+
if @parsed_ua.browser == "Firefox" && ((@parsed_ua.version || FALLBACK_VERSION) >= VERSION_46)
217218
VARIATIONS["FirefoxTransitional"]
218219
else
219220
VARIATIONS[@parsed_ua.browser]

lib/secure_headers/headers/policy_management.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def validate_config!(config)
217217
def nonces_supported?(user_agent)
218218
user_agent = UserAgent.parse(user_agent) if user_agent.is_a?(String)
219219
MODERN_BROWSERS.include?(user_agent.browser) ||
220-
user_agent.browser == "Safari" && user_agent.version >= CSP::VERSION_10
220+
user_agent.browser == "Safari" && (user_agent.version || CSP::FALLBACK_VERSION) >= CSP::VERSION_10
221221
end
222222

223223
# Public: combine the values from two different configs.

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ module SecureHeaders
149149
policy = ContentSecurityPolicy.new(complex_opts, USER_AGENTS[:safari6])
150150
expect(policy.value).to eq("default-src default-src.com; connect-src connect-src.com; font-src font-src.com; frame-src child-src.com; img-src img-src.com; media-src media-src.com; object-src object-src.com; sandbox sandbox.com; script-src script-src.com 'unsafe-inline'; style-src style-src.com; report-uri report-uri.com")
151151
end
152+
153+
it "falls back to standard Firefox defaults when the useragent version is not present" do
154+
ua = USER_AGENTS[:firefox].dup
155+
allow(ua).to receive(:version).and_return(nil)
156+
policy = ContentSecurityPolicy.new(complex_opts, ua)
157+
expect(policy.value).to eq("default-src default-src.com; base-uri base-uri.com; connect-src connect-src.com; font-src font-src.com; form-action form-action.com; frame-ancestors frame-ancestors.com; frame-src child-src.com; img-src img-src.com; media-src media-src.com; object-src object-src.com; sandbox sandbox.com; script-src script-src.com 'nonce-123456'; style-src style-src.com; upgrade-insecure-requests; report-uri report-uri.com")
158+
end
152159
end
153160
end
154161
end

0 commit comments

Comments
 (0)