Skip to content

The headless browser for AI agents and web scraping

fix(js): preserve nested frame parents in message sources

SGavrl6d25c43eb8d4
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 33d636cd..bea38ec2 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -15713,6 +15713,7 @@ function _frameWindowFor(frameId) {
if (globalThis.__obscura_frameObjects[frameId].initial) return real;
if (existing && existing.__obscura_wrapsRealm) return existing;
+ const parentId = Reflect.get(real, '__obscura_parentFrameId') || 0;
const post = _markNative(function (data, targetOrigin, _transfer) {
_sendRealmMessage(frameId, data, targetOrigin);
});
@@ -15720,11 +15721,10 @@ 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;
+ // Message sources can be grandchildren. Resolve their actual parent in
+ // the caller's realm so replies keep both the recipient and sender.
+ if (prop === 'parent') return parentId === _realmFrameId
+ ? globalThis : (_frameWindowFor(parentId) || _remoteWindow(parentId));
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.
diff --git a/crates/obscura-js/src/frame.rs b/crates/obscura-js/src/frame.rs
index 26853b71..2a06d1dd 100644
--- a/crates/obscura-js/src/frame.rs
+++ b/crates/obscura-js/src/frame.rs
@@ -699,6 +699,49 @@ mod tests {
]);
}
+ #[test]
+ fn nested_message_source_parent_keeps_the_intermediate_frame() {
+ let mut parent = page("https://parent.example/", "<html><body></body></html>");
+ let _child = FrameRealm::new(&mut parent, 1, 0,
+ "https://parent.example/child", "<html><body></body></html>").unwrap();
+ let _grandchild = FrameRealm::new(&mut parent, 2, 1,
+ "https://parent.example/grandchild", "<html><body></body></html>").unwrap();
+ parent.execute_script("nested-message-parent", r#"
+ globalThis.sources = [];
+ addEventListener('message', event => sources.push(event.source));
+ __obscura_deliverMessage('{"v":"child"}', 'https://parent.example', 1, '*');
+ __obscura_deliverMessage('{"v":"grandchild"}', 'https://parent.example', 2, '*');
+ sources[1].parent.postMessage('reply', '*');
+ "#).unwrap();
+ assert_eq!(parent.evaluate(
+ "[sources[1].parent === sources[0], sources[1].parent.parent === window, sources[1].top === window]"
+ ).unwrap(), serde_json::json!([true, true, true]));
+ let messages = parent.take_pending_frame_messages();
+ assert_eq!(messages.iter().map(|message| (
+ message.target_frame_id, message.source_frame_id,
+ )).collect::<Vec<_>>(), vec![(1, 0)]);
+ }
+
+ #[test]
+ fn message_source_parent_stays_opaque_across_origins() {
+ let mut parent = page("https://parent.example/", "<html><body></body></html>");
+ let _child = FrameRealm::new(&mut parent, 1, 0,
+ "https://other.example/child", "<html><body></body></html>").unwrap();
+ let _grandchild = FrameRealm::new(&mut parent, 2, 1,
+ "https://parent.example/grandchild", "<html><body></body></html>").unwrap();
+ parent.execute_script("opaque-message-parent", r#"
+ addEventListener('message', event => { globalThis.sourceParent = event.source.parent; });
+ __obscura_deliverMessage('{"v":"grandchild"}', 'https://parent.example', 2, '*');
+ sourceParent.postMessage('reply', '*');
+ "#).unwrap();
+ assert_eq!(parent.evaluate("[sourceParent !== window, sourceParent.document === undefined]").unwrap(),
+ serde_json::json!([true, true]));
+ let messages = parent.take_pending_frame_messages();
+ assert_eq!(messages.iter().map(|message| (
+ message.target_frame_id, message.source_frame_id,
+ )).collect::<Vec<_>>(), vec![(1, 0)]);
+ }
+
#[test]
fn nested_initial_frame_uses_its_owner_document_not_the_entered_page() {
let mut parent = page("https://parent.example/", "<html><body></body></html>");