Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(location): navigate after rejected protocol replacements

ldm094c7b64f43b2
diff --git a/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs b/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
index b2e36341c..8ab299665 100644
--- a/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
+++ b/moli-renderer-v8/src/context_bootstrap/location_runtime/install.rs
@@ -429,10 +429,22 @@ fn location_writable_attribute_setter_callback<'s>(
let Ok(mut target) = url::Url::parse(&current_href) else {
return;
};
- // A syntactically valid scheme can still be incompatible with the
- // current URL (for example, changing an HTTP URL to `data`). URL's
- // scheme-state override treats that as a non-failing no-op.
- if target.set_scheme(&scheme).is_err() || !matches!(target.scheme(), "http" | "https") {
+ // Without credentials or a port, an HTTP(S)-to-file change would
+ // produce a non-HTTP(S) URL and stop here. Url::set_scheme rejects
+ // file changes more broadly (for any URL with an authority), so
+ // handle this non-navigating result before applying the scheme.
+ if scheme == "file"
+ && target.username().is_empty()
+ && target.password().is_none_or(str::is_empty)
+ && target.port().is_none()
+ {
+ return;
+ }
+ // Syntax was validated above. Rejected scheme replacements keep
+ // the original URL and are non-failing URL parser terminations;
+ // Location must still navigate if that URL remains HTTP(S).
+ let _ = target.set_scheme(&scheme);
+ if !matches!(target.scheme(), "http" | "https") {
return;
}
// Unlike the other component setters, protocol assignment must
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs
index f15f115eb..26b28cf8a 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/misc/extracted/events_errors.rs
@@ -854,36 +854,6 @@ fn location_protocol_setter_rejects_invalid_schemes_without_navigation() {
#[test]
fn location_protocol_setter_uses_scheme_state_override_navigation_rules() {
let original = "http://example.com/path?x=1#frag";
- let mut ignored_vm = new_storage_test_vm(original);
- let ignored = ignored_vm
- .eval(
- r#"
-(() => {
- const results = [];
- for (const scheme of ["x", "data", "file", "ftp", "http+x"]) {
- try {
- location.protocol = scheme;
- results.push(location.href);
- } catch (error) {
- results.push(error.name);
- }
- }
- return JSON.stringify(results);
-})()
-"#,
- )
- .expect("non-HTTP Location protocol probe should evaluate");
- assert_eq!(
- ignored,
- r#"["http://example.com/path?x=1#frag","http://example.com/path?x=1#frag","http://example.com/path?x=1#frag","http://example.com/path?x=1#frag","http://example.com/path?x=1#frag"]"#
- );
- assert!(
- ignored_vm
- .take_pending_location_navigation_with_seed()
- .is_none(),
- "non-HTTP(S) protocol results must not queue navigation"
- );
-
let mut same_vm = new_storage_test_vm(original);
same_vm
.eval(r#"location.protocol = "ht\ntp:gunk"; "queued""#)
@@ -910,3 +880,56 @@ fn location_protocol_setter_uses_scheme_state_override_navigation_rules() {
"https://example.com/path?x=1#frag"
);
}
+
+#[test]
+fn location_protocol_setter_navigates_after_rejected_scheme_changes() {
+ for (original, scheme) in [
+ ("http://example.com/path?x=1#frag", "data"),
+ ("http://example.com/path?x=1#frag", "x"),
+ ("http://example.com/path?x=1#frag", "http+x"),
+ ("https://example.com/path?x=1#frag", "data:gunk"),
+ ("http://example.com:8080/path?x=1#frag", "file"),
+ ("https://example.com:8443/path?x=1#frag", "file"),
+ ("http://[email protected]/path?x=1#frag", "file"),
+ ("http://:[email protected]/path?x=1#frag", "file"),
+ ] {
+ let mut vm = new_storage_test_vm(original);
+ let result = vm
+ .eval(&format!(
+ "location.protocol = {}; location.href",
+ serde_json::to_string(scheme).unwrap()
+ ))
+ .expect("incompatible scheme assignment should not throw");
+ assert_eq!(result, original, "{original} -> {scheme}");
+ let navigation = vm
+ .take_pending_location_navigation_with_seed()
+ .unwrap_or_else(|| panic!("{original} -> {scheme} must still navigate"));
+ assert_eq!(navigation.url.as_str(), original);
+ }
+}
+
+#[test]
+fn location_protocol_setter_does_not_navigate_non_http_results() {
+ for (original, scheme) in [
+ ("http://example.com/path?x=1#frag", "ftp"),
+ ("http://example.com/path?x=1#frag", "ws"),
+ ("http://example.com/path?x=1#frag", "wss"),
+ ("http://example.com/path?x=1#frag", "file"),
+ ("http://:@example.com/path?x=1#frag", "file"),
+ ("http://example.com:80/path?x=1#frag", "file"),
+ ("https://example.com:443/path?x=1#frag", "file"),
+ ("about:blank", "https"),
+ ("file:///path", "https"),
+ ] {
+ let mut vm = new_storage_test_vm(original);
+ vm.eval(&format!(
+ "location.protocol = {}",
+ serde_json::to_string(scheme).unwrap()
+ ))
+ .expect("valid scheme assignment should not throw");
+ assert!(
+ vm.take_pending_location_navigation_with_seed().is_none(),
+ "{original} -> {scheme} must not navigate"
+ );
+ }
+}
diff --git a/moli-wpt-compat/fixtures/wpt/manifest.toml b/moli-wpt-compat/fixtures/wpt/manifest.toml
index edaea2a33..88936a602 100644
--- a/moli-wpt-compat/fixtures/wpt/manifest.toml
+++ b/moli-wpt-compat/fixtures/wpt/manifest.toml
@@ -1358,7 +1358,7 @@ wait_until = "load"
timeout_ms = 5000
suite = "smoke"
tags = ["location"]
-notes = "Manual compat coverage for location.protocol failure boundaries: syntactically invalid schemes throw SyntaxError, while valid scheme-state overrides that do not produce an HTTP(S) URL leave the current URL and session history unchanged. Same-scheme and HTTP-to-HTTPS navigation are covered by renderer tests and upstream WPT."
+notes = "Manual compat coverage for location.protocol failure boundaries: invalid syntax throws SyntaxError, while rejected data/custom/file replacements reload a same-origin HTTP iframe at its unchanged URL. The file case uses the fixture's non-default port. Renderer tests also cover credentials, default ports, non-HTTP(S) results, same-scheme assignments and HTTP-to-HTTPS navigation."
[[test]]
id = "location-assign-fragment-history-basic"
diff --git a/moli-wpt-compat/fixtures/wpt/ported/location/location-protocol-noop-basic.html b/moli-wpt-compat/fixtures/wpt/ported/location/location-protocol-noop-basic.html
index 7685cff27..568e193ff 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/location/location-protocol-noop-basic.html
+++ b/moli-wpt-compat/fixtures/wpt/ported/location/location-protocol-noop-basic.html
@@ -3,6 +3,7 @@
<script src="/wpt/resources/testharness.js"></script>
<script src="/wpt/resources/testharnessreport.js"></script>
<script src="/wpt/resources/moli-wpt-adapter.js"></script>
+<body>
<script>
test(function () {
const initialHref = location.href;
@@ -17,12 +18,31 @@ test(function () {
);
}
- for (const ignored of ["data", "file", "ftp", "custom"]) {
- location.protocol = ignored;
- assert_equals(location.href, initialHref, `${ignored} must not navigate an HTTP document`);
- assert_equals(location.protocol, initialProtocol, `${ignored} must preserve the HTTP protocol`);
- }
+ assert_equals(location.href, initialHref, "invalid syntax must preserve the URL");
+ assert_equals(location.protocol, initialProtocol, "invalid syntax must preserve the protocol");
+ assert_equals(history.length, initialLength, "invalid syntax must not grow history");
+}, "location.protocol rejects invalid schemes without navigation");
+
+for (const scheme of ["data", "custom", "file"]) {
+ promise_test(async t => {
+ const iframe = document.createElement("iframe");
+ t.add_cleanup(() => iframe.remove());
+ iframe.src = "resources/location-protocol-target.html";
+ const loaded = new Promise(resolve => { iframe.onload = resolve; });
+ document.body.appendChild(iframe);
+ await loaded;
+
+ const originalDocument = iframe.contentDocument;
+ const originalHref = iframe.contentWindow.location.href;
+ if (scheme === "file") {
+ assert_not_equals(new URL(originalHref).port, "", "fixture uses a non-default port");
+ }
+ const reloaded = new Promise(resolve => { iframe.onload = resolve; });
+ iframe.contentWindow.location.protocol = scheme;
+ await reloaded;
- assert_equals(history.length, initialLength, "rejected and ignored assignments should not grow history");
-}, "location.protocol rejects invalid schemes and ignores non-HTTP(S) results");
+ assert_equals(iframe.contentWindow.location.href, originalHref, "scheme replacement preserves the URL");
+ assert_not_equals(iframe.contentDocument, originalDocument, "unchanged HTTP URL still navigates to a new Document");
+ }, `location.protocol = ${scheme} reloads an HTTP URL when scheme replacement is rejected`);
+}
</script>
diff --git a/moli-wpt-compat/fixtures/wpt/ported/location/resources/location-protocol-target.html b/moli-wpt-compat/fixtures/wpt/ported/location/resources/location-protocol-target.html
new file mode 100644
index 000000000..0dc57dff3
--- /dev/null
+++ b/moli-wpt-compat/fixtures/wpt/ported/location/resources/location-protocol-target.html
@@ -0,0 +1,3 @@
+<!doctype html>
+<meta charset="utf-8">
+<title>Location protocol navigation target</title>