Skip to content

Commit 78b8bc6

Browse files
committed
Deprecate Configuration#get
1 parent c7fc44c commit 78b8bc6

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

lib/secure_headers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def header_hash_for(request)
196196
#
197197
# name - the name of the previously configured override.
198198
def use_secure_headers_override(request, name)
199-
if config = Configuration.get(name)
199+
if config = Configuration.get(name, internal: true)
200200
override_secure_headers_request_config(request, config)
201201
else
202202
raise ArgumentError.new("no override by the name of #{name} has been configured")
@@ -228,7 +228,7 @@ def content_security_policy_style_nonce(request)
228228
# Falls back to the global config
229229
def config_for(request, prevent_dup = false)
230230
config = request.env[SECURE_HEADERS_CONFIG] ||
231-
Configuration.get(Configuration::DEFAULT_CONFIG)
231+
Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
232232

233233

234234
# Global configs are frozen, per-request configs are not. When we're not

lib/secure_headers/configuration.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def default(&block)
2828
#
2929
# Returns: the newly created config
3030
def override(name, base = DEFAULT_CONFIG, &block)
31-
unless get(base)
31+
unless get(base, internal: true)
3232
raise NotYetConfiguredError, "#{base} policy not yet supplied"
3333
end
3434
override = @configurations[base].dup
@@ -40,7 +40,11 @@ def override(name, base = DEFAULT_CONFIG, &block)
4040
#
4141
# Returns the configuration with a given name or raises a
4242
# NotYetConfiguredError if `default` has not been called.
43-
def get(name = DEFAULT_CONFIG)
43+
def get(name = DEFAULT_CONFIG, internal: false)
44+
unless internal
45+
Kernel.warn "#{Kernel.caller.first}: [DEPRECATION] `#get` is deprecated. It will be removed in the next major release. Use SecureHeaders::Configuration.dup to retrieve the default config."
46+
end
47+
4448
if @configurations.nil?
4549
raise NotYetConfiguredError, "Default policy not yet supplied"
4650
end

spec/lib/secure_headers/configuration_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ module SecureHeaders
99
end
1010

1111
it "has a default config" do
12-
expect(Configuration.get(Configuration::DEFAULT_CONFIG)).to_not be_nil
12+
expect(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)).to_not be_nil
1313
end
1414

1515
it "has an 'noop' config" do
16-
expect(Configuration.get(Configuration::NOOP_CONFIGURATION)).to_not be_nil
16+
expect(Configuration.get(Configuration::NOOP_CONFIGURATION, internal: true)).to_not be_nil
1717
end
1818

1919
it "precomputes headers upon creation" do
20-
default_config = Configuration.get(Configuration::DEFAULT_CONFIG)
20+
default_config = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
2121
header_hash = default_config.cached_headers.each_with_object({}) do |(key, value), hash|
2222
header_name, header_value = if key == :csp
2323
value["Chrome"]
@@ -35,8 +35,8 @@ module SecureHeaders
3535
# do nothing, just copy it
3636
end
3737

38-
config = Configuration.get(:test_override)
39-
noop = Configuration.get(Configuration::NOOP_CONFIGURATION)
38+
config = Configuration.get(:test_override, internal: true)
39+
noop = Configuration.get(Configuration::NOOP_CONFIGURATION, internal: true)
4040
[:csp, :csp_report_only, :cookies].each do |key|
4141
expect(config.send(key)).to eq(noop.send(key))
4242
end
@@ -47,24 +47,24 @@ module SecureHeaders
4747
config.x_content_type_options = OPT_OUT
4848
end
4949

50-
expect(Configuration.get.cached_headers).to_not eq(Configuration.get(:test_override).cached_headers)
50+
expect(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).cached_headers).to_not eq(Configuration.get(:test_override, internal: true).cached_headers)
5151
end
5252

5353
it "stores an override of the global config" do
5454
Configuration.override(:test_override) do |config|
5555
config.x_frame_options = "DENY"
5656
end
5757

58-
expect(Configuration.get(:test_override)).to_not be_nil
58+
expect(Configuration.get(:test_override, internal: true)).to_not be_nil
5959
end
6060

6161
it "deep dup's config values when overriding so the original cannot be modified" do
6262
Configuration.override(:override) do |config|
6363
config.csp[:default_src] << "'self'"
6464
end
6565

66-
default = Configuration.get
67-
override = Configuration.get(:override)
66+
default = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
67+
override = Configuration.get(:override, internal: true)
6868

6969
expect(override.csp.directive_value(:default_src)).not_to be(default.csp.directive_value(:default_src))
7070
end
@@ -78,9 +78,9 @@ module SecureHeaders
7878
config.csp = config.csp.merge(script_src: %w(example.org))
7979
end
8080

