Skip to content

Avoid reparse issues with non-special URLs #459

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1035,12 +1035,21 @@ impl Url {
/// # run().unwrap();
/// ```
pub fn path(&self) -> &str {
match (self.query_start, self.fragment_start) {
let path = match (self.query_start, self.fragment_start) {
(None, None) => self.slice(self.path_start..),
(Some(next_component_start), _) |
(None, Some(next_component_start)) => {
self.slice(self.path_start..next_component_start)
}
};
// Disambiguating a path that starts with "//" from a URL with an authority may require
// the serializer to insert a disambiguating marker to the start of the path.
// When deserializing, "/./" is supposed to be reduced to "/", so avoid exposing it to
// the application.
if path.len() >= 3 && &path[..3] == "/./" {
&path[2..]
} else {
path
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,17 @@ impl<'a> Parser<'a> {
}
}
if ends_with_slash {
// This is a willfull violation of the URL specification for serialization.
//
// It aligns with the behaviour of Microsoft Edge,
// it does not affect the result of parsing (that's still compliant),
// and it's necessary to make URL reparsing idempotent.
//
// See the specification bug at https://github.com/whatwg/url/issues/415
if !*has_host && &self.serialization[path_start..] == "/" {
self.serialization.push('.');
self.serialization.push('/');
}
self.serialization.push('/')
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/urltestdata.json
Original file line number Diff line number Diff line change
Expand Up @@ -6144,5 +6144,20 @@
"pathname": "/test",
"search": "?a",
"hash": "#bc"
},
"Found by fuzzing",
{
"input": "a:/a/..//a",
"base": "about:blank",
"href": "a:/.//a",
"protocol": "a:",
"username": "",
"password": "",
"host": "",
"hostname": "",
"port": "",
"pathname": "//a",
"search": "",
"hash": ""
}
]