Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(iframe): reuse referrer policy for explicit about blank navigation

ldm0db2e6f84f9d6
diff --git a/moli-fetch/src/lib.rs b/moli-fetch/src/lib.rs
index 90c1e6ca8..943bc1d38 100644
--- a/moli-fetch/src/lib.rs
+++ b/moli-fetch/src/lib.rs
@@ -65,7 +65,8 @@ pub use network_fetch_result::{
NetworkResponseObservation,
};
pub use referrer_policy::{
- DEFAULT_REFERRER_POLICY, origin_referrer_url, referrer_header_value, sanitized_referrer_url,
+ DEFAULT_REFERRER_POLICY, origin_referrer_url, referrer_header_value, referrer_value,
+ sanitized_referrer_url,
};
pub use request::{
BrowserNavigationRequestKind, BrowserRequestMetadata, FetchPriorityHint, Request, RequestAuth,
diff --git a/moli-fetch/src/referrer_policy.rs b/moli-fetch/src/referrer_policy.rs
index 01a1f218d..e8a041f21 100644
--- a/moli-fetch/src/referrer_policy.rs
+++ b/moli-fetch/src/referrer_policy.rs
@@ -12,6 +12,25 @@ pub fn referrer_header_value(
if !matches!(request_url.scheme(), "http" | "https") {
return None;
}
+
+ referrer_value(
+ referrer_url,
+ request_url,
+ referrer_policy,
+ document_referrer_policy,
+ )
+}
+
+/// Applies Referrer Policy without requiring an HTTP(S) destination.
+///
+/// Local navigations still use the selected value for `Document.referrer`,
+/// even though they do not emit a `Referer` request header.
+pub fn referrer_value(
+ referrer_url: &Url,
+ request_url: &Url,
+ referrer_policy: Option<&str>,
+ document_referrer_policy: Option<&str>,
+) -> Option<String> {
if !matches!(referrer_url.scheme(), "http" | "https") {
return None;
}
@@ -207,4 +226,16 @@ mod tests {
None
);
}
+
+ #[test]
+ fn non_http_navigation_target_still_has_a_policy_selected_referrer() {
+ let source = url("https://example.com/docs/page.html?x=1#section");
+ let target = url("about:blank");
+
+ assert_eq!(
+ referrer_value(&source, &target, None, None),
+ Some("https://example.com/".to_owned())
+ );
+ assert_eq!(referrer_header_value(&source, &target, None, None), None);
+ }
}
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs
index 20745fa48..df05c722a 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_frames/registry.rs
@@ -308,14 +308,32 @@ impl JsContextHost {
} else {
existing_document_policy.as_ref()
};
+ // The internal initial empty Document starts with the creator
+ // URL. A later explicit about:blank navigation must run that
+ // URL through Referrer Policy even though it sends no request.
+ let document_referrer = if !is_new
+ && attribute_bootstrap_changed
+ && let ChildBrowsingContextBootstrap::Url(target) = &attribute_bootstrap
+ && target.as_str() == "about:blank"
+ {
+ let creator_policy =
+ self.initial_child_about_blank_policy_container_from_parent(handle);
+ moli_fetch::referrer_value(
+ &creator_document_url,
+ target,
+ None,
+ creator_policy.referrer_policy.as_deref(),
+ )
+ .unwrap_or_default()
+ } else if attribute_bootstrap_changed || is_new {
+ creator_document_url.to_string()
+ } else {
+ refresh_policy_source
+ .map(|policy| policy.document_referrer.clone())
+ .unwrap_or_else(|| creator_document_url.to_string())
+ };
let document_policy_container = ChildDocumentPolicyContainer {
- document_referrer: if attribute_bootstrap_changed || is_new {
- creator_document_url.to_string()
- } else {
- refresh_policy_source
- .map(|policy| policy.document_referrer.clone())
- .unwrap_or_else(|| creator_document_url.to_string())
- },
+ document_referrer,
referrer_policy: refresh_policy_source
.and_then(|policy| policy.referrer_policy.clone()),
cross_origin_embedder_policy: refresh_policy_source
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
index 649e09411..ccad5ebe7 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface.rs
@@ -15420,3 +15420,50 @@ fn window_named_access_uses_its_realm_during_child_history_callbacks() {
.unwrap();
assert_eq!(result.unwrap(), "topFrame|undefined|childOnly");
}
+
+#[test]
+fn dynamic_about_blank_iframe_navigation_uses_origin_referrer() {
+ let mut vm = new_storage_test_vm(
+ "http://dynamic-about-blank-referrer.test/source/parent.html?query#fragment",
+ );
+
+ vm.eval(
+ r#"
+(() => {
+ const frame = document.createElement('iframe');
+ (document.body || document.documentElement || document).appendChild(frame);
+ globalThis.__dynamicBlankFrame = frame;
+})()
+"#,
+ )
+ .expect("initial about:blank child should evaluate");
+ vm.drain_pending_child_frame_work_for_test();
+
+ vm.eval(
+ r#"
+(() => {
+ globalThis.__dynamicBlankLoadCount = 0;
+ __dynamicBlankFrame.onload = () => globalThis.__dynamicBlankLoadCount++;
+ __dynamicBlankFrame.src = 'about:blank';
+})()
+"#,
+ )
+ .expect("dynamic about:blank navigation should evaluate");
+
+ vm.drain_pending_child_frame_work_for_test();
+ assert_eq!(
+ vm.eval(
+ r#"(() => {
+ const frame = document.querySelector('iframe');
+ return JSON.stringify({
+ referrer: frame.contentDocument.referrer,
+ loads: __dynamicBlankLoadCount,
+ href: frame.contentWindow.location.href,
+ historyLength: frame.contentWindow.history.length
+ });
+})()"#,
+ )
+ .expect("dynamic about:blank result should evaluate"),
+ r#"{"referrer":"http://dynamic-about-blank-referrer.test/","loads":1,"href":"about:blank","historyLength":1}"#
+ );
+}