-
Notifications
You must be signed in to change notification settings - Fork 254
normalize domains with trailing slashes #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,7 @@ def minify_source_list(directive, source_list) | |
else | ||
source_list = populate_nonces(directive, source_list) | ||
source_list = reject_all_values_if_none(source_list) | ||
source_list = normalize_uri_paths(source_list) | ||
|
||
unless directive == REPORT_URI || @preserve_schemes | ||
source_list = strip_source_schemes(source_list) | ||
|
@@ -147,6 +148,27 @@ def reject_all_values_if_none(source_list) | |
end | ||
end | ||
|
||
def normalize_uri_paths(source_list) | ||
source_list.map do |source| | ||
# Normalize domains ending in a single / as without omitting the slash accomplishes the same. | ||
# https://www.w3.org/TR/CSP3/#match-paths § 6.6.2.10 Step 2 | ||
begin | ||
uri = URI(source) | ||
if uri.path == "/" | ||
next source.chomp("/") | ||
end | ||
rescue URI::InvalidURIError | ||
end | ||
|
||
if source.chomp("/").include?("/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing this might not be normalizing is a CSP directive like The quoted spec is about matching on the It would be good to address this, with unit tests as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a test and some code to fix this. The code doesn't feel great, I'd welcome more elegant solutions here! |
||
source | ||
else | ||
source.chomp("/") | ||
end | ||
end | ||
end | ||
|
||
|
||
# Removes duplicates and sources that already match an existing wild card. | ||
# | ||
# e.g. *.github.com asdf.github.com becomes *.github.com | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keithamus I know you wrote this code several years ago, but do you remember what edge case we are looking catch here?
It seems like we might be able to get away with something like:
I believe
.chomp
will either remove the last char if it exists or return the original string unmodified.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don’t think that’s sufficient as it shouldn’t always remove the trailing
/
- only when the entire path is just/
. For example “example.com/foo/“ should remain as is, but “example.com/“ should be trimmed so it becomes “example.com”.The logic could be described as: if the entire contents of the URL after the domain name are “/“ then that is the equivalent of a URL with no path (so the contents after the domain name are “”).