From 7cc8ae9f316dc2bc15b1bbae0a7c9105fcb0a1ff Mon Sep 17 00:00:00 2001 From: lanyue-llk <270302213+lanyue-llk@users.noreply.github.com> Date: Thu, 12 Nov 2026 19:44:18 +0700 Subject: [PATCH 1/2] fix(navigation): apply the document and link referrer policy to top-level navigations --- moli-core/tests/fetch/security.rs | 88 +++++++++++++++++++ .../src/native_bridge/context_host/core.rs | 1 + .../src/native_bridge/context_host/mod.rs | 3 + .../native_bridge/context_host/navigation.rs | 25 +++++- .../element/activation/default_action.rs | 37 ++++++++ .../runtime/page_vm/followed_navigation.rs | 22 ++++- 6 files changed, 173 insertions(+), 3 deletions(-) diff --git a/moli-core/tests/fetch/security.rs b/moli-core/tests/fetch/security.rs index 3ecf7f437cf..55e4294b2fa 100644 --- a/moli-core/tests/fetch/security.rs +++ b/moli-core/tests/fetch/security.rs @@ -104,6 +104,61 @@ fn fixture_response(request: &IncomingRequest) -> (u16, String, String) { } else { "Cache-Control: no-store\r\n" }; + if url.path() == "/referrer-nav" { + // A page whose script navigates to /nav-target?case=.. one way. + let case = url.query().unwrap_or_default().trim_start_matches("case="); + let target = format!("/nav-target?case={case}"); + let meta = |policy| format!(""); + let click = ""; + let (headers, body) = match case { + "meta-link" => ( + String::new(), + format!("{}go{click}", meta("no-referrer")), + ), + "header-link" => ( + "Referrer-Policy: no-referrer\r\n".to_owned(), + format!("go{click}"), + ), + "attr-link" => ( + String::new(), + format!("go{click}"), + ), + "rel-link" => ( + String::new(), + format!("go{click}"), + ), + "origin-link" => ( + String::new(), + format!("{}go{click}", meta("origin")), + ), + "meta-location" => ( + String::new(), + format!( + "{}", + meta("no-referrer") + ), + ), + "meta-form" => ( + String::new(), + format!( + "{}
", + meta("no-referrer") + ), + ), + _ => ( + String::new(), + format!("go{click}"), + ), + }; + return (200, format!("Content-Type: text/html\r\n{headers}"), body); + } + if url.path() == "/nav-target" { + return ( + 200, + "Content-Type: text/html\r\n".to_owned(), + "

target

".to_owned(), + ); + } if url.path() == "/page" { return ( 200, @@ -963,3 +1018,36 @@ async fn csp_sandboxed_child_script_and_modulepreload_preserve_opaque_origin() - } Ok(()) } + +#[tokio::test(flavor = "multi_thread")] +async fn top_level_navigations_apply_the_document_and_link_referrer_policy() -> Result<()> { + let server = SecurityServers::spawn().await?; + let browser = Browser::new(BrowserConfig::default())?; + let origin = server.origin.clone(); + let source = |case: &str| format!("{origin}/referrer-nav?case={case}"); + for (case, expected) in [ + ("plain-link", Some(source("plain-link"))), + ("meta-link", None), + ("header-link", None), + ("attr-link", None), + ("rel-link", None), + ("origin-link", Some(format!("{origin}/"))), + ("meta-location", None), + ("meta-form", None), + ] { + browser + .fetch_with_wait_until( + &source(case), + moli_core::runtime::RenderedDomWaitUntil::NetworkIdle, + std::time::Duration::from_secs(5), + ) + .await?; + let requests = server.requests.lock(); + let target = requests + .iter() + .find(|request| request.path == format!("/nav-target?case={case}")) + .unwrap_or_else(|| panic!("{case}: no navigation request in {requests:?}")); + assert_eq!(target.referrer, expected, "{case}"); + } + Ok(()) +} diff --git a/moli-renderer-v8/src/native_bridge/context_host/core.rs b/moli-renderer-v8/src/native_bridge/context_host/core.rs index 1044d54c69e..8442374d280 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/core.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/core.rs @@ -549,6 +549,7 @@ impl PageScriptHost { current_inline_script_stack: Vec::new(), compiled_string_provenance: Vec::new(), active_runtime_command_cause: None, + hyperlink_referrer_policy: None, active_inspector_dispatch: false, pending_top_level_navigation: None, ordinary_page_turn_navigation_handoff_active: false, diff --git a/moli-renderer-v8/src/native_bridge/context_host/mod.rs b/moli-renderer-v8/src/native_bridge/context_host/mod.rs index b4d8c4c3cec..d2d35b18e57 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/mod.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/mod.rs @@ -1045,6 +1045,9 @@ pub(crate) struct PageScriptHost { /// entering V8. Only effects created synchronously inside that dispatch /// may copy this value; it is never inferred from later Page work. active_runtime_command_cause: Option, + /// A hyperlink's own `referrerpolicy` / `rel=noreferrer` while its + /// activation runs; it overrides the Document policy for that navigation. + pub(crate) hyperlink_referrer_policy: Option, /// True only while V8 Inspector is synchronously dispatching a protocol /// command. DebugEvaluate can expose StackFrame objects whose location /// accessors are invalid, so callbacks use this scope to avoid probing them. diff --git a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs index 79b6345c770..36dc3279655 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs @@ -173,6 +173,7 @@ impl PageScriptHost { let handoff = self.top_level_navigation_handoff_tx.next_handoff(); let reserved_service_worker_client = self.pending_reserved_service_worker_top_level_client_for_navigation(&url); + let navigation_initiator = Some(self.document_navigation_initiator()); self.pending_top_level_navigation = Some(PendingTopLevelNavigation::Location(Box::new( PendingLocationNavigation { handoff, @@ -186,7 +187,7 @@ impl PageScriptHost { request_body, request_headers, browser_navigation_kind, - navigation_initiator: None, + navigation_initiator, initial_document_environment: None, entry_seed, reserved_service_worker_client, @@ -197,6 +198,28 @@ impl PageScriptHost { self.handoff_ordinary_page_turn_navigation(handoff); } + /// The page Document as the initiator of its own location navigation, so + /// its referrer policy (Referrer-Policy header or ``, + /// or the activating hyperlink's own policy) applies to the request. + /// Navigations from another realm replace this with their own source. + fn document_navigation_initiator(&self) -> crate::runtime::RendererNavigationInitiator { + let top = crate::native_bridge::OwnerDispatchScope::Top; + let url = self + .document_referrer_source_url_for_dispatch_scope(top) + .unwrap_or_else(|| self.document_url().clone()); + let origin = self + .document_resource_loader_for_dispatch_scope(top) + .map(|loader| loader.fetch_context().request_origin()) + .unwrap_or_else(|| moli_url::WebOrigin::from_url(&url)); + let policy = self.hyperlink_referrer_policy.clone().or_else(|| { + crate::context_bootstrap::document_referrer_policy_for_native_document( + self, + self.document_handle(), + ) + }); + crate::runtime::RendererNavigationInitiator::new(url, origin, policy) + } + pub(crate) fn record_pending_service_worker_client_navigation( &mut self, url: Url, diff --git a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs index 05bd86259cc..d0f6824623a 100644 --- a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs +++ b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs @@ -1894,6 +1894,43 @@ fn anchor_click_default_action( modifiers: u8, user_initiated: bool, pending_child_navigations_before_default: &[(DomHandle, ChildBrowsingContextBootstrap)], +) -> Option { + let runtime = unsafe { &*runtime_ptr }; + let noreferrer = element_attribute(runtime, handle, "rel").is_some_and(|rel| { + rel.split_ascii_whitespace() + .any(|token| token.eq_ignore_ascii_case("noreferrer")) + }); + let link_policy = if noreferrer { + Some("no-referrer".to_owned()) + } else { + element_attribute(runtime, handle, "referrerpolicy") + .and_then(|value| crate::referrer_policy::normalize_referrer_policy(&value)) + }; + let previous = std::mem::replace( + &mut unsafe { &mut *runtime_ptr }.hyperlink_referrer_policy, + link_policy, + ); + let result = anchor_click_navigation( + scope, + runtime_ptr, + handle, + button, + modifiers, + user_initiated, + pending_child_navigations_before_default, + ); + unsafe { &mut *runtime_ptr }.hyperlink_referrer_policy = previous; + result +} + +fn anchor_click_navigation( + scope: &mut v8::PinScope<'_, '_>, + runtime_ptr: *mut JsContextHost, + handle: DomHandle, + button: i32, + modifiers: u8, + user_initiated: bool, + pending_child_navigations_before_default: &[(DomHandle, ChildBrowsingContextBootstrap)], ) -> Option { let resolved = resolve_url_like_attribute(unsafe { &*runtime_ptr }, handle, "href"); if resolved.is_empty() || url::Url::parse(&resolved).is_err() { diff --git a/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs b/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs index 2fbf25760fe..707e4478076 100644 --- a/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs +++ b/moli-renderer-v8/src/runtime/page_vm/followed_navigation.rs @@ -70,6 +70,7 @@ pub(super) async fn load_followed_location_navigation( request_body: Option, request_headers: Vec<(String, String)>, browser_navigation_kind: BrowserNavigationRequestKind, + request_metadata: Option, ) -> Result { debug!(%url, "starting pre-commit location navigation fetch"); if let Some(response) = about_blank_navigation_response(&url) @@ -115,6 +116,7 @@ pub(super) async fn load_followed_location_navigation( request_body, request_headers, browser_navigation_kind, + request_metadata, ) .map_err(|error| anyhow!("failed to build location navigation request: {error}"))?; let mut response = loader @@ -464,13 +466,19 @@ fn build_followed_location_navigation_request( request_body: Option, request_headers: Vec<(String, String)>, browser_navigation_kind: BrowserNavigationRequestKind, + request_metadata: Option, ) -> Result { Request::new_bytes(request_method, url.as_str(), request_body, request_headers).map(|request| { - request + let request = request .with_top_level_navigation_cookie_context() .with_browser_navigation_kind(browser_navigation_kind) .with_initiator_url(initiator_url) - .with_request_origin(moli_url::WebOrigin::from_url(initiator_url)) + .with_request_origin(moli_url::WebOrigin::from_url(initiator_url)); + // The initiating Document's referrer policy, when it recorded one. + match request_metadata { + Some(metadata) => request.with_subresource_request_metadata(metadata), + None => request, + } }) } @@ -772,6 +780,10 @@ impl PageVm { let request_body = pending.request_body.clone(); let request_headers = pending.request_headers.clone(); let browser_navigation_kind = pending.browser_navigation_kind; + let request_metadata = pending + .navigation_initiator + .as_ref() + .map(|initiator| initiator.request_metadata()); let reserved_service_worker_client_id = pending .reserved_service_worker_client .map(|reserved| reserved.release()); @@ -840,6 +852,7 @@ impl PageVm { request_body, request_headers, browser_navigation_kind, + request_metadata, ) .await { @@ -1523,6 +1536,7 @@ mod tests { None, Vec::new(), BrowserNavigationRequestKind::Navigate, + None, ) .unwrap(); let headers = outgoing_request_headers(&Default::default(), &request, None); @@ -1566,6 +1580,7 @@ mod tests { None, Vec::new(), BrowserNavigationRequestKind::Reload, + None, ) .unwrap(); let headers = outgoing_request_headers(&Default::default(), &request, None); @@ -1599,6 +1614,7 @@ mod tests { "application/x-www-form-urlencoded".to_owned(), )], BrowserNavigationRequestKind::Navigate, + None, ) .unwrap(); @@ -1626,6 +1642,7 @@ mod tests { None, Vec::new(), BrowserNavigationRequestKind::Navigate, + None, ) .await; match result { @@ -1644,6 +1661,7 @@ mod tests { Some(bytes::Bytes::from_static(b"upload")), Vec::new(), BrowserNavigationRequestKind::Navigate, + None, ) .await .unwrap(); From 98b68cebd0b5dfcb38c1e6c1f6a5228ab39ea612 Mon Sep 17 00:00:00 2001 From: lanyue-llk <270302213+lanyue-llk@users.noreply.github.com> Date: Thu, 12 Nov 2026 20:35:40 +0700 Subject: [PATCH 2/2] fix(navigation): scope link referrer policy to its own navigation and skip reloads --- moli-core/tests/fetch/security.rs | 24 +++++++- .../context_bootstrap/location_navigation.rs | 7 +++ .../navigation_cross_document.rs | 1 + .../src/native_bridge/context_host/core.rs | 1 - .../src/native_bridge/context_host/mod.rs | 3 - .../native_bridge/context_host/navigation.rs | 55 ++++++++++++++++--- .../element/activation/default_action.rs | 38 +------------ .../element/activation/targets.rs | 13 ++++- .../native_bridge/element/forms/submission.rs | 1 + .../tests/browser_api/history_worlds.rs | 19 +++++++ 10 files changed, 110 insertions(+), 52 deletions(-) diff --git a/moli-core/tests/fetch/security.rs b/moli-core/tests/fetch/security.rs index 55e4294b2fa..158dbd3b784 100644 --- a/moli-core/tests/fetch/security.rs +++ b/moli-core/tests/fetch/security.rs @@ -138,6 +138,22 @@ fn fixture_response(request: &IncomingRequest) -> (u16, String, String) { meta("no-referrer") ), ), + "unsafe-link" => ( + String::new(), + format!( + "{}go{click}", + meta("no-referrer") + ), + ), + // The link's own policy must not leak into a navigation that a + // navigate handler starts instead of it. + "navigate-handler" => ( + String::new(), + format!( + "{}go{click}", + meta("no-referrer"), + ), + ), "meta-form" => ( String::new(), format!( @@ -1034,6 +1050,8 @@ async fn top_level_navigations_apply_the_document_and_link_referrer_policy() -> ("origin-link", Some(format!("{origin}/"))), ("meta-location", None), ("meta-form", None), + ("unsafe-link", Some(source("unsafe-link"))), + ("navigate-handler", None), ] { browser .fetch_with_wait_until( @@ -1043,9 +1061,13 @@ async fn top_level_navigations_apply_the_document_and_link_referrer_policy() -> ) .await?; let requests = server.requests.lock(); + let path = match case { + "navigate-handler" => "/nav-target?case=navigate-handler-redirect".to_owned(), + _ => format!("/nav-target?case={case}"), + }; let target = requests .iter() - .find(|request| request.path == format!("/nav-target?case={case}")) + .find(|request| request.path == path) .unwrap_or_else(|| panic!("{case}: no navigation request in {requests:?}")); assert_eq!(target.referrer, expected, "{case}"); } diff --git a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs index ae144456ce9..83ad1c8fd3f 100644 --- a/moli-renderer-v8/src/context_bootstrap/location_navigation.rs +++ b/moli-renderer-v8/src/context_bootstrap/location_navigation.rs @@ -702,6 +702,13 @@ fn navigate_location_object_with_source_element_and_child_navigate_event<'s>( ); if let Some((initiator, environment)) = navigation_source { unsafe { &mut *host_ptr }.set_pending_location_navigation_source(initiator, environment); + } else if !matches!(kind, LocationNavigationKind::Reload) { + let link_policy = source_element + .and_then(|element| { + crate::native_bridge::node_runtime_and_handle_from_object(scope, element).ok() + }) + .and_then(|(runtime, handle)| unsafe { &*runtime }.hyperlink_referrer_policy(handle)); + unsafe { &mut *host_ptr }.set_pending_location_navigation_document_initiator(link_policy); } } diff --git a/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs b/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs index 587aa8c042f..5459b08a9d3 100644 --- a/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs +++ b/moli-renderer-v8/src/context_bootstrap/navigation_cross_document.rs @@ -84,6 +84,7 @@ pub(super) fn handle_navigation_navigate_cross_document<'s>( }; unsafe { &mut *host_ptr } .record_pending_location_navigation(next_url.clone(), Some(entry_seed)); + unsafe { &mut *host_ptr }.set_pending_location_navigation_document_initiator(None); return navigation_signal .map(|(navigation, signal)| { navigation_cross_document_pending_result( diff --git a/moli-renderer-v8/src/native_bridge/context_host/core.rs b/moli-renderer-v8/src/native_bridge/context_host/core.rs index 8442374d280..1044d54c69e 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/core.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/core.rs @@ -549,7 +549,6 @@ impl PageScriptHost { current_inline_script_stack: Vec::new(), compiled_string_provenance: Vec::new(), active_runtime_command_cause: None, - hyperlink_referrer_policy: None, active_inspector_dispatch: false, pending_top_level_navigation: None, ordinary_page_turn_navigation_handoff_active: false, diff --git a/moli-renderer-v8/src/native_bridge/context_host/mod.rs b/moli-renderer-v8/src/native_bridge/context_host/mod.rs index d2d35b18e57..b4d8c4c3cec 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/mod.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/mod.rs @@ -1045,9 +1045,6 @@ pub(crate) struct PageScriptHost { /// entering V8. Only effects created synchronously inside that dispatch /// may copy this value; it is never inferred from later Page work. active_runtime_command_cause: Option, - /// A hyperlink's own `referrerpolicy` / `rel=noreferrer` while its - /// activation runs; it overrides the Document policy for that navigation. - pub(crate) hyperlink_referrer_policy: Option, /// True only while V8 Inspector is synchronously dispatching a protocol /// command. DebugEvaluate can expose StackFrame objects whose location /// accessors are invalid, so callbacks use this scope to avoid probing them. diff --git a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs index 36dc3279655..938e900880f 100644 --- a/moli-renderer-v8/src/native_bridge/context_host/navigation.rs +++ b/moli-renderer-v8/src/native_bridge/context_host/navigation.rs @@ -173,7 +173,6 @@ impl PageScriptHost { let handoff = self.top_level_navigation_handoff_tx.next_handoff(); let reserved_service_worker_client = self.pending_reserved_service_worker_top_level_client_for_navigation(&url); - let navigation_initiator = Some(self.document_navigation_initiator()); self.pending_top_level_navigation = Some(PendingTopLevelNavigation::Location(Box::new( PendingLocationNavigation { handoff, @@ -187,7 +186,7 @@ impl PageScriptHost { request_body, request_headers, browser_navigation_kind, - navigation_initiator, + navigation_initiator: None, initial_document_environment: None, entry_seed, reserved_service_worker_client, @@ -198,11 +197,51 @@ impl PageScriptHost { self.handoff_ordinary_page_turn_navigation(handoff); } - /// The page Document as the initiator of its own location navigation, so - /// its referrer policy (Referrer-Policy header or ``, - /// or the activating hyperlink's own policy) applies to the request. - /// Navigations from another realm replace this with their own source. - fn document_navigation_initiator(&self) -> crate::runtime::RendererNavigationInitiator { + /// Marks the page Document as the initiator of the just-recorded + /// navigation, so its referrer policy (Referrer-Policy header or + /// ``), or `link_policy` from the activating + /// hyperlink, applies to the request. Reloads and traversals do not call + /// this; navigations from another realm set their own source instead. + pub(crate) fn set_pending_location_navigation_document_initiator( + &mut self, + link_policy: Option, + ) { + let initiator = self.document_navigation_initiator(link_policy); + if let Some(PendingTopLevelNavigation::Location(pending)) = + self.pending_top_level_navigation.as_mut() + { + pending.navigation_initiator = Some(initiator); + } + } + + /// A hyperlink's own referrer policy: `rel=noreferrer`, else its + /// `referrerpolicy` attribute. Other elements have none. + pub(crate) fn hyperlink_referrer_policy( + &self, + source_handle: crate::document_runtime::DomHandle, + ) -> Option { + let element = self + .dom_host() + .node(source_handle) + .and_then(crate::dom::native::Node::as_element)?; + if !(element.is_html_element("a") || element.is_html_element("area")) { + return None; + } + if element.attribute("rel").is_some_and(|rel| { + rel.split_ascii_whitespace() + .any(|token| token.eq_ignore_ascii_case("noreferrer")) + }) { + return Some("no-referrer".to_owned()); + } + element + .attribute("referrerpolicy") + .and_then(crate::referrer_policy::normalize_referrer_policy) + } + + fn document_navigation_initiator( + &self, + link_policy: Option, + ) -> crate::runtime::RendererNavigationInitiator { let top = crate::native_bridge::OwnerDispatchScope::Top; let url = self .document_referrer_source_url_for_dispatch_scope(top) @@ -211,7 +250,7 @@ impl PageScriptHost { .document_resource_loader_for_dispatch_scope(top) .map(|loader| loader.fetch_context().request_origin()) .unwrap_or_else(|| moli_url::WebOrigin::from_url(&url)); - let policy = self.hyperlink_referrer_policy.clone().or_else(|| { + let policy = link_policy.or_else(|| { crate::context_bootstrap::document_referrer_policy_for_native_document( self, self.document_handle(), diff --git a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs index d0f6824623a..6b8e13cb339 100644 --- a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs +++ b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs @@ -1894,43 +1894,6 @@ fn anchor_click_default_action( modifiers: u8, user_initiated: bool, pending_child_navigations_before_default: &[(DomHandle, ChildBrowsingContextBootstrap)], -) -> Option { - let runtime = unsafe { &*runtime_ptr }; - let noreferrer = element_attribute(runtime, handle, "rel").is_some_and(|rel| { - rel.split_ascii_whitespace() - .any(|token| token.eq_ignore_ascii_case("noreferrer")) - }); - let link_policy = if noreferrer { - Some("no-referrer".to_owned()) - } else { - element_attribute(runtime, handle, "referrerpolicy") - .and_then(|value| crate::referrer_policy::normalize_referrer_policy(&value)) - }; - let previous = std::mem::replace( - &mut unsafe { &mut *runtime_ptr }.hyperlink_referrer_policy, - link_policy, - ); - let result = anchor_click_navigation( - scope, - runtime_ptr, - handle, - button, - modifiers, - user_initiated, - pending_child_navigations_before_default, - ); - unsafe { &mut *runtime_ptr }.hyperlink_referrer_policy = previous; - result -} - -fn anchor_click_navigation( - scope: &mut v8::PinScope<'_, '_>, - runtime_ptr: *mut JsContextHost, - handle: DomHandle, - button: i32, - modifiers: u8, - user_initiated: bool, - pending_child_navigations_before_default: &[(DomHandle, ChildBrowsingContextBootstrap)], ) -> Option { let resolved = resolve_url_like_attribute(unsafe { &*runtime_ptr }, handle, "href"); if resolved.is_empty() || url::Url::parse(&resolved).is_err() { @@ -2480,6 +2443,7 @@ pub(in crate::native_bridge) fn navigate_form_target_browsing_context( return false; }; unsafe { &mut *runtime_ptr }.record_pending_location_navigation(url, None); + unsafe { &mut *runtime_ptr }.set_pending_location_navigation_document_initiator(None); crate::context_bootstrap::web_mcp::bind_root_navigation( unsafe { &mut *runtime_ptr }, form_handle, diff --git a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs index 0e4e485529c..3b5318dd682 100644 --- a/moli-renderer-v8/src/native_bridge/element/activation/targets.rs +++ b/moli-renderer-v8/src/native_bridge/element/activation/targets.rs @@ -86,12 +86,16 @@ fn navigate_target_window_location( fn queue_top_level_location_navigation( _scope: &mut v8::PinScope<'_, '_>, runtime_ptr: *mut PageScriptHost, + source_handle: DomHandle, resolved_url: &str, ) -> bool { let Ok(url) = url::Url::parse(resolved_url) else { return false; }; - unsafe { &mut *runtime_ptr }.record_pending_location_navigation(url, None); + let runtime = unsafe { &mut *runtime_ptr }; + let link_policy = runtime.hyperlink_referrer_policy(source_handle); + runtime.record_pending_location_navigation(url, None); + runtime.set_pending_location_navigation_document_initiator(link_policy); true } @@ -722,7 +726,12 @@ pub(in crate::native_bridge) fn navigate_hyperlink_target_browsing_context<'s>( ) { return true; } - return queue_top_level_location_navigation(scope, runtime_ptr, resolved_url); + return queue_top_level_location_navigation( + scope, + runtime_ptr, + source_handle, + resolved_url, + ); } navigate_target_window_location(scope, target_window, resolved_url) } diff --git a/moli-renderer-v8/src/native_bridge/element/forms/submission.rs b/moli-renderer-v8/src/native_bridge/element/forms/submission.rs index 36d4655aded..64a25c7f82f 100644 --- a/moli-renderer-v8/src/native_bridge/element/forms/submission.rs +++ b/moli-renderer-v8/src/native_bridge/element/forms/submission.rs @@ -1047,6 +1047,7 @@ fn submit_post_form_to_top_level_browsing_context( None, moli_fetch::BrowserNavigationRequestKind::Navigate, ); + unsafe { &mut *runtime_ptr }.set_pending_location_navigation_document_initiator(None); crate::context_bootstrap::web_mcp::bind_root_navigation( unsafe { &mut *runtime_ptr }, form_handle, diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs index d20604b141c..21996cbaa93 100644 --- a/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs +++ b/moli-renderer-v8/src/script_vm/tests/browser_api/history_worlds.rs @@ -1613,3 +1613,22 @@ fn history_native_fragment_navigation_preserves_structured_navigation_state() { .unwrap(); assert_eq!(vm.eval("String(history.state.value === 5n && navigation.currentEntry.getState().get('value') === 9n && retained.getState().get('value') === 9n)").unwrap(), "true"); } + +#[test] +fn location_navigations_record_a_document_initiator_but_reloads_do_not() { + let mut vm = new_storage_test_vm("https://example.com/base"); + vm.eval("location.href = '/next'; 'queued'").unwrap(); + let pending = vm.take_pending_location_navigation_with_seed().unwrap(); + assert_eq!( + pending + .navigation_initiator + .map(|initiator| initiator.url().to_string()), + Some("https://example.com/base".to_owned()) + ); + vm.eval("location.reload(); 'queued'").unwrap(); + let pending = vm.take_pending_location_navigation_with_seed().unwrap(); + assert!( + pending.navigation_initiator.is_none(), + "a reload keeps the entry's referrer, so it must not record a new initiator" + ); +}