Skip to content

Commit 1c13c9f

Browse files
authored
Merge pull request #371 from ptoomey3/master
Update Referrer-Policy to support multiple token values
2 parents f738658 + 0474176 commit 1c13c9f

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 5.0.2
2+
3+
- Updates `Referrer-Policy` header to support multiple policy values
4+
15
## 5.0.1
26

37
- Updates `Expect-CT` header to use a comma separator between directives, as specified in the most current spec.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ SecureHeaders::Configuration.default do |config|
7171
config.x_xss_protection = "1; mode=block"
7272
config.x_download_options = "noopen"
7373
config.x_permitted_cross_domain_policies = "none"
74-
config.referrer_policy = "origin-when-cross-origin"
74+
config.referrer_policy = %w(origin-when-cross-origin strict-origin-when-cross-origin)
7575
config.csp = {
7676
# "meta" values. these will shape the header, but the values are not included in the header.
7777
preserve_schemes: true, # default: false. Schemes are removed from host sources to save bytes and discourage mixed content.

lib/secure_headers/headers/referrer_policy.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@ class << self
2222
# Returns a default header if no configuration is provided, or a
2323
# header name and value based on the config.
2424
def make_header(config = nil)
25-
[HEADER_NAME, config || DEFAULT_VALUE]
25+
config ||= DEFAULT_VALUE
26+
[HEADER_NAME, Array(config).join(", ")]
2627
end
2728

2829
def validate_config!(config)
29-
return if config.nil? || config == OPT_OUT
30-
raise TypeError.new("Must be a string. Found #{config.class}: #{config}") unless config.is_a?(String)
31-
unless VALID_POLICIES.include?(config.downcase)
32-
raise ReferrerPolicyConfigError.new("Value can only be one of #{VALID_POLICIES.join(', ')}")
30+
case config
31+
when nil, OPT_OUT
32+
# valid
33+
when String, Array
34+
config = Array(config)
35+
unless config.all? { |t| t.is_a?(String) && VALID_POLICIES.include?(t.downcase) }
36+
raise ReferrerPolicyConfigError.new("Value can only be one or more of #{VALID_POLICIES.join(", ")}")
37+
end
38+
else
39+
raise TypeError.new("Must be a string or array of strings. Found #{config.class}: #{config}")
3340
end
3441
end
3542
end

spec/lib/secure_headers/headers/referrer_policy_spec.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module SecureHeaders
55
describe ReferrerPolicy do
66
specify { expect(ReferrerPolicy.make_header).to eq([ReferrerPolicy::HEADER_NAME, "origin-when-cross-origin"]) }
77
specify { expect(ReferrerPolicy.make_header("no-referrer")).to eq([ReferrerPolicy::HEADER_NAME, "no-referrer"]) }
8+
specify { expect(ReferrerPolicy.make_header(%w(origin-when-cross-origin strict-origin-when-cross-origin))).to eq([ReferrerPolicy::HEADER_NAME, "origin-when-cross-origin, strict-origin-when-cross-origin"]) }
89

910
context "valid configuration values" do
1011
it "accepts 'no-referrer'" do
@@ -60,14 +61,31 @@ module SecureHeaders
6061
ReferrerPolicy.validate_config!(nil)
6162
end.not_to raise_error
6263
end
64+
65+
it "accepts array of policy values" do
66+
expect do
67+
ReferrerPolicy.validate_config!(
68+
%w(
69+
origin-when-cross-origin
70+
strict-origin-when-cross-origin
71+
)
72+
)
73+
end.not_to raise_error
74+
end
6375
end
6476

65-
context "invlaid configuration values" do
77+
context "invalid configuration values" do
6678
it "doesn't accept invalid values" do
6779
expect do
6880
ReferrerPolicy.validate_config!("open")
6981
end.to raise_error(ReferrerPolicyConfigError)
7082
end
83+
84+
it "doesn't accept invalid types" do
85+
expect do
86+
ReferrerPolicy.validate_config!({})
87+
end.to raise_error(TypeError)
88+
end
7189
end
7290
end
7391
end

0 commit comments

Comments
 (0)