Skip to content

Commit 261ac0b

Browse files
Copilotfletchto99
andcommitted
Add Configuration.disable! to completely disable secure_headers
Co-authored-by: fletchto99 <[email protected]>
1 parent b4d2d2c commit 261ac0b

File tree

4,667 files changed

+874100
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

4,667 files changed

+874100
-0
lines changed

lib/secure_headers/configuration.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ class AlreadyConfiguredError < StandardError; end
99
class NotYetConfiguredError < StandardError; end
1010
class IllegalPolicyModificationError < StandardError; end
1111
class << self
12+
# Public: Disable secure_headers entirely. When disabled, no headers will be set.
13+
#
14+
# Returns nothing
15+
def disable!
16+
@disabled = true
17+
# Create a NOOP config that opts out of all headers
18+
@noop_config = new do |config|
19+
CONFIG_ATTRIBUTES.each do |attr|
20+
config.instance_variable_set("@#{attr}", OPT_OUT)
21+
end
22+
end.freeze
23+
end
24+
25+
# Public: Check if secure_headers is disabled
26+
#
27+
# Returns boolean
28+
def disabled?
29+
defined?(@disabled) && @disabled
30+
end
31+
1232
# Public: Set the global default configuration.
1333
#
1434
# Optionally supply a block to override the defaults set by this library.
@@ -101,6 +121,7 @@ def deep_copy(config)
101121
# of ensuring that the default config is never mutated and is dup(ed)
102122
# before it is used in a request.
103123
def default_config
124+
return @noop_config if disabled?
104125
unless defined?(@default_config)
105126
raise NotYetConfiguredError, "Default policy not yet configured"
106127
end

spec/lib/secure_headers/configuration_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,25 @@ module SecureHeaders
119119
config = Configuration.dup
120120
expect(config.cookies).to eq({ httponly: true, secure: true, samesite: { lax: false } })
121121
end
122+
123+
describe ".disable!" do
124+
it "disables secure_headers completely" do
125+
Configuration.disable!
126+
expect(Configuration.disabled?).to be true
127+
end
128+
129+
it "returns a noop config when disabled" do
130+
Configuration.disable!
131+
config = Configuration.send(:default_config)
132+
Configuration::CONFIG_ATTRIBUTES.each do |attr|
133+
expect(config.instance_variable_get("@#{attr}")).to eq(OPT_OUT)
134+
end
135+
end
136+
137+
it "does not raise NotYetConfiguredError when disabled without default config" do
138+
Configuration.disable!
139+
expect { Configuration.send(:default_config) }.not_to raise_error
140+
end
141+
end
122142
end
123143
end

spec/lib/secure_headers/middleware_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,34 @@ module SecureHeaders
113113
expect(env["Set-Cookie"]).to eq("foo=bar; secure")
114114
end
115115
end
116+
117+
context "when disabled" do
118+
before(:each) do
119+
reset_config
120+
Configuration.disable!
121+
end
122+
123+
it "does not set any headers" do
124+
_, env = middleware.call(Rack::MockRequest.env_for("https://looocalhost", {}))
125+
126+
# Check individual header classes that have HEADER_NAME
127+
expect(env[XFrameOptions::HEADER_NAME]).to be_nil
128+
expect(env[XContentTypeOptions::HEADER_NAME]).to be_nil
129+
expect(env[XDownloadOptions::HEADER_NAME]).to be_nil
130+
expect(env[XPermittedCrossDomainPolicies::HEADER_NAME]).to be_nil
131+
expect(env[XXssProtection::HEADER_NAME]).to be_nil
132+
expect(env[StrictTransportSecurity::HEADER_NAME]).to be_nil
133+
expect(env[ReferrerPolicy::HEADER_NAME]).to be_nil
134+
expect(env[ContentSecurityPolicyConfig::HEADER_NAME]).to be_nil
135+
expect(env[ContentSecurityPolicyReportOnlyConfig::HEADER_NAME]).to be_nil
136+
expect(env[ClearSiteData::HEADER_NAME]).to be_nil
137+
expect(env[ExpectCertificateTransparency::HEADER_NAME]).to be_nil
138+
end
139+
140+
it "does not flag cookies" do
141+
_, env = cookie_middleware.call(Rack::MockRequest.env_for("https://looocalhost", {}))
142+
expect(env["Set-Cookie"]).to eq("foo=bar")
143+
end
144+
end
116145
end
117146
end

spec/lib/secure_headers_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ module SecureHeaders
112112
expect(hash.count).to eq(0)
113113
end
114114

115+
it "allows you to disable secure_headers entirely via Configuration.disable!" do
116+
Configuration.disable!
117+
hash = SecureHeaders.header_hash_for(request)
118+
expect(hash.count).to eq(0)
119+
end
120+
115121
it "allows you to override x-frame-options settings" do
116122
Configuration.default
117123
SecureHeaders.override_x_frame_options(request, XFrameOptions::DENY)

