@@ -17,7 +17,8 @@ module SecureHeaders
1717 FIREFOX_23 = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:23.0) Gecko/20131011 Firefox/23.0"
1818 CHROME = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4"
1919 CHROME_25 = "Mozilla/5.0 (Macintosh; Intel Mac OS X 1084) AppleWebKit/537.22 (KHTML like Gecko) Chrome/25.0.1364.99 Safari/537.22"
20-
20+ SAFARI = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A"
21+ OPERA = "Opera/9.80 (X11; Linux i686; Ubuntu/14.10) Presto/2.12.388 Version/12.16"
2122
2223 def request_for user_agent , request_uri = nil , options = { :ssl => false }
2324 double ( :ssl? => options [ :ssl ] , :env => { 'HTTP_USER_AGENT' => user_agent } , :url => ( request_uri || 'http://areallylongdomainexample.com' ) )
@@ -76,7 +77,7 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
7677 end
7778
7879 it "adds a @enforce and @app_name variables to the report uri" do
79- opts = @opts . merge ( :tag_report_uri => true , :enforce => true , :app_name => lambda { 'twitter' } )
80+ opts = @opts . merge ( :tag_report_uri => true , :enforce => true , :app_name => proc { 'twitter' } )
8081 csp = ContentSecurityPolicy . new ( opts , :request => request_for ( CHROME ) )
8182 expect ( csp . value ) . to include ( "/csp_report?enforce=true&app_name=twitter" )
8283 end
@@ -90,7 +91,7 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
9091 it "accepts procs for report-uris" do
9192 opts = {
9293 :default_src => 'self' ,
93- :report_uri => lambda { "http://lambda/result" }
94+ :report_uri => proc { "http://lambda/result" }
9495 }
9596
9697 csp = ContentSecurityPolicy . new ( opts )
@@ -99,15 +100,29 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
99100
100101 it "accepts procs for other fields" do
101102 opts = {
102- :default_src => lambda { "http://lambda/result" } ,
103- :enforce => lambda { true } ,
104- :disable_fill_missing => lambda { true }
103+ :default_src => proc { "http://lambda/result" } ,
104+ :enforce => proc { true } ,
105+ :disable_fill_missing => proc { true }
105106 }
106107
107108 csp = ContentSecurityPolicy . new ( opts )
108109 expect ( csp . value ) . to eq ( "default-src http://lambda/result; img-src http://lambda/result data:;" )
109110 expect ( csp . name ) . to match ( "Content-Security-Policy" )
110111 end
112+
113+ it "passes a reference to the controller to the proc" do
114+ controller = double
115+ user = double ( :beta_testing? => true )
116+
117+ allow ( controller ) . to receive ( :current_user ) . and_return ( user )
118+ opts = {
119+ :disable_fill_missing => true ,
120+ :default_src => "self" ,
121+ :enforce => lambda { |c | c . current_user . beta_testing? }
122+ }
123+ csp = ContentSecurityPolicy . new ( opts , :controller => controller )
124+ expect ( csp . name ) . to match ( "Content-Security-Policy" )
125+ end
111126 end
112127 end
113128
@@ -170,11 +185,33 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
170185 end
171186
172187 context "when using a nonce" do
173- it "adds a nonce and unsafe-inline to the script-src value" do
188+ it "adds a nonce and unsafe-inline to the script-src value when using chrome " do
174189 header = ContentSecurityPolicy . new ( default_opts . merge ( :script_src => "self nonce" ) , :request => request_for ( CHROME ) )
175190 expect ( header . value ) . to include ( "script-src 'self' 'nonce-#{ header . nonce } ' 'unsafe-inline'" )
176191 end
177192
193+ it "adds a nonce and unsafe-inline to the script-src value when using firefox" do
194+ header = ContentSecurityPolicy . new ( default_opts . merge ( :script_src => "self nonce" ) , :request => request_for ( FIREFOX ) )
195+ expect ( header . value ) . to include ( "script-src 'self' 'nonce-#{ header . nonce } ' 'unsafe-inline'" )
196+ end
197+
198+ it "adds a nonce and unsafe-inline to the script-src value when using opera" do
199+ header = ContentSecurityPolicy . new ( default_opts . merge ( :script_src => "self nonce" ) , :request => request_for ( OPERA ) )
200+ expect ( header . value ) . to include ( "script-src 'self' 'nonce-#{ header . nonce } ' 'unsafe-inline'" )
201+ end
202+
203+ it "does not add a nonce and unsafe-inline to the script-src value when using Safari" do
204+ header = ContentSecurityPolicy . new ( default_opts . merge ( :script_src => "self nonce" ) , :request => request_for ( SAFARI ) )
205+ expect ( header . value ) . to include ( "script-src 'self' 'unsafe-inline'" )
206+ expect ( header . value ) . not_to include ( "nonce" )
207+ end
208+
209+ it "does not add a nonce and unsafe-inline to the script-src value when using IE" do
210+ header = ContentSecurityPolicy . new ( default_opts . merge ( :script_src => "self nonce" ) , :request => request_for ( IE ) )
211+ expect ( header . value ) . to include ( "script-src 'self' 'unsafe-inline'" )
212+ expect ( header . value ) . not_to include ( "nonce" )
213+ end
214+
178215 it "adds a nonce and unsafe-inline to the style-src value" do
179216 header = ContentSecurityPolicy . new ( default_opts . merge ( :style_src => "self nonce" ) , :request => request_for ( CHROME ) )
180217 expect ( header . value ) . to include ( "style-src 'self' 'nonce-#{ header . nonce } ' 'unsafe-inline'" )
0 commit comments