Skip to content

Commit ccaa65c

Browse files
committed
Merge pull request #148 from twitter/pass-reference-to-controller
Pass reference to controller to CSP callable config values
2 parents df60b59 + abd16ca commit ccaa65c

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ This gem makes a few assumptions about how you will use some features. For exam
4949
config.x_permitted_cross_domain_policies = 'none'
5050
config.csp = {
5151
:default_src => "https: self",
52+
:enforce => proc {|controller| contoller.current_user.enforce_csp? }
5253
:frame_src => "https: http:.twimg.com http://itunes.apple.com",
5354
:img_src => "https:",
5455
:report_uri => '//example.com/uri-directive'

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def initialize(config=nil, options={})
106106

107107
# Config values can be string, array, or lamdba values
108108
@config = config.inject({}) do |hash, (key, value)|
109-
config_val = value.respond_to?(:call) ? value.call : value
109+
config_val = value.respond_to?(:call) ? value.call(@controller) : value
110110

111111
if SOURCE_DIRECTIVES.include?(key) # directives need to be normalized to arrays of strings
112112
config_val = config_val.split if config_val.is_a? String

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
7676
end
7777

7878
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' })
79+
opts = @opts.merge(:tag_report_uri => true, :enforce => true, :app_name => proc { 'twitter' })
8080
csp = ContentSecurityPolicy.new(opts, :request => request_for(CHROME))
8181
expect(csp.value).to include("/csp_report?enforce=true&app_name=twitter")
8282
end
@@ -90,7 +90,7 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
9090
it "accepts procs for report-uris" do
9191
opts = {
9292
:default_src => 'self',
93-
:report_uri => lambda { "http://lambda/result" }
93+
:report_uri => proc { "http://lambda/result" }
9494
}
9595

9696
csp = ContentSecurityPolicy.new(opts)
@@ -99,15 +99,29 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
9999

100100
it "accepts procs for other fields" do
101101
opts = {
102-
:default_src => lambda { "http://lambda/result" },
103-
:enforce => lambda { true },
104-
:disable_fill_missing => lambda { true }
102+
:default_src => proc { "http://lambda/result" },
103+
:enforce => proc { true },
104+
:disable_fill_missing => proc { true }
105105
}
106106

107107
csp = ContentSecurityPolicy.new(opts)
108108
expect(csp.value).to eq("default-src http://lambda/result; img-src http://lambda/result data:;")
109109
expect(csp.name).to match("Content-Security-Policy")
110110
end
111+
112+
it "passes a reference to the controller to the proc" do
113+
controller = double
114+
user = double(:beta_testing? => true)
115+
116+
allow(controller).to receive(:current_user).and_return(user)
117+
opts = {
118+
:disable_fill_missing => true,
119+
:default_src => "self",
120+
:enforce => lambda { |c| c.current_user.beta_testing? }
121+
}
122+
csp = ContentSecurityPolicy.new(opts, :controller => controller)
123+
expect(csp.name).to match("Content-Security-Policy")
124+
end
111125
end
112126
end
113127

0 commit comments

Comments
 (0)