Skip to content

Commit 3301a0d

Browse files
committed
Make SecureHeaders.config_for public allowing us to access config from
different contexts.
1 parent 0cd223a commit 3301a0d

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

lib/secure_headers.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ def content_security_policy_style_nonce(request)
149149
content_security_policy_nonce(request, CSP::STYLE_SRC)
150150
end
151151

152+
# Public: Retreives the config for a given header type:
153+
#
154+
# Checks to see if there is an override for this request, then
155+
# Checks to see if a named override is used for this request, then
156+
# Falls back to the global config
157+
def config_for(request)
158+
request.env[SECURE_HEADERS_CONFIG] ||
159+
Configuration.get(Configuration::DEFAULT_CONFIG)
160+
end
161+
152162
private
153163

154164
# Private: gets or creates a nonce for CSP.
@@ -217,16 +227,6 @@ def use_cached_headers(default_headers, request)
217227
end
218228
end
219229

220-
# Private: Retreives the config for a given header type:
221-
#
222-
# Checks to see if there is an override for this request, then
223-
# Checks to see if a named override is used for this request, then
224-
# Falls back to the global config
225-
def config_for(request)
226-
request.env[SECURE_HEADERS_CONFIG] ||
227-
Configuration.get(Configuration::DEFAULT_CONFIG)
228-
end
229-
230230
# Private: chooses the applicable CSP header for the provided user agent.
231231
#
232232
# headers - a hash of header_config_key => [header_name, header_value]

lib/secure_headers/middleware.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def call(env)
1111
req = Rack::Request.new(env)
1212
status, headers, response = @app.call(env)
1313

14-
flag_cookies_as_secure!(headers) if config(req).secure_cookies
14+
config = SecureHeaders.config_for(req)
15+
flag_cookies_as_secure!(headers) if config.secure_cookies
1516
headers.merge!(SecureHeaders.header_hash_for(req))
1617
[status, headers, response]
1718
end
@@ -33,9 +34,5 @@ def flag_cookies_as_secure!(headers)
3334
end.join("\n")
3435
end
3536
end
36-
37-
def config(req)
38-
req.env[SECURE_HEADERS_CONFIG] || Configuration.get(Configuration::DEFAULT_CONFIG)
39-
end
4037
end
4138
end

0 commit comments

Comments
 (0)