Skip to content

Commit

Permalink
Handle null host with leading empty path fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
qsantos committed Mar 2, 2023
1 parent edeaea7 commit 22b510b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,16 @@ impl<'a> Parser<'a> {
self.serialization.push_str(path.trim_start_matches('/'));
}

// This prevents web+demo:/.//not-a-host/ or web+demo:/path/..//not-a-host/,
// when parsed and then serialized, from ending up as web+demo://not-a-host/
// (they end up as web+demo:/.//not-a-host/).
if !*has_host && self.serialization[path_start..].starts_with("//") {
// If url’s host is null, url does not have an opaque path,
// url’s path’s size is greater than 1, and url’s path[0] is the empty string,
// then append U+002F (/) followed by U+002E (.) to output.
self.serialization.insert_str(path_start, "/.");
}

input
}

Expand Down
10 changes: 10 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,16 @@ fn no_panic() {
url::quirks::set_hostname(&mut url, "//eom/datcom/\\\\t\\://eom/data.cs").unwrap();
}

#[test]
fn test_null_host_with_leading_empty_path_fragment() {
// since Note in item 3 of URL serializing in the URL Standard
// https://url.spec.whatwg.org/#url-serializing
let url = Url::parse("m:/.//\\").unwrap();
let encoded = url.as_str();
let reparsed = Url::parse(encoded).unwrap();
assert_eq!(reparsed, url);
}

#[test]
fn pop_if_empty_in_bounds() {
let mut url = Url::parse("m://").unwrap();
Expand Down

0 comments on commit 22b510b

Please sign in to comment.