Skip to content

Commit e0e663a

Browse files
committed
Handle null host with leading empty path fragment
1 parent 6ef3876 commit e0e663a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

url/src/parser.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,16 @@ impl<'a> Parser<'a> {
12811281
self.serialization.push_str(path.trim_start_matches('/'));
12821282
}
12831283

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

url/tests/unit.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,17 @@ fn no_panic() {
954954
url::quirks::set_hostname(&mut url, "//eom/datcom/\\\\t\\://eom/data.cs").unwrap();
955955
}
956956

957+
#[test]
958+
fn test_null_host_with_leading_empty_path_fragment() {
959+
// since Note in item 3 of URL serializing in the URL Standard
960+
// https://url.spec.whatwg.org/#url-serializing
961+
let url = Url::parse("m:/.//\\").unwrap();
962+
let encoded = url.as_str();
963+
println!("{url:?}, {encoded:?}");
964+
let reparsed = Url::parse(encoded).unwrap();
965+
assert_eq!(reparsed, url);
966+
}
967+
957968
#[test]
958969
fn pop_if_empty_in_bounds() {
959970
let mut url = Url::parse("m://").unwrap();

0 commit comments

Comments
 (0)