Skip to content

Commit 651ee71

Browse files
committed
Merge pull request #102 from twitter/report_uri_tagging
add the ability to tag requests with report-only and app_name information
2 parents aa601ee + ea3f0a6 commit 651ee71

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ def configure(config)
6060
@config.merge!(experimental_config)
6161
end
6262

63-
# http_additions will be the only field that still doesn't support
64-
# lambdas because it's an ugly api that's showing it's age.
63+
# these values don't support lambdas because this needs to be rewritten
6564
@http_additions = @config.delete(:http_additions)
65+
@app_name = @config.delete(:app_name)
6666

6767
normalize_csp_options
6868

6969
META.each do |meta|
7070
self.send("#{meta}=", @config.delete(meta))
7171
end
7272

73-
@enforce = @config.delete(:enforce)
73+
@enforce = !!@config.delete(:enforce)
74+
@tag_report_uri = @config.delete(:tag_report_uri)
75+
7476
normalize_reporting_endpoint
7577
fill_directives unless disable_fill_missing?
7678
end
@@ -172,6 +174,11 @@ def normalize_reporting_endpoint
172174
@report_uri = FF_CSP_ENDPOINT
173175
end
174176
end
177+
178+
if @tag_report_uri
179+
@report_uri = "#{@report_uri}?enforce=#{@enforce}"
180+
@report_uri += "&app_name=#{@app_name}" if @app_name
181+
end
175182
end
176183

177184
def same_origin?

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ def request_for user_agent, request_uri=nil, options={:ssl => false}
7979
expect(csp.value).to include("script-src 'unsafe-inline' 'unsafe-eval' https://* data: 'self' 'none'")
8080
end
8181

82+
it "adds a @enforce and @app_name variables to the report uri" do
83+
opts = @opts.merge(:tag_report_uri => true, :enforce => true, :app_name => 'twitter')
84+
csp = ContentSecurityPolicy.new(opts, :request => request_for(CHROME))
85+
expect(csp.value).to include("/csp_report?enforce=true&app_name=twitter")
86+
end
87+
88+
it "does not add an empty @app_name variable to the report uri" do
89+
opts = @opts.merge(:tag_report_uri => true, :enforce => true)
90+
csp = ContentSecurityPolicy.new(opts, :request => request_for(CHROME))
91+
expect(csp.value).to include("/csp_report?enforce=true")
92+
end
93+
8294
it "accepts procs for report-uris" do
8395
opts = {
8496
:default_src => 'self',

0 commit comments

Comments
 (0)