File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -129,6 +129,7 @@ def minify_source_list(directive, source_list)
129129 else
130130 source_list = populate_nonces ( directive , source_list )
131131 source_list = reject_all_values_if_none ( source_list )
132+ source_list = normalize_uri_paths ( source_list )
132133
133134 unless directive == REPORT_URI || @preserve_schemes
134135 source_list = strip_source_schemes ( source_list )
@@ -151,6 +152,26 @@ def reject_all_values_if_none(source_list)
151152 end
152153 end
153154
155+ def normalize_uri_paths ( source_list )
156+ source_list . map do |source |
157+ # Normalize domains ending in a single / as without omitting the slash accomplishes the same.
158+ # https://www.w3.org/TR/CSP3/#match-paths § 6.6.2.10 Step 2
159+ begin
160+ uri = URI ( source )
161+ if uri . path == "/"
162+ next source . chomp ( "/" )
163+ end
164+ rescue URI ::InvalidURIError
165+ end
166+
167+ if source . chomp ( "/" ) . include? ( "/" )
168+ source
169+ else
170+ source . chomp ( "/" )
171+ end
172+ end
173+ end
174+
154175 # Private: append a nonce to the script/style directories if script_nonce
155176 # or style_nonce are provided.
156177 def populate_nonces ( directive , source_list )
Original file line number Diff line number Diff line change @@ -48,9 +48,17 @@ module SecureHeaders
4848 expect ( csp . value ) . to eq ( "default-src * 'unsafe-inline' 'unsafe-eval' data: blob:" )
4949 end
5050
51+ it "normalizes source expressions that end with a trailing /" do
52+ config = {
53+ default_src : %w( a.example.org/ b.example.com/ c.example.net/foo/ b.example.co/bar )
54+ }
55+ csp = ContentSecurityPolicy . new ( config )
56+ expect ( csp . value ) . to eq ( "default-src a.example.org b.example.com c.example.net/foo/ b.example.co/bar" )
57+ end
58+
5159 it "does not minify source expressions based on overlapping wildcards" do
5260 config = {
53- default_src : %w( a.example.org b.example.org *.example.org https://*.example.org )
61+ default_src : %w( a.example.org b.example.org *.example.org https://*.example.org c.example.org/ )
5462 }
5563 csp = ContentSecurityPolicy . new ( config )
5664 expect ( csp . value ) . to eq ( "default-src a.example.org b.example.org *.example.org" )
You can’t perform that action at this time.
0 commit comments