Skip to content

Commit 86bd58f

Browse files
committed
Fix mutating path of URL without authority (idempotency, empty path segments)
1 parent 069718b commit 86bd58f

File tree

4 files changed

+103
-6
lines changed

4 files changed

+103
-6
lines changed

url/src/lib.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -2128,7 +2128,7 @@ impl Url {
21282128
} else {
21292129
self.host_end
21302130
};
2131-
let suffix = self.slice(old_suffix_pos..).to_owned();
2131+
let mut suffix = self.slice(old_suffix_pos..).to_owned();
21322132
self.serialization.truncate(self.host_start as usize);
21332133
if !self.has_authority() {
21342134
debug_assert!(self.slice(self.scheme_end..self.host_start) == ":");
@@ -2148,7 +2148,16 @@ impl Url {
21482148
write!(&mut self.serialization, ":{}", port).unwrap();
21492149
}
21502150
}
2151-
let new_suffix_pos = to_u32(self.serialization.len()).unwrap();
2151+
let mut new_suffix_pos = to_u32(self.serialization.len()).unwrap();
2152+
2153+
// Remove starting "/." for empty path segment followed by the host
2154+
if suffix.starts_with("/.//") {
2155+
let adjustment: usize = "/.".len();
2156+
suffix.drain(..adjustment);
2157+
// pathname should be "//p" not "p" given that the first segment was empty
2158+
new_suffix_pos -= adjustment as u32;
2159+
}
2160+
21522161
self.serialization.push_str(&suffix);
21532162

21542163
let adjust = |index: &mut u32| {

url/src/path_segments.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ impl PathSegmentsMut<'_> {
239239
I::Item: AsRef<str>,
240240
{
241241
let scheme_type = SchemeType::from(self.url.scheme());
242-
let path_start = self.url.path_start as usize;
242+
let mut path_start = self.url.path_start as usize;
243243
self.url.mutate(|parser| {
244244
parser.context = parser::Context::PathSegmentSetter;
245245
for segment in segments {
@@ -253,7 +253,44 @@ impl PathSegmentsMut<'_> {
253253
{
254254
parser.serialization.push('/');
255255
}
256-
let mut has_host = true; // FIXME account for this?
256+
257+
let mut path_empty = false;
258+
259+
// Check ':' and then see if the next character is '/'
260+
let mut has_host = if let Some(index) = parser.serialization.find(":") {
261+
if parser.serialization.len() > index + 1
262+
&& parser.serialization.as_bytes().get(index + 1) == Some(&b'/')
263+
{
264+
let rest = &parser.serialization[(index + ":/".len())..];
265+
let host_part = rest.split('/').next().unwrap_or("");
266+
path_empty = rest.is_empty();
267+
!host_part.is_empty() && !host_part.contains('@')
268+
} else {
269+
false
270+
}
271+
} else {
272+
false
273+
};
274+
275+
// For cases where normalization is applied across both the serialization and the path.
276+
// Append "/." immediately after the scheme (up to ":")
277+
// This is done if three conditions are met.
278+
// https://url.spec.whatwg.org/#url-serializing
279+
// 1. The host is null
280+
// 2. The url's path length is greater than 1
281+
// 3. the first segment of the URL's path is an empty string
282+
if !has_host && segment.len() > 1 && path_empty {
283+
if let Some(index) = parser.serialization.find(":") {
284+
if parser.serialization.len() == index + 2
285+
&& parser.serialization.as_bytes().get(index + 1) == Some(&b'/')
286+
{
287+
// Append an extra '/' to ensure that "/./path" becomes "/.//path"
288+
parser.serialization.insert_str(index + ":".len(), "/./");
289+
path_start += "/.".len();
290+
}
291+
}
292+
}
293+
257294
parser.parse_path(
258295
scheme_type,
259296
&mut has_host,
@@ -262,6 +299,7 @@ impl PathSegmentsMut<'_> {
262299
);
263300
}
264301
});
302+
self.url.path_start = path_start as u32;
265303
self
266304
}
267305
}

url/tests/expected_failures.txt

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
<file:/.//p>
3737
<http://example.net/path> set hostname to <example.com:8080>
3838
<http://example.net:8080/path> set hostname to <example.com:>
39-
<non-spec:/.//p> set hostname to <h>
40-
<non-spec:/.//p> set hostname to <>
4139
<foo:///some/path> set pathname to <>
4240
<file:///var/log/system.log> set href to <http://0300.168.0xF0>
4341
<file://monkey/> set pathname to <\\\\>

url/tests/unit.rs

+52
Original file line numberDiff line numberDiff line change
@@ -1427,3 +1427,55 @@ fn test_fuzzing_uri_failures() {
14271427
assert_eq!(url.as_str(), "web+demo:////.dummy.path");
14281428
url.check_invariants().unwrap();
14291429
}
1430+
1431+
#[test]
1432+
fn test_can_be_a_base_with_set_path() {
1433+
use url::quirks;
1434+
let mut url = Url::parse("web+demo:/").unwrap();
1435+
assert!(!url.cannot_be_a_base());
1436+
1437+
url.set_path("//not-a-host");
1438+
assert_eq!(url.path(), "//not-a-host");
1439+
1440+
let segments: Vec<_> = url
1441+
.path_segments()
1442+
.expect("should have path segments")
1443+
.collect();
1444+
1445+
assert_eq!(segments, vec!["", "not-a-host"]);
1446+
1447+
url.set_query(Some("query"));
1448+
url.set_fragment(Some("frag"));
1449+
1450+
assert_eq!(url.as_str(), "web+demo:/.//not-a-host?query#frag");
1451+
quirks::set_hostname(&mut url, "test").unwrap();
1452+
assert_eq!(url.as_str(), "web+demo://test//not-a-host?query#frag");
1453+
url.check_invariants().unwrap();
1454+
quirks::set_hostname(&mut url, "").unwrap();
1455+
assert_eq!(url.as_str(), "web+demo:////not-a-host?query#frag");
1456+
url.check_invariants().unwrap();
1457+
}
1458+
1459+
#[test]
1460+
fn test_can_be_a_base_with_path_segments_mut() {
1461+
let mut url = Url::parse("web+demo:/").unwrap();
1462+
assert!(!url.cannot_be_a_base());
1463+
1464+
url.path_segments_mut()
1465+
.expect("should have path segments")
1466+
.push("")
1467+
.push("not-a-host");
1468+
1469+
url.set_query(Some("query"));
1470+
url.set_fragment(Some("frag"));
1471+
1472+
assert_eq!(url.as_str(), "web+demo:/.//not-a-host?query#frag");
1473+
assert_eq!(url.path(), "//not-a-host");
1474+
url.check_invariants().unwrap();
1475+
1476+
let segments: Vec<_> = url
1477+
.path_segments()
1478+
.expect("should have path segments")
1479+
.collect();
1480+
assert_eq!(segments, vec!["", "not-a-host"]);
1481+
}

0 commit comments

Comments
 (0)