Skip to content

Commit 9b7083f

Browse files
committed
handle the case where only some config options are provided
1 parent 1fc6699 commit 9b7083f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/secure_headers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ def append_features(base)
5151

5252
def header_hash(options = nil)
5353
ALL_HEADER_CLASSES.inject({}) do |memo, klass|
54-
config = if options.is_a?(Hash)
54+
config = if options.is_a?(Hash) && options[klass::Constants::CONFIG_KEY]
5555
options[klass::Constants::CONFIG_KEY]
5656
else
5757
::SecureHeaders::Configuration.send(klass::Constants::CONFIG_KEY)
5858
end
59+
5960
header = get_a_header(klass::Constants::CONFIG_KEY, klass, config)
6061
memo[header.name] = header.value
6162
memo

spec/lib/secure_headers_spec.rb

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,40 @@ def set_security_headers(subject)
160160
end
161161

162162
describe "SecureHeaders#header_hash" do
163+
def expect_default_values(hash)
164+
expect(hash[XFO_HEADER_NAME]).to eq(SecureHeaders::XFrameOptions::Constants::DEFAULT_VALUE)
165+
expect(hash[XDO_HEADER_NAME]).to eq(SecureHeaders::XDownloadOptions::Constants::DEFAULT_VALUE)
166+
expect(hash[HSTS_HEADER_NAME]).to eq(SecureHeaders::StrictTransportSecurity::Constants::DEFAULT_VALUE)
167+
expect(hash[X_XSS_PROTECTION_HEADER_NAME]).to eq(SecureHeaders::XXssProtection::Constants::DEFAULT_VALUE)
168+
expect(hash[X_CONTENT_TYPE_OPTIONS_HEADER_NAME]).to eq(SecureHeaders::XContentTypeOptions::Constants::DEFAULT_VALUE)
169+
expect(hash[XPCDP_HEADER_NAME]).to eq(SecureHeaders::XPermittedCrossDomainPolicies::Constants::DEFAULT_VALUE)
170+
end
171+
163172
it "produces a hash of headers given a hash as config" do
164173
hash = SecureHeaders::header_hash(:csp => {:default_src => 'none', :img_src => "data:", :disable_fill_missing => true})
165174
expect(hash['Content-Security-Policy-Report-Only']).to eq("default-src 'none'; img-src data:;")
175+
expect_default_values(hash)
176+
end
177+
178+
it "produces a hash with a mix of config values, override values, and default values" do
179+
::SecureHeaders::Configuration.configure do |config|
180+
config.hsts = { :max_age => '123456'}
181+
end
182+
183+
hash = SecureHeaders::header_hash(:csp => {:default_src => 'none', :img_src => "data:", :disable_fill_missing => true})
184+
::SecureHeaders::Configuration.configure do |config|
185+
config.hsts = nil
186+
end
187+
188+
expect(hash['Content-Security-Policy-Report-Only']).to eq("default-src 'none'; img-src data:;")
189+
expect(hash[XFO_HEADER_NAME]).to eq(SecureHeaders::XFrameOptions::Constants::DEFAULT_VALUE)
190+
expect(hash[HSTS_HEADER_NAME]).to eq("max-age=123456")
166191
end
167192

168193
it "produces a hash of headers with default config" do
169194
hash = SecureHeaders::header_hash
170195
expect(hash['Content-Security-Policy-Report-Only']).to eq(SecureHeaders::ContentSecurityPolicy::Constants::DEFAULT_CSP_HEADER)
171-
expect(hash[XFO_HEADER_NAME]).to eq(SecureHeaders::XFrameOptions::Constants::DEFAULT_VALUE)
172-
expect(hash[XDO_HEADER_NAME]).to eq(SecureHeaders::XDownloadOptions::Constants::DEFAULT_VALUE)
173-
expect(hash[HSTS_HEADER_NAME]).to eq(SecureHeaders::StrictTransportSecurity::Constants::DEFAULT_VALUE)
174-
expect(hash[X_XSS_PROTECTION_HEADER_NAME]).to eq(SecureHeaders::XXssProtection::Constants::DEFAULT_VALUE)
175-
expect(hash[X_CONTENT_TYPE_OPTIONS_HEADER_NAME]).to eq(SecureHeaders::XContentTypeOptions::Constants::DEFAULT_VALUE)
176-
expect(hash[XPCDP_HEADER_NAME]).to eq(SecureHeaders::XPermittedCrossDomainPolicies::Constants::DEFAULT_VALUE)
196+
expect_default_values(hash)
177197
end
178198
end
179199

0 commit comments

Comments
 (0)