Skip to content

The headless browser for AI agents and web scraping

fix(js): end a loaded frame's parent chain at the page

Kevsosmooth1737a4b1396f
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rs
index 6b7f15d4..3ebe34eb 100644
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@ -7925,6 +7925,21 @@ mod tests {
assert_eq!(called.value.unwrap(), serde_json::json!(["function", true]));
}
+ // Anti-bot scripts walk `parent` from a frame's window until they reach
+ // their own; a chain that never gets there hangs the page for good.
+ #[tokio::test(flavor = "current_thread")]
+ async fn loaded_frame_parent_chain_reaches_the_page() {
+ let (base, _, _) = spawn_slow_frame_script_server().await;
+ let mut page = import_map_test_page("frame-parent", &base, "<html><body></body></html>");
+ let script = "new Promise(resolve => { const frame = document.createElement('iframe'); \
+ frame.onload = () => { const w = frame.contentWindow; \
+ resolve([w.parent === window, w.top === window, \
+ w.parent.parent === window]); }; \
+ frame.src = '/child.html'; document.body.appendChild(frame); })";
+ let evaluated = page.evaluate_for_cdp_with_timeout(script, true, true, 2_000).await.unwrap();
+ assert_eq!(evaluated.value.unwrap(), serde_json::json!([true, true, true]));
+ }
+
#[tokio::test(flavor = "current_thread")]
async fn cancelling_frame_script_fetch_keeps_all_siblings_resumable() {
let (mut page, requests, slow_seen) =
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index da3d7041..33d636cd 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -15720,6 +15720,12 @@ function _frameWindowFor(frameId) {
get(target, prop) {
if (prop === 'postMessage') return post;
if (prop === '__obscura_wrapsRealm') return true;
+ // The frame realm only holds stand-ins for the windows above it, and a
+ // stand-in is its own parent. Seen from here, the frame's parent is this
+ // window, so `contentWindow.parent === window` holds and a walk up the
+ // parent chain ends.
+ if (prop === 'parent') return globalThis;
+ if (prop === 'top') return globalThis.top;
// Not `receiver`: an accessor on a real global must run with the global
// itself as `this`, not with this proxy.
return Reflect.get(target, prop);