81-
original_override = Configuration.get(:override)
81+
original_override = Configuration.get(:override, internal: true)
8282
expect(original_override.csp.to_h).to eq(default_src: %w('self'), script_src: %w('self'))
83-
override_config = Configuration.get(:second_override)
83+
override_config = Configuration.get(:second_override, internal: true)
8484
expect(override_config.csp.to_h).to eq(default_src: %w('self'), script_src: %w('self' example.org))
8585
end
8686

@@ -101,7 +101,7 @@ module SecureHeaders
101101
config.cookies = OPT_OUT
102102
end
103103

104-
config = Configuration.get
104+
config = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
105105
expect(config.cookies).to eq(OPT_OUT)
106106
end
107107

@@ -110,7 +110,7 @@ module SecureHeaders
110110
config.cookies = {httponly: true, secure: true, samesite: {lax: false}}
111111
end
112112

113-
config = Configuration.get
113+
config = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
114114
expect(config.cookies).to eq({httponly: true, secure: true, samesite: {lax: false}})
115115
end
116116
end

spec/lib/secure_headers/headers/policy_management_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ module SecureHeaders
152152
script_src: %w('self'),
153153
}
154154
end
155-
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get.csp.to_h, style_src: %w(anothercdn.com))
155+
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).csp.to_h, style_src: %w(anothercdn.com))
156156
csp = ContentSecurityPolicy.new(combined_config)
157157
expect(csp.name).to eq(ContentSecurityPolicyConfig::HEADER_NAME)
158158
expect(csp.value).to eq("default-src https:; script-src 'self'; style-src https: anothercdn.com")
@@ -167,7 +167,7 @@ module SecureHeaders
167167
}.freeze
168168
end
169169
report_uri = "https://report-uri.io/asdf"
170-
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get.csp.to_h, report_uri: [report_uri])
170+
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).csp.to_h, report_uri: [report_uri])
171171
csp = ContentSecurityPolicy.new(combined_config, USER_AGENTS[:firefox])
172172
expect(csp.value).to include("report-uri #{report_uri}")
173173
end
@@ -183,7 +183,7 @@ module SecureHeaders
183183
non_default_source_additions = ContentSecurityPolicy::NON_FETCH_SOURCES.each_with_object({}) do |directive, hash|
184184
hash[directive] = %w("http://example.org)
185185
end
186-
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get.csp.to_h, non_default_source_additions)
186+
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).csp.to_h, non_default_source_additions)
187187

188188
ContentSecurityPolicy::NON_FETCH_SOURCES.each do |directive|
189189
expect(combined_config[directive]).to eq(%w("http://example.org))
@@ -198,7 +198,7 @@ module SecureHeaders
198198
report_only: false
199199
}
200200
end
201-
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get.csp.to_h, report_only: true)
201+
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).csp.to_h, report_only: true)
202202
csp = ContentSecurityPolicy.new(combined_config, USER_AGENTS[:firefox])
203203
expect(csp.name).to eq(ContentSecurityPolicyReportOnlyConfig::HEADER_NAME)
204204
end
@@ -211,7 +211,7 @@ module SecureHeaders
211211
block_all_mixed_content: false
212212
}
213213
end
214-
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get.csp.to_h, block_all_mixed_content: true)
214+
combined_config = ContentSecurityPolicy.combine_policies(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).csp.to_h, block_all_mixed_content: true)
215215
csp = ContentSecurityPolicy.new(combined_config)
216216
expect(csp.value).to eq("default-src https:; block-all-mixed-content; script-src 'self'")
217217
end
@@ -221,7 +221,7 @@ module SecureHeaders
221221
config.csp = OPT_OUT
222222
end
223223
expect do
224-
ContentSecurityPolicy.combine_policies(Configuration.get.csp.to_h, script_src: %w(anothercdn.com))
224+
ContentSecurityPolicy.combine_policies(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).csp.to_h, script_src: %w(anothercdn.com))
225225
end.to raise_error(ContentSecurityPolicyConfigError)
226226
end
227227
end

spec/lib/secure_headers/middleware_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module SecureHeaders
5050
end
5151
request = Rack::Request.new({})
5252
SecureHeaders.use_secure_headers_override(request, "my_custom_config")
53-
expect(request.env[SECURE_HEADERS_CONFIG]).to be(Configuration.get("my_custom_config"))
53+
expect(request.env[SECURE_HEADERS_CONFIG]).to be(Configuration.get("my_custom_config", internal: true))
5454
_, env = middleware.call request.env
5555
expect(env[ContentSecurityPolicyConfig::HEADER_NAME]).to match("example.org")
5656
end

0 commit comments

Comments
 (0)