File tree Expand file tree Collapse file tree 2 files changed +31
-6
lines changed
Expand file tree Collapse file tree 2 files changed +31
-6
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
7391end
You can’t perform that action at this time.
0 commit comments