Skip to content

Commit 7a07c3c

Browse files
authored
Merge pull request #386 from twitter/deprecate-get
Deprecate Configuration#get
2 parents c7fc44c + 0b9b443 commit 7a07c3c

File tree

8 files changed

+37
-27
lines changed

8 files changed

+37
-27
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ group :guard do
1919
gem "growl"
2020
gem "guard-rspec", platforms: [:ruby_19, :ruby_20, :ruby_21, :ruby_22, :ruby_23, :ruby_24]
2121
gem "rb-fsevent"
22+
gem "terminal-notifier-guard"
2223
end

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

lib/secure_headers/headers/clear_site_data.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def validate_config!(config)
4848
#
4949
# Returns a String of quoted values that are comma separated.
5050
def make_header_value(types)
51-
types.map { |t| "\"#{t}\""}.join(", ")
51+
types.map { |t| %("#{t}") }.join(", ")
5252
end
5353
end
5454
end

spec/lib/secure_headers/configuration_spec.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ 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
13+
end
14+
15+
it "warns when using deprecated internal-ish #get API" do
16+
expect(Kernel).to receive(:warn).once.with(/`#get` is deprecated/)
17+
Configuration.get(Configuration::DEFAULT_CONFIG)
1318
end
1419

1520
it "has an 'noop' config" do
16-
expect(Configuration.get(Configuration::NOOP_CONFIGURATION)).to_not be_nil
21+
expect(Configuration.get(Configuration::NOOP_CONFIGURATION, internal: true)).to_not be_nil
1722
end
1823

1924
it "precomputes headers upon creation" do
20-
default_config = Configuration.get(Configuration::DEFAULT_CONFIG)
25+
default_config = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
2126
header_hash = default_config.cached_headers.each_with_object({}) do |(key, value), hash|
2227
header_name, header_value = if key == :csp
2328
value["Chrome"]
@@ -35,8 +40,8 @@ module SecureHeaders
3540
# do nothing, just copy it
3641
end
3742

38-
config = Configuration.get(:test_override)
39-
noop = Configuration.get(Configuration::NOOP_CONFIGURATION)
43+
config = Configuration.get(:test_override, internal: true)
44+
noop = Configuration.get(Configuration::NOOP_CONFIGURATION, internal: true)
4045
[:csp, :csp_report_only, :cookies].each do |key|
4146
expect(config.send(key)).to eq(noop.send(key))
4247
end
@@ -47,24 +52,24 @@ module SecureHeaders
4752
config.x_content_type_options = OPT_OUT
4853
end
4954

50-
expect(Configuration.get.cached_headers).to_not eq(Configuration.get(:test_override).cached_headers)
55+
expect(Configuration.get(Configuration::DEFAULT_CONFIG, internal: true).cached_headers).to_not eq(Configuration.get(:test_override, internal: true).cached_headers)
5156
end
5257

5358
it "stores an override of the global config" do
5459
Configuration.override(:test_override) do |config|
5560
config.x_frame_options = "DENY"
5661
end
5762

58-
expect(Configuration.get(:test_override)).to_not be_nil
63+
expect(Configuration.get(:test_override, internal: true)).to_not be_nil
5964
end
6065

6166
it "deep dup's config values when overriding so the original cannot be modified" do
6267
Configuration.override(:override) do |config|
6368
config.csp[:default_src] << "'self'"
6469
end
6570

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

6974
expect(override.csp.directive_value(:default_src)).not_to be(default.csp.directive_value(:default_src))
7075
end
@@ -78,9 +83,9 @@ module SecureHeaders
7883
config.csp = config.csp.merge(script_src: %w(example.org))
7984
end
8085

81-
original_override = Configuration.get(:override)
86+
original_override = Configuration.get(:override, internal: true)
8287
expect(original_override.csp.to_h).to eq(default_src: %w('self'), script_src: %w('self'))
83-
override_config = Configuration.get(:second_override)
88+
override_config = Configuration.get(:second_override, internal: true)
8489
expect(override_config.csp.to_h).to eq(default_src: %w('self'), script_src: %w('self' example.org))
8590
end
8691

@@ -101,7 +106,7 @@ module SecureHeaders
101106
config.cookies = OPT_OUT
102107
end
103108

104-
config = Configuration.get
109+
config = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
105110
expect(config.cookies).to eq(OPT_OUT)
106111
end
107112

@@ -110,7 +115,7 @@ module SecureHeaders
110115
config.cookies = {httponly: true, secure: true, samesite: {lax: false}}
111116
end
112117

113-
config = Configuration.get
118+
config = Configuration.get(Configuration::DEFAULT_CONFIG, internal: true)
114119
expect(config.cookies).to eq({httponly: true, secure: true, samesite: {lax: false}})
115120
end
116121
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: 2 additions & 2 deletions
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
@@ -66,7 +66,7 @@ module SecureHeaders
6666
end
6767

6868
it "allows opting out of cookie protection with OPT_OUT alone" do
69-
Configuration.default { |config| config.cookies = OPT_OUT}
69+
Configuration.default { |config| config.cookies = OPT_OUT }
7070

7171
# do NOT make this request https. non-https requests modify a config,
7272
# causing an exception when operating on OPT_OUT. This ensures we don't

spec/lib/secure_headers/view_helpers_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def content_tag(type, content = nil, options = nil, &block)
6767
end
6868

6969
if options.is_a?(Hash)
70-
options = options.map {|k, v| " #{k}=#{v}"}
70+
options = options.map { |k, v| " #{k}=#{v}" }
7171
end
7272
"<#{type}#{options}>#{content}</#{type}>"
7373
end

0 commit comments

Comments
 (0)