fix(url): keep hierarchical paths distinct from authorities

Apply scheme-specific slash handling to relative URL resolution: consume all special authority slashes, but retain non-special backslashes as path data.

Share serialization-only path prefix adjustment across parsing and path mutation, preserve logical paths when adding or removing authorities, and keep host metadata and query/fragment offsets consistent. Distinguish empty authorities from missing hosts in pathname setters and remove the redundant Web API workaround.

Add seven parser/mutation regressions and two cross-realm URL/DOM regressions. Remove only the seven upstream expected failures proven fixed; retain the original WPT data.

Validation: cargo fmt --all; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo nextest run --no-fail-fast (17,624 passed, 13 skipped); upstream all-features unit/WPT/doctests.

Focused CLI/CDP WPT agree across 50 cases: 48 additional passing subtests, no new failures. CDP smoke passes 42/43 groups; Puppeteer cannot start because node is missing, unchanged from baseline.
This commit is contained in:
ldm0
2026-11-02 06:01:31 +08:00
parent a4cdf50d84
commit 78d2d670d3
9 changed files with 338 additions and 81 deletions
@@ -352,6 +352,77 @@ for (const tag of ['a', 'area']) {
);
}
#[test]
fn url_hierarchical_path_mutations_keep_hosts_and_logical_paths_separate() {
assert_url_components(
r#"
for (const input of ['custom:/old?q#f', 'custom:/.//old?q#f']) {
for (const value of ['//p', '/.//p', '/..//p']) {
checkSetter(input, 'pathname', value, 'custom:/.//p?q#f');
}
checkSetter(input, 'pathname', 'p', 'custom:/p?q#f');
checkSetter(input, 'pathname', '', 'custom:/?q#f');
}
checkSetter('custom:///old?q#f', 'pathname', '', 'custom://?q#f');
checkSetter('custom://host/old?q#f', 'pathname', '//p', 'custom://host//p?q#f');
checkSetter('custom:/.//p?q#f', 'host', 'h:77', 'custom://h:77//p?q#f');
for (const [name, create] of factories) {
for (const hostname of ['h', '', '[::1]']) {
const object = create('custom:/.//p?q#f');
assert(object.pathname === '//p', name + ': prefix is not part of pathname');
object.hostname = hostname;
assert(object.href === 'custom://' + hostname + '//p?q#f', name + ': add authority');
assert(object.pathname === '//p', name + ': authority change preserves path');
assert(object.search === '?q' && object.hash === '#f', name + ': suffix offsets');
assert(new URL(object.href).pathname === '//p', name + ': serialized URL round-trips');
object.pathname = '';
assert(object.href === 'custom://' + hostname + '?q#f', name + ': empty authority path');
}
}
"#,
);
}
#[test]
fn url_relative_resolution_distinguishes_special_slashes_and_non_special_backslashes() {
assert_url_components(
r#"
for (const [input, base, expected] of [
['///next.test/a/../p?q#f', 'https://user:pass@old.test:123/a/b', 'https://next.test/p?q#f'],
['///\\//\\//next.test/p', 'http://old.test/a/b', 'http://next.test/p'],
['\\p', 'custom://host/a/b', 'custom://host/a/\\p'],
['\\/p', 'custom://host/a/b', 'custom://host/a/\\/p'],
['\\\\p', 'custom://host/a/b', 'custom://host/a/\\\\p'],
['\\p', 'custom:/.//a/b', 'custom:/.//a/\\p'],
['///next', 'custom://host/a/b', 'custom:///next'],
['////next', 'custom:/a/b', 'custom:////next'],
]) {
for (const constructor of [URL, frame.contentWindow.URL]) {
assert(new constructor(input, base).href === expected, 'relative constructor: ' + input);
assert(constructor.parse(input, base).href === expected, 'relative URL.parse: ' + input);
assert(constructor.canParse(input, base), 'relative URL.canParse: ' + input);
}
for (const owner of [document, frame.contentDocument,
document.implementation.createHTMLDocument('relative path')]) {
const baseElement = owner.head.appendChild(owner.createElement('base'));
baseElement.href = base;
for (const tag of ['a', 'area']) {
const link = owner.body.appendChild(owner.createElement(tag));
link.href = input;
assert(link.href === expected, tag + ': relative document base: ' + input);
link.remove();
}
baseElement.remove();
}
}
for (const constructor of [URL, frame.contentWindow.URL]) {
assert(constructor.parse('///?q', 'https://host/') === null, 'empty special host fails');
assert(!constructor.canParse('//\\host', 'custom://base/'), 'backslash in opaque host fails');
}
"#,
);
}
#[test]
fn file_url_components_preserve_hosts_drive_letters_and_empty_path_segments() {
assert_url_components(
+1 -7
View File
@@ -47,13 +47,7 @@ pub fn set_pathname(url: &mut Url, value: &str) {
return;
}
let value = strip_tabs_and_newlines(value);
// rust-url represents an empty host like a missing host. An authority
// distinguishes them: non-special URLs with an empty host can have no path.
if value.is_empty() && url.has_authority() {
url.set_path(&value);
} else {
url::quirks::set_pathname(url, &value);
}
url::quirks::set_pathname(url, &value);
}
fn strip_tabs_and_newlines(value: &str) -> Cow<'_, str> {
+168
View File
@@ -0,0 +1,168 @@
use url::Url;
fn assert_url(url: &Url, expected: &str, path: &str) {
assert_eq!(url.as_str(), expected);
assert_eq!(url.path(), path);
url.check_invariants().unwrap();
assert_eq!(Url::parse(url.as_str()).unwrap(), *url);
}
#[test]
fn relative_special_urls_skip_all_authority_slashes() {
for scheme in ["http", "https", "ws", "wss", "ftp"] {
let base = Url::parse(&format!("{scheme}://user:pass@old.test:123/a/b?q#f")).unwrap();
for prefix in ["//", "///", r"///\//\//", r"\\", r"/\", r"\/", "/\t/\n/"] {
let url = base
.join(&format!("{prefix}new.test/a/../p?next#frag"))
.unwrap();
assert_url(&url, &format!("{scheme}://new.test/p?next#frag"), "/p");
assert_eq!(url.host_str(), Some("new.test"));
assert_eq!(url.port(), None);
assert!(url.username().is_empty());
assert_eq!(url.password(), None);
}
for input in ["///", r"/\\", "////?query", r"\\#fragment"] {
assert!(base.join(input).is_err(), "{scheme}: {input:?}");
}
for input in ["/p", r"\p"] {
assert_url(
&base.join(input).unwrap(),
&format!("{scheme}://user:pass@old.test:123/p"),
"/p",
);
}
}
}
#[test]
fn relative_non_special_urls_keep_backslashes_in_the_path() {
for (base, prefix) in [
("custom://host/a/b?q#f", "custom://host/a/"),
("custom:/a/b?q#f", "custom:/a/"),
("custom:/.//a/b?q#f", "custom:/.//a/"),
] {
let base = Url::parse(base).unwrap();
for input in [r"\p", r"\/p", r"\\p"] {
let expected = format!("{prefix}{input}");
let url = base.join(input).unwrap();
assert_eq!(url.as_str(), expected);
url.check_invariants().unwrap();
assert_eq!(url.has_authority(), base.has_authority());
assert_eq!(url.host_str(), base.host_str());
}
for (input, expected, path) in [
("//next", "custom://next", ""),
("///next", "custom:///next", "/next"),
("////next", "custom:////next", "//next"),
] {
assert_url(&base.join(input).unwrap(), expected, path);
}
assert!(base.join(r"//\next").is_err());
}
}
#[test]
fn hierarchical_path_setters_add_and_remove_only_the_serialization_prefix() {
for input in ["custom:/old?q#f", "custom:/.//old?q#f"] {
for (value, expected, path) in [
("//p", "custom:/.//p?q#f", "//p"),
("/.//p", "custom:/.//p?q#f", "//p"),
("/..//p", "custom:/.//p?q#f", "//p"),
("//", "custom:/.//?q#f", "//"),
("p", "custom:/p?q#f", "/p"),
("/", "custom:/?q#f", "/"),
] {
let mut url = Url::parse(input).unwrap();
url.set_path(value);
assert_url(&url, expected, path);
assert!(!url.has_authority());
assert_eq!(url.query(), Some("q"));
assert_eq!(url.fragment(), Some("f"));
}
let mut url = Url::parse(input).unwrap();
crate::components::set_pathname(&mut url, "");
assert_url(&url, "custom:/?q#f", "/");
}
for authority in ["", "host", "user:pass@host:123"] {
let mut url = Url::parse(&format!("custom://{authority}/old?q#f")).unwrap();
url.set_path("//p");
assert_url(&url, &format!("custom://{authority}//p?q#f"), "//p");
assert!(url.has_authority());
}
}
#[test]
fn adding_a_host_drops_the_path_serialization_prefix_without_changing_the_path() {
for input in ["custom:/.//p?q#f", "custom:/.//?q#f"] {
for host in ["h", "", "[::1]"] {
let mut url = Url::parse(input).unwrap();
let path = url.path().to_owned();
crate::components::set_hostname(&mut url, host);
assert_url(&url, &format!("custom://{host}{path}?q#f"), &path);
assert!(url.has_authority());
assert_eq!(url.query(), Some("q"));
assert_eq!(url.fragment(), Some("f"));
}
let mut url = Url::parse(input).unwrap();
let path = url.path().to_owned();
crate::components::set_host(&mut url, "h:77");
assert_url(&url, &format!("custom://h:77{path}?q#f"), &path);
assert_eq!(url.port(), Some(77));
}
}
#[test]
fn removing_a_host_keeps_leading_empty_segments_out_of_the_authority() {
for authority in ["h", "", "[::1]", "user:pass@h:77"] {
for path in ["", "/p", "//p", "///"] {
let mut url = Url::parse(&format!("custom://{authority}{path}?q#f")).unwrap();
url.set_host(None).unwrap();
let path = if path.is_empty() { "/" } else { path };
let prefix = if path.starts_with("//") { "/." } else { "" };
assert_url(&url, &format!("custom:{prefix}{path}?q#f"), path);
assert!(!url.has_authority());
assert_eq!(url.host_str(), None);
assert_eq!(url.port(), None);
assert_eq!(url.query(), Some("q"));
assert_eq!(url.fragment(), Some("f"));
url.set_host(Some("next")).unwrap();
assert_url(&url, &format!("custom://next{path}?q#f"), path);
}
}
}
#[test]
fn empty_pathname_distinguishes_empty_hosts_from_missing_hosts() {
for (input, expected, path) in [
("custom:///p?q#f", "custom://?q#f", ""),
("custom://host/p?q#f", "custom://host?q#f", ""),
("custom:/p?q#f", "custom:/?q#f", "/"),
("custom:/.//p?q#f", "custom:/?q#f", "/"),
("https://host/p?q#f", "https://host/?q#f", "/"),
("file:///p?q#f", "file:///?q#f", "/"),
("data:payload?q#f", "data:payload?q#f", "payload"),
] {
let mut url = Url::parse(input).unwrap();
url::quirks::set_pathname(&mut url, "");
assert_url(&url, expected, path);
}
}
#[test]
fn path_segment_mutations_keep_the_path_prefix_and_component_offsets_consistent() {
let mut url = Url::parse("custom:/.//p?q#f").unwrap();
url.path_segments_mut().unwrap().pop();
assert_url(&url, "custom:/?q#f", "/");
url.set_path("//p");
url.path_segments_mut()
.unwrap()
.clear()
.push("next")
.push("");
assert_url(&url, "custom:/next/?q#f", "/next/");
url.set_path("///");
url.path_segments_mut().unwrap().pop_if_empty();
assert_url(&url, "custom:/.//?q#f", "//");
url.path_segments_mut().unwrap().pop_if_empty();
assert_url(&url, "custom:/?q#f", "/");
}
+2
View File
@@ -9,6 +9,8 @@ pub use origin::is_about_srcdoc;
#[cfg(test)]
mod file_url;
#[cfg(test)]
mod hierarchical_path;
pub use origin::{
WebOrigin, is_about_blank, is_opaque_origin, is_potentially_trustworthy_url,
+31 -7
View File
@@ -26,11 +26,33 @@ References:
<https://url.spec.whatwg.org/#path-state>, and
<https://url.spec.whatwg.org/#shorten-a-urls-path>.
The upstream WPT data is unchanged. The 39 file parsing/setter tests fixed by
this patch were removed from `tests/expected_failures.txt`; the eight remaining
upstream expected failures are retained. The obsolete unit-test expectation of
a host/drive-letter syntax violation is replaced with an explicit preservation
and no-violation assertion.
Hierarchical URL parsing and mutation also keep the authority distinct from
the logical path:
- In relative special URLs, consume all authority slashes before the host.
In non-special URLs, backslashes remain path data and only two leading
forward slashes introduce an authority.
- Share the serialization-only "/." prefix adjustment between parsing, path
replacement, and path-segment mutation. Keep query and fragment offsets in
sync when the prefix is added or removed.
- Discard that prefix when adding a host, and restore it when removing a host
from a URL whose logical path starts with "//". Host removal also clears the
internal host kind and preserves an empty hierarchical path with a slash.
- Distinguish a missing host from an empty authority when clearing pathname,
so the Web API adapter no longer needs its own empty-authority workaround.
References:
<https://url.spec.whatwg.org/#relative-state>,
<https://url.spec.whatwg.org/#relative-slash-state>,
<https://url.spec.whatwg.org/#special-authority-ignore-slashes-state>,
<https://url.spec.whatwg.org/#concept-url-serializer>, and
<https://url.spec.whatwg.org/#dom-url-pathname>.
The upstream WPT data is unchanged. The 39 file parsing/setter tests and seven
hierarchical setter tests fixed by these patches were removed from
`tests/expected_failures.txt`; the one remaining upstream expected failure is
retained. The obsolete unit-test expectation of a host/drive-letter syntax
violation is replaced with an explicit preservation and no-violation assertion.
Packaging adjustment: `debug_metadata/url.natvis` is copied from the same pinned
upstream revision, and its include path is made package-local so the
@@ -43,6 +65,8 @@ cargo test --manifest-path vendor/url-2.5.8/Cargo.toml --all-features
```
Workspace regressions live in `moli-url/src/file_url.rs`,
`moli-url/src/hierarchical_path.rs`,
`moli-renderer-v8/src/script_vm/tests/url_components.rs`, and
`moli-url-policy/src/tests.rs`. Non-file parser and setter conformance issues
remain separate work; this patch does not change resource access permissions.
`moli-url-policy/src/tests.rs`. Remaining opaque-path and percent-encoding
conformance issues are separate work; these patches do not change resource
access permissions.
+21 -5
View File
@@ -1797,6 +1797,9 @@ impl Url {
}
fn restore_after_path(&mut self, old_after_path_position: u32, after_path: &str) {
let scheme_end = self.scheme_end;
let path_start = self.path_start;
self.path_start = self.mutate(|parser| parser.adjust_path_prefix(scheme_end, path_start));
let new_after_path_position = to_u32(self.serialization.len()).unwrap();
let adjust = |index: &mut u32| {
*index -= old_after_path_position;
@@ -2044,11 +2047,17 @@ impl Url {
} else {
self.set_host_internal(Host::parse_opaque_cow(host_substr.into())?, None);
}
} else if self.has_host() {
} else if self.has_authority() {
if scheme_type.is_special() && !scheme_type.is_file() {
return Err(ParseError::EmptyHost);
} else if self.serialization.len() == self.path_start as usize {
self.serialization.push('/');
} else if self.path().is_empty() {
self.serialization.insert(self.path_start as usize, '/');
if let Some(ref mut index) = self.query_start {
*index += 1;
}
if let Some(ref mut index) = self.fragment_start {
*index += 1;
}
}
debug_assert!(self.byte_at(self.scheme_end) == b':');
debug_assert!(self.byte_at(self.path_start) == b'/');
@@ -2061,12 +2070,17 @@ impl Url {
self.serialization
.drain(new_path_start as usize..self.path_start as usize);
let offset = self.path_start - new_path_start;
let old_path_start = self.path_start;
self.path_start = new_path_start;
self.username_end = new_path_start;
self.host_start = new_path_start;
self.host_end = new_path_start;
self.host = HostInternal::None;
self.port = None;
let scheme_end = self.scheme_end;
self.path_start =
self.mutate(|parser| parser.adjust_path_prefix(scheme_end, new_path_start));
let offset = old_path_start - self.path_start;
if let Some(ref mut index) = self.query_start {
*index -= offset
}
@@ -2079,7 +2093,9 @@ impl Url {
/// opt_new_port: None means leave unchanged, Some(None) means remove any port number.
fn set_host_internal(&mut self, host: Host<Cow<'_, str>>, opt_new_port: Option<Option<u16>>) {
let old_suffix_pos = if opt_new_port.is_some() {
// When creating an authority, discard any serialization-only "/."
// prefix and retain the logical path, query, and fragment.
let old_suffix_pos = if opt_new_port.is_some() || !self.has_authority() {
self.path_start
} else {
self.host_end
+43 -54
View File
@@ -690,25 +690,33 @@ impl Parser<'_> {
})
}
Some('#') => self.fragment_only(base_url, input),
Some('/') | Some('\\') => {
let (slashes_count, remaining) = input.count_matching(|c| matches!(c, '/' | '\\'));
Some(c) if c == '/' || (c == '\\' && scheme_type.is_special()) => {
let (slashes_count, remaining) =
input.count_matching(|c| c == '/' || (c == '\\' && scheme_type.is_special()));
if slashes_count >= 2 {
self.log_violation_if(SyntaxViolation::ExpectedDoubleSlash, || {
input
.clone()
.take_while(|&c| matches!(c, '/' | '\\'))
.collect::<String>()
!= "//"
});
let remaining = if scheme_type.is_special() {
self.log_violation_if(SyntaxViolation::ExpectedDoubleSlash, || {
input
.clone()
.take_while(|&c| matches!(c, '/' | '\\'))
.collect::<String>()
!= "//"
});
// Special authority ignore slashes state consumes every
// leading slash or backslash before parsing the host.
remaining
} else {
// A non-special authority consumes exactly two slashes;
// further slashes belong to its path, not its host.
input_after_first_char.split_prefix('/').unwrap()
};
let scheme_end = base_url.scheme_end;
debug_assert!(base_url.byte_at(scheme_end) == b':');
self.serialization
.push_str(base_url.slice(..scheme_end + 1));
if let Some(after_prefix) = input.split_prefix("//") {
return self.after_double_slash(after_prefix, scheme_type, scheme_end);
}
return self.after_double_slash(remaining, scheme_type, scheme_end);
}
self.log_violation_if(SyntaxViolation::Backslash, || c == '\\');
let path_start = base_url.path_start;
self.serialization.push_str(base_url.slice(..path_start));
self.serialization.push('/');
@@ -1341,6 +1349,27 @@ impl Parser<'_> {
}
}
/// Keep a hostless hierarchical path beginning with "//" distinct from an
/// authority. The "/." prefix belongs to serialization, never to path().
/// Callers must keep query and fragment offsets in sync with the new offset.
pub fn adjust_path_prefix(&mut self, scheme_end: u32, path_start: u32) -> u32 {
let prefix_start = scheme_end as usize + 1;
let path_start_usize = path_start as usize;
let needs_prefix = self.serialization[path_start_usize..].starts_with("//");
if path_start_usize == prefix_start && needs_prefix {
self.serialization.insert_str(prefix_start, "/.");
path_start + 2
} else if path_start_usize == prefix_start + 2
&& &self.serialization[prefix_start..path_start_usize] == "/."
&& !needs_prefix
{
self.serialization.drain(prefix_start..path_start_usize);
path_start - 2
} else {
path_start
}
}
#[allow(clippy::too_many_arguments)]
fn with_query_and_fragment(
mut self,
@@ -1351,50 +1380,10 @@ impl Parser<'_> {
host_end: u32,
host: HostInternal,
port: Option<u16>,
mut path_start: u32,
path_start: u32,
remaining: Input<'_>,
) -> ParseResult<Url> {
// Special case for anarchist URL's with a leading empty path segment
// 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 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.
let scheme_end_as_usize = scheme_end as usize;
let path_start_as_usize = path_start as usize;
if path_start_as_usize == scheme_end_as_usize + 1 {
// Anarchist URL
if self.serialization[path_start_as_usize..].starts_with("//") {
// Case 1: The base URL did not have an empty path segment, but the resulting one does
// Insert the "/." prefix
self.serialization.insert_str(path_start_as_usize, "/.");
path_start += 2;
}
assert!(!self.serialization[scheme_end_as_usize..].starts_with("://"));
} else if path_start_as_usize == scheme_end_as_usize + 3
&& &self.serialization[scheme_end_as_usize..path_start_as_usize] == ":/."
{
// Anarchist URL with leading empty path segment
// The base URL has a "/." between the host and the path
assert_eq!(self.serialization.as_bytes()[path_start_as_usize], b'/');
if self
.serialization
.as_bytes()
.get(path_start_as_usize + 1)
.copied()
!= Some(b'/')
{
// Case 2: The base URL had an empty path segment, but the resulting one does not
// Remove the "/." prefix
self.serialization
.replace_range(scheme_end_as_usize..path_start_as_usize, ":");
path_start -= 2;
}
assert!(!self.serialization[scheme_end_as_usize..].starts_with("://"));
}
let path_start = self.adjust_path_prefix(scheme_end, path_start);
let (query_start, fragment_start) =
self.parse_query_and_fragment(scheme_type, scheme_end, remaining)?;
Ok(Url {
+1 -1
View File
@@ -286,7 +286,7 @@ pub fn set_pathname(url: &mut Url, new_pathname: &str) {
url.set_path(new_pathname)
} else if SchemeType::from(url.scheme()).is_special()
|| !new_pathname.is_empty()
|| !url.has_host()
|| !url.has_authority()
{
let mut path_to_set = String::from("/");
path_to_set.push_str(new_pathname);
-7
View File
@@ -1,8 +1 @@
<non-spec:/.//p> set hostname to <h>
<non-spec:/.//p> set hostname to <>
<foo:///some/path> set pathname to <>
<file:///var/log/system.log> set href to <http://0300.168.0xF0>
<non-spec:/> set pathname to </.//p>
<non-spec:/> set pathname to </..//p>
<non-spec:/> set pathname to <//p>
<non-spec:/.//> set pathname to <p>