Skip to content

Commit 31eb926

Browse files
committed
Merge branch 'tests_for_csp_override_action'
2 parents 15917c4 + 865ff8b commit 31eb926

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

fixtures/rails_3_2_12_no_init/app/controllers/other_things_controller.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,18 @@ class OtherThingsController < ApplicationController
33
def index
44

55
end
6+
7+
def other_action
8+
render :text => 'yooooo'
9+
end
10+
11+
def secure_header_options_for(header, options)
12+
if params[:action] == "other_action"
13+
if header == :csp
14+
options.merge(:style_src => 'self')
15+
end
16+
else
17+
options
18+
end
19+
end
620
end

fixtures/rails_3_2_12_no_init/spec/controllers/other_things_controller_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
expect(response.headers['X-Frame-Options']).to eq(SecureHeaders::XFrameOptions::Constants::DEFAULT_VALUE)
1313
end
1414

15-
it "sets the X-WebKit-CSP header" do
15+
it "sets the CSP header" do
1616
get :index
1717
expect(response.headers['Content-Security-Policy-Report-Only']).to eq("default-src 'self'; img-src 'self' data:;")
1818
end
1919

20+
it "sets per-action values based on secure_header_options_for" do
21+
# munges :style_src => self into policy
22+
get :other_action
23+
expect(response.headers['Content-Security-Policy-Report-Only']).to eq("default-src 'self'; img-src 'self' data:; style-src 'self';")
24+
end
25+
2026
#mock ssl
2127
it "sets the Strict-Transport-Security header" do
2228
request.env['HTTPS'] = 'on'

fixtures/rails_4_1_8/app/controllers/other_things_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ class OtherThingsController < ApplicationController
22
def index
33

44
end
5-
end
5+
end

lib/secure_headers.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@ def ensure_security_headers options = {}
5050
before_filter :set_x_download_options_header
5151
before_filter :set_x_permitted_cross_domain_policies_header
5252
end
53-
54-
# we can't use ||= because I'm overloading false => disable, nil => default
55-
# both of which trigger the conditional assignment
56-
def options_for(type, options)
57-
options.nil? ? ::SecureHeaders::Configuration.send(type) : options
58-
end
5953
end
6054

6155
module InstanceMethods
@@ -80,7 +74,7 @@ def set_csp_header(req = nil, config=nil)
8074
end
8175

8276
config = self.class.secure_headers_options[:csp] if config.nil?
83-
config = self.class.options_for :csp, config
77+
config = secure_header_options_for :csp, config
8478

8579
return if config == false
8680

@@ -140,7 +134,7 @@ def set_hsts_header(options=self.class.secure_headers_options[:hsts])
140134

141135
def set_hpkp_header(options=self.class.secure_headers_options[:hpkp])
142136
return unless request.ssl?
143-
config = self.class.options_for :hpkp, options
137+
config = secure_header_options_for :hpkp, options
144138

145139
return if config == false || config.nil?
146140

@@ -158,8 +152,15 @@ def set_x_permitted_cross_domain_policies_header(options=self.class.secure_heade
158152

159153
private
160154

155+
# we can't use ||= because I'm overloading false => disable, nil => default
156+
# both of which trigger the conditional assignment
157+
def secure_header_options_for(type, options)
158+
options.nil? ? ::SecureHeaders::Configuration.send(type) : options
159+
end
160+
161+
161162
def set_a_header(name, klass, options=nil)
162-
options = self.class.options_for name, options
163+
options = secure_header_options_for name, options
163164
return if options == false
164165

165166
header = klass.new(options)

0 commit comments

Comments
 (0)