Skip to content

Commit d81007d

Browse files
Copilotfletchto99
andcommitted
Add Configuration.disable! to completely disable secure_headers
Co-authored-by: fletchto99 <718681+fletchto99@users.noreply.github.com>
1 parent b3557f7 commit d81007d

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
@@ -123,5 +123,34 @@ module SecureHeaders
123123
expect(env["Set-Cookie"]).to eq("foo=bar; secure")
124124
end
125125
end
126+
127+
context "when disabled" do
128+
before(:each) do
129+
reset_config
130+
Configuration.disable!
131+
end
132+
133+
it "does not set any headers" do
134+
_, env = middleware.call(Rack::MockRequest.env_for("https://looocalhost", {}))
135+
136+
# Check individual header classes that have HEADER_NAME
137+
expect(env[XFrameOptions::HEADER_NAME]).to be_nil
138+
expect(env[XContentTypeOptions::HEADER_NAME]).to be_nil
139+
expect(env[XDownloadOptions::HEADER_NAME]).to be_nil
140+
expect(env[XPermittedCrossDomainPolicies::HEADER_NAME]).to be_nil
141+
expect(env[XXssProtection::HEADER_NAME]).to be_nil
142+
expect(env[StrictTransportSecurity::HEADER_NAME]).to be_nil
143+
expect(env[ReferrerPolicy::HEADER_NAME]).to be_nil
144+
expect(env[ContentSecurityPolicyConfig::HEADER_NAME]).to be_nil
145+
expect(env[ContentSecurityPolicyReportOnlyConfig::HEADER_NAME]).to be_nil
146+
expect(env[ClearSiteData::HEADER_NAME]).to be_nil
147+
expect(env[ExpectCertificateTransparency::HEADER_NAME]).to be_nil
148+
end
149+
150+
it "does not flag cookies" do
151+
_, env = cookie_middleware.call(Rack::MockRequest.env_for("https://looocalhost", {}))
152+
expect(env["Set-Cookie"]).to eq("foo=bar")
153+
end
154+
end
126155
end
127156
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
@@ -54,11 +54,17 @@ def clear_appends
5454
remove_instance_variable(:@appends) if defined?(@appends)
5555
end
5656
end
57+
58+
def self.clear_disabled
59+
remove_instance_variable(:@disabled) if defined?(@disabled)
60+
remove_instance_variable(:@noop_config) if defined?(@noop_config)
61+
end
5762
end
5863
end
5964

6065
def reset_config
6166
SecureHeaders::Configuration.clear_default_config
6267
SecureHeaders::Configuration.clear_overrides
6368
SecureHeaders::Configuration.clear_appends
69+
SecureHeaders::Configuration.clear_disabled
6470
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)