Skip to content

Commit 680e7e9

Browse files
committed
make the nonce support helper method public
1 parent 7801462 commit 680e7e9

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

lib/secure_headers.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def opt_out?
4747
SECURE_HEADERS_CONFIG = "secure_headers_request_config".freeze
4848
NONCE_KEY = "secure_headers_content_security_policy_nonce".freeze
4949
HTTPS = "https".freeze
50+
CSP = ContentSecurityPolicy
5051

5152
ALL_HEADER_CLASSES = [
5253
ContentSecurityPolicyConfig,

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,7 @@ def supported_directives
224224
end
225225

226226
def nonces_supported?
227-
@nonces_supported ||= MODERN_BROWSERS.include?(@parsed_ua.browser) ||
228-
@parsed_ua.browser == "Safari" && @parsed_ua.version >= VERSION_10
227+
@nonces_supported ||= self.class.nonces_supported?(@parsed_ua)
229228
end
230229

231230
def symbol_to_hyphen_case(sym)

lib/secure_headers/headers/policy_management.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ def validate_config!(config)
211211
end
212212
end
213213

214+
# Public: check if a user agent supports CSP nonces
215+
#
216+
# user_agent - a String or a UserAgent object
217+
def nonces_supported?(user_agent)
218+
user_agent = UserAgent.parse(user_agent) if user_agent.is_a?(String)
219+
MODERN_BROWSERS.include?(user_agent.browser) ||
220+
user_agent.browser == "Safari" && user_agent.version >= CSP::VERSION_10
221+
end
222+
214223
# Public: combine the values from two different configs.
215224
#
216225
# original - the main config

0 commit comments

Comments
 (0)