Skip to content

The headless browser for AI agents and web scraping

fix(frames): avoid publishing opaque child objects

SGavrl17b5d7db743f
diff --git a/crates/obscura-browser/src/page.rs b/crates/obscura-browser/src/page.rs
index 1f245f04..f24e63fe 100644
--- a/crates/obscura-browser/src/page.rs
+++ b/crates/obscura-browser/src/page.rs
@@ -7307,6 +7307,36 @@ mod tests {
serde_json::json!(["about:srcdoc", "HELLO"]));
}
+ #[tokio::test(flavor = "current_thread")]
+ async fn iframe_srcdoc_opaque_document_is_not_published_to_creator_objects() {
+ let mut page = import_map_test_page("srcdoc-private-realm", "https://parent.test", "<body></body>");
+ page.evaluate_script(r#"(() => {
+ globalThis.seenOpaqueSource = false;
+ const f = document.createElement('iframe'); f.id = 'opaque';
+ f.setAttribute('sandbox', 'allow-scripts');
+ f.srcdoc = '<p>private</p><script>parent.postMessage("loaded", "*")</script>';
+ window.addEventListener('message', event => {
+ seenOpaqueSource = event.data === 'loaded' && event.source === f.contentWindow;
+ });
+ document.body.appendChild(f); return 1;
+ })()"#).unwrap();
+ pump_srcdoc_frames(&mut page).await;
+ assert_eq!(page.evaluate_script(r#"(() => {
+ const f = document.getElementById('opaque');
+ const references = [
+ () => f._iframeDoc,
+ () => f._iframeWin?.document,
+ () => globalThis.__obscura_frameObjects[f._frameId]?.document,
+ () => globalThis.__obscura_frameObjects[f._frameId]?.window?.document,
+ ];
+ const readable = references.map(getDocument => {
+ try { return getDocument()?.body?.textContent ?? null; }
+ catch (_) { return null; }
+ });
+ return [f.contentDocument === null, readable, seenOpaqueSource];
+ })()"#).unwrap(), serde_json::json!([true, [null, null, null, null], true]));
+ }
+
#[tokio::test(flavor = "current_thread")]
async fn iframe_load_waits_for_the_real_document_and_author_scripts() {
let (base, _, _) = spawn_slow_frame_script_server().await;
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 905b12af..9d45822f 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -15323,22 +15323,26 @@ function _ensureInitialFrameRealm(element, srcdoc) {
child.__obscura_frameId = id;
child.__obscura_parentFrameId = _realmFrameId;
child.__obscura_init();
- if (!opaque) Object.defineProperties(child, {
+ if (!opaque) {
+ Object.defineProperties(child, {
parent: { value: globalThis, configurable: true },
top: { value: globalThis.top, configurable: true },
frameElement: { value: element, configurable: true },
});
+ globalThis.__obscura_frameObjects[id] = {
+ window: child, document: child.document, initial: true,
+ };
+ }
child.innerWidth = 300;
child.innerHeight = 150;
- globalThis.__obscura_frameObjects[id] = {
- window: child, document: child.document, initial: true,
- };
}, element._nid, srcdoc);
if (frameId) {
element._frameId = frameId;
element._iframeOpaque = opaque;
- element._iframeWin = globalThis.__obscura_frameObjects[frameId].window;
- element._iframeDoc = globalThis.__obscura_frameObjects[frameId].document;
+ element._iframeWin = opaque ? _remoteWindow(frameId)
+ : globalThis.__obscura_frameObjects[frameId].window;
+ element._iframeDoc = opaque ? null
+ : globalThis.__obscura_frameObjects[frameId].document;
globalThis.__obscura_frameElements[frameId] = element;
}
return frameId !== 0;
@@ -15366,7 +15370,7 @@ function _frameWindowFor(frameId) {
if (!frameId) return null;
const real = globalThis.__obscura_frameObjects?.[frameId]?.window;
const existing = globalThis.__obscura_frameWindows[frameId];
- if (!real) return existing || null;
+ if (!real) return existing || _remoteWindows.get(frameId) || null;
if (globalThis.__obscura_frameObjects[frameId].initial) return real;
if (existing && existing.__obscura_wrapsRealm) return existing;