Skip to content

Commit 8f188da

Browse files
Copilotfletchto99
andcommitted
Address PR review feedback: Fix edge cases and improve consistency
- Register NOOP_OVERRIDE in disable! to avoid ArgumentError - Clear @default_config when disable! is called after default - Move clear_disabled inside class << self for consistency - Add comprehensive edge case tests (calling disable! after default, default after disable!, interaction with overrides/dup) - Document behavior in method comments Co-authored-by: fletchto99 <[email protected]>
1 parent b93ac8d commit 8f188da

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

lib/secure_headers/configuration.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@ class IllegalPolicyModificationError < StandardError; end
1111
class << self
1212
# Public: Disable secure_headers entirely. When disabled, no headers will be set.
1313
#
14+
# Note: This should be called before Configuration.default. Calling it after
15+
# Configuration.default has been set will clear the default configuration.
16+
#
1417
# Returns nothing
1518
def disable!
19+
# Clear any existing default config to maintain consistency
20+
remove_instance_variable(:@default_config) if defined?(@default_config)
21+
1622
@disabled = true
1723
@noop_config = create_noop_config.freeze
24+
25+
# Ensure the built-in NOOP override is available even if `default` has never been called
26+
@overrides ||= {}
27+
unless @overrides.key?(NOOP_OVERRIDE)
28+
@overrides[NOOP_OVERRIDE] = method(:create_noop_config_block)
29+
end
1830
end
1931

2032
# Public: Check if secure_headers is disabled

spec/lib/secure_headers/configuration_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,66 @@ module SecureHeaders
138138
Configuration.disable!
139139
expect { Configuration.send(:default_config) }.not_to raise_error
140140
end
141+
142+
it "registers the NOOP_OVERRIDE when disabled without calling default" do
143+
Configuration.disable!
144+
expect(Configuration.overrides(Configuration::NOOP_OVERRIDE)).to_not be_nil
145+
end
146+
147+
it "clears existing default config when called after default" do
148+
Configuration.default do |config|
149+
config.csp = { default_src: %w('self'), script_src: %w('self') }
150+
end
151+
152+
Configuration.disable!
153+
154+
expect(Configuration.disabled?).to be true
155+
expect(Configuration.instance_variable_defined?(:@default_config)).to be false
156+
end
157+
158+
it "allows default to be called after disable! has been invoked" do
159+
Configuration.disable!
160+
reset_config
161+
162+
expect {
163+
Configuration.default do |config|
164+
config.csp = { default_src: %w('self'), script_src: %w('self') }
165+
end
166+
}.not_to raise_error
167+
168+
# After reset_config, disabled? returns nil (not false) because @disabled is removed
169+
expect(Configuration.disabled?).to be_falsy
170+
expect(Configuration.instance_variable_defined?(:@default_config)).to be true
171+
end
172+
173+
it "works correctly with dup when library is disabled" do
174+
Configuration.disable!
175+
config = Configuration.dup
176+
177+
Configuration::CONFIG_ATTRIBUTES.each do |attr|
178+
expect(config.instance_variable_get("@#{attr}")).to eq(OPT_OUT)
179+
end
180+
end
181+
182+
it "does not interfere with override mechanism" do
183+
Configuration.disable!
184+
185+
# Should be able to use opt_out_of_all_protection without error
186+
request = Rack::Request.new("HTTP_X_FORWARDED_SSL" => "on")
187+
expect {
188+
SecureHeaders.opt_out_of_all_protection(request)
189+
}.not_to raise_error
190+
end
191+
192+
it "interacts correctly with named overrides when disabled" do
193+
Configuration.disable!
194+
195+
Configuration.override(:test_override) do |config|
196+
config.x_frame_options = "DENY"
197+
end
198+
199+
expect(Configuration.overrides(:test_override)).to_not be_nil
200+
end
141201
end
142202
end
143203
end

spec/spec_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ def clear_overrides
5353
def clear_appends
5454
remove_instance_variable(:@appends) if defined?(@appends)
5555
end
56-
end
5756

58-
def self.clear_disabled
59-
remove_instance_variable(:@disabled) if defined?(@disabled)
60-
remove_instance_variable(:@noop_config) if defined?(@noop_config)
57+
def clear_disabled
58+
remove_instance_variable(:@disabled) if defined?(@disabled)
59+
remove_instance_variable(:@noop_config) if defined?(@noop_config)
60+
end
6161
end
6262
end
6363
end

0 commit comments

Comments
 (0)