spec/spec_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,17 @@ def self.clear_overrides
5252
def self.clear_appends
5353
remove_instance_variable(:@appends) if defined?(@appends)
5454
end
55+
56+
def self.clear_disabled
57+
remove_instance_variable(:@disabled) if defined?(@disabled)
58+
remove_instance_variable(:@noop_config) if defined?(@noop_config)
59+
end
5560
end
5661
end
5762

5863
def reset_config
5964
SecureHeaders::Configuration.clear_default_config
6065
SecureHeaders::Configuration.clear_overrides
6166
SecureHeaders::Configuration.clear_appends
67+
SecureHeaders::Configuration.clear_disabled
6268
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby3.2
2+
#
3+
# This file was generated by RubyGems.
4+
#
5+
# The application 'guard' is installed as part of a gem, and
6+
# this file is here to facilitate running it.
7+
#
8+
9+
require 'rubygems'
10+
11+
Gem.use_gemdeps
12+
13+
version = ">= 0.a"
14+
15+
str = ARGV.first
16+
if str
17+
str = str.b[/\A_(.*)_\z/, 1]
18+
if str and Gem::Version.correct?(str)
19+
version = str
20+
ARGV.shift
21+
end
22+
end
23+
24+
if Gem.respond_to?(:activate_bin_path)
25+
load Gem.activate_bin_path('guard', '_guard-core', version)
26+
else
27+
gem "guard", version
28+
load Gem.bin_path("guard", "_guard-core", version)
29+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby3.2
2+
#
3+
# This file was generated by RubyGems.
4+
#
5+
# The application 'coderay' is installed as part of a gem, and
6+
# this file is here to facilitate running it.
7+
#
8+
9+
require 'rubygems'
10+
11+
Gem.use_gemdeps
12+
13+
version = ">= 0.a"
14+
15+
str = ARGV.first
16+
if str
17+
str = str.b[/\A_(.*)_\z/, 1]
18+
if str and Gem::Version.correct?(str)
19+
version = str
20+
ARGV.shift
21+
end
22+
end
23+
24+
if Gem.respond_to?(:activate_bin_path)
25+
load Gem.activate_bin_path('coderay', 'coderay', version)
26+
else
27+
gem "coderay", version
28+
load Gem.bin_path("coderay", "coderay", version)
29+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby3.2
2+
#
3+
# This file was generated by RubyGems.
4+
#
5+
# The application 'coveralls' is installed as part of a gem, and
6+
# this file is here to facilitate running it.
7+
#
8+
9+
require 'rubygems'
10+
11+
Gem.use_gemdeps
12+
13+
version = ">= 0.a"
14+
15+
str = ARGV.first
16+
if str
17+
str = str.b[/\A_(.*)_\z/, 1]
18+
if str and Gem::Version.correct?(str)
19+
version = str
20+
ARGV.shift
21+
end
22+
end
23+
24+
if Gem.respond_to?(:activate_bin_path)
25+
load Gem.activate_bin_path('coveralls', 'coveralls', version)
26+
else
27+
gem "coveralls", version
28+
load Gem.bin_path("coveralls", "coveralls", version)
29+
end

vendor/bundle/ruby/3.2.0/bin/guard

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby3.2
2+
#
3+
# This file was generated by RubyGems.
4+
#
5+
# The application 'guard' is installed as part of a gem, and
6+
# this file is here to facilitate running it.
7+
#
8+
9+
require 'rubygems'
10+
11+
Gem.use_gemdeps
12+
13+
version = ">= 0.a"
14+
15+
str = ARGV.first
16+
if str
17+
str = str.b[/\A_(.*)_\z/, 1]
18+
if str and Gem::Version.correct?(str)
19+
version = str
20+
ARGV.shift
21+
end
22+
end
23+
24+
if Gem.respond_to?(:activate_bin_path)
25+
load Gem.activate_bin_path('guard', 'guard', version)
26+
else
27+
gem "guard", version
28+
load Gem.bin_path("guard", "guard", version)
29+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env ruby3.2
2+
#
3+
# This file was generated by RubyGems.
4+
#
5+
# The application 'diff-lcs' is installed as part of a gem, and
6+
# this file is here to facilitate running it.
7+
#
8+
9+
require 'rubygems'
10+
11+
Gem.use_gemdeps
12+
13+
version = ">= 0.a"
14+
15+
str = ARGV.first
16+
if str
17+
str = str.b[/\A_(.*)_\z/, 1]
18+
if str and Gem::Version.correct?(str)
19+
version = str
20+
ARGV.shift
21+
end
22+
end
23+
24+
if Gem.respond_to?(:activate_bin_path)
25+
load Gem.activate_bin_path('diff-lcs', 'htmldiff', version)
26+
else
27+
gem "diff-lcs", version
28+
load Gem.bin_path("diff-lcs", "htmldiff", version)
29+
end

0 commit comments

Comments
 (0)