Skip to content

Commit 6ffcee7

Browse files
keithamusdgreif
andcommitted
fix: allow URIs with schema to have trailing slashes normalised
Co-authored-by: Dusty Greif <[email protected]>
1 parent 0d44152 commit 6ffcee7

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/secure_headers/headers/content_security_policy.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ def normalize_uri_paths(source_list)
152152
source_list.map do |source|
153153
# Normalize domains ending in a single / as without omitting the slash accomplisheg the same.
154154
# https://www.w3.org/TR/CSP3/#match-paths § 6.6.2.10 Step 2
155+
begin
156+
uri = URI(source)
157+
if uri.path == "/"
158+
next source.chomp("/")
159+
end
160+
rescue URI::InvalidURIError
161+
end
162+
155163
if source.chomp("/").include?("/")
156164
source
157165
else

spec/lib/secure_headers/headers/content_security_policy_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ module SecureHeaders
5050

5151
it "normalizes source expressions that end with a trailing /" do
5252
config = {
53-
default_src: %w(a.example.org/ b.example.com/ c.example.net/foo/ b.example.co/bar)
53+
default_src: %w(a.example.org/ b.example.com/ wss://c.example.com/ c.example.net/foo/ b.example.co/bar wss://b.example.co/)
5454
}
5555
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")
56+
expect(csp.value).to eq("default-src a.example.org b.example.com wss://c.example.com c.example.net/foo/ b.example.co/bar wss://b.example.co")
5757
end
5858

5959
it "minifies source expressions based on overlapping wildcards" do

0 commit comments

Comments
 (0)