Skip to content

Commit af7f61b

Browse files
Copilotfletchto99
andcommitted
Raise AlreadyConfiguredError when disable! and default conflict
- disable! now raises AlreadyConfiguredError if default has been called - default now raises AlreadyConfiguredError if disable! has been called - Updated tests to verify error conditions - Updated documentation to reflect mutual exclusion Co-authored-by: fletchto99 <[email protected]>
1 parent 8f188da commit af7f61b

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

lib/secure_headers/configuration.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ 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.
14+
# Note: This must be called before Configuration.default. Calling it after
15+
# Configuration.default has been set will raise an AlreadyConfiguredError.
1616
#
1717
# Returns nothing
18+
# Raises AlreadyConfiguredError if Configuration.default has already been called
1819
def disable!
19-
# Clear any existing default config to maintain consistency
20-
remove_instance_variable(:@default_config) if defined?(@default_config)
20+
if defined?(@default_config)
21+
raise AlreadyConfiguredError, "Configuration already set, cannot disable"
22+
end
2123

2224
@disabled = true
2325
@noop_config = create_noop_config.freeze
@@ -41,7 +43,12 @@ def disabled?
4143
# Optionally supply a block to override the defaults set by this library.
4244
#
4345
# Returns the newly created config.
46+
# Raises AlreadyConfiguredError if Configuration.disable! has already been called
4447
def default(&block)
48+
if disabled?
49+
raise AlreadyConfiguredError, "Configuration has been disabled, cannot set default"
50+
end
51+
4552
if defined?(@default_config)
4653
raise AlreadyConfiguredError, "Policy already configured"
4754
end

spec/lib/secure_headers/configuration_spec.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,27 @@ module SecureHeaders
144144
expect(Configuration.overrides(Configuration::NOOP_OVERRIDE)).to_not be_nil
145145
end
146146

147-
it "clears existing default config when called after default" do
147+
it "raises AlreadyConfiguredError when called after default" do
148148
Configuration.default do |config|
149149
config.csp = { default_src: %w('self'), script_src: %w('self') }
150150
end
151151

152+
expect {
153+
Configuration.disable!
154+
}.to raise_error(Configuration::AlreadyConfiguredError, "Configuration already set, cannot disable")
155+
end
156+
157+
it "raises AlreadyConfiguredError when default is called after disable!" do
152158
Configuration.disable!
153159

154-
expect(Configuration.disabled?).to be true
155-
expect(Configuration.instance_variable_defined?(:@default_config)).to be false
160+
expect {
161+
Configuration.default do |config|
162+
config.csp = { default_src: %w('self'), script_src: %w('self') }
163+
end
164+
}.to raise_error(Configuration::AlreadyConfiguredError, "Configuration has been disabled, cannot set default")
156165
end
157166

158-
it "allows default to be called after disable! has been invoked" do
167+
it "allows default to be called after disable! and reset_config" do
159168
Configuration.disable!
160169
reset_config
161170

0 commit comments

Comments
 (0)