Skip to content

The headless browser for AI agents and web scraping

fix(js): iframe shadow root document routing

Ben Kircher2c18068dc294
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index c77d5201..d3dc4e66 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -16183,10 +16183,10 @@ Element.prototype.attachShadow = function attachShadow(opts) {
if (!globalThis.__obscura_shadowHostNames.has(_ln) && _ln.indexOf('-') === -1) {
throw new DOMException('Failed to execute attachShadow on Element: this element does not support attachShadow', 'NotSupportedError');
}
- if (__obscuraCore.ops.op_shadow_root_info(this._nid)) {
+ if (__obscuraCore.ops.op_shadow_root_info(this._nid, _realmFrameId)) {
throw new DOMException('Failed to execute attachShadow on Element: the element already hosts a shadow tree.', 'NotSupportedError');
}
- const rootNid = __obscuraCore.ops.op_shadow_attach(this._nid, _mode);
+ const rootNid = __obscuraCore.ops.op_shadow_attach(this._nid, _mode, _realmFrameId);
if (rootNid < 0) {
throw new DOMException('Failed to execute attachShadow on Element: this element does not support attachShadow', 'NotSupportedError');
}
@@ -16205,7 +16205,7 @@ _markNative(Element.prototype.attachShadow);
function _shadowRootForHost(host, includeClosed) {
if (!host) return null;
- const info = __obscuraCore.ops.op_shadow_root_info(host._nid);
+ const info = __obscuraCore.ops.op_shadow_root_info(host._nid, _realmFrameId);
if (!info) return null;
const parts = info.split('\0');
if (!includeClosed && parts[1] !== 'open') return null;
diff --git a/crates/obscura-js/src/frame.rs b/crates/obscura-js/src/frame.rs
index 57c70ebe..7675b733 100644
--- a/crates/obscura-js/src/frame.rs
+++ b/crates/obscura-js/src/frame.rs
@@ -451,6 +451,107 @@ mod tests {
runtime
}
+ #[test]
+ fn frame_shadow_root_append_uses_its_own_document() {
+ let mut parent = page("https://parent.example/",
+ "<html><body><div id='host'>Parent</div></body></html>");
+ let frame = FrameRealm::new(&mut parent, 1, 0, "https://child.example/",
+ "<html><body><div id='host'>Child</div></body></html>").unwrap();
+
+ let result = frame.evaluate(&mut parent, r#"(() => {
+ const host = document.getElementById('host');
+ const root = host.attachShadow({ mode: 'open' });
+ const child = document.createElement('span');
+ child.textContent = 'shadow child';
+ root.appendChild(child);
+ return [
+ host.shadowRoot === root,
+ root.host === host,
+ root.ownerDocument === document,
+ child.parentNode === root,
+ root.textContent
+ ];
+ })()"#).unwrap();
+
+ assert_eq!(result, serde_json::json!([true, true, true, true, "shadow child"]));
+ assert_eq!(parent.evaluate(
+ "document.getElementById('host').shadowRoot === null").unwrap(),
+ serde_json::json!(true));
+ }
+
+ #[test]
+ fn frame_shadow_root_lookup_does_not_read_parent() {
+ let mut parent = page("https://parent.example/",
+ "<html><body><div id='host'>Parent</div></body></html>");
+ parent.execute_script("parent-shadow", r#"
+ document.getElementById('host').attachShadow({ mode: 'open' })
+ .innerHTML = '<span>parent shadow</span>';
+ "#).unwrap();
+ let frame = FrameRealm::new(&mut parent, 1, 0, "https://child.example/",
+ "<html><body><div id='host'>Child</div></body></html>").unwrap();
+
+ let result = frame.evaluate(&mut parent,
+ "document.getElementById('host').shadowRoot === null").unwrap();
+
+ assert_eq!(result, serde_json::json!(true));
+ assert_eq!(parent.evaluate(
+ "document.getElementById('host').shadowRoot.textContent").unwrap(),
+ serde_json::json!("parent shadow"));
+ }
+
+ #[test]
+ fn initial_frame_shadow_root_uses_its_own_document() {
+ let mut parent = page("https://parent.example/",
+ "<html><body><div id='host'>Parent</div></body></html>");
+
+ let result = parent.evaluate(r#"(() => {
+ const frame = document.createElement('iframe');
+ document.body.appendChild(frame);
+ const childDocument = frame.contentDocument;
+ const host = childDocument.createElement('div');
+ childDocument.body.appendChild(host);
+ const root = host.attachShadow({ mode: 'open' });
+ const child = childDocument.createElement('span');
+ child.textContent = 'initial frame shadow';
+ root.appendChild(child);
+ return [
+ host.shadowRoot === root,
+ root.ownerDocument === childDocument,
+ child.parentNode === root,
+ root.textContent,
+ document.getElementById('host').shadowRoot === null
+ ];
+ })()"#).unwrap();
+
+ assert_eq!(result,
+ serde_json::json!([true, true, true, "initial frame shadow", true]));
+ }
+
+ #[test]
+ fn frame_closed_shadow_root_stays_private_and_rejects_duplicate_attach() {
+ let mut parent = page("https://parent.example/",
+ "<html><body><div id='host'>Parent</div></body></html>");
+ let frame = FrameRealm::new(&mut parent, 1, 0, "https://child.example/",
+ "<html><body><div id='host'>Child</div></body></html>").unwrap();
+
+ let result = frame.evaluate(&mut parent, r#"(() => {
+ const host = document.getElementById('host');
+ const root = host.attachShadow({ mode: 'closed' });
+ const child = document.createElement('span');
+ child.textContent = 'closed frame shadow';
+ root.appendChild(child);
+ return [host.shadowRoot === null, root.mode, root.textContent];
+ })()"#).unwrap();
+ let error = frame.evaluate(&mut parent,
+ "document.getElementById('host').attachShadow({ mode: 'open' })").unwrap_err();
+
+ assert_eq!(result, serde_json::json!([true, "closed", "closed frame shadow"]));
+ assert!(error.contains("NotSupportedError"), "{error}");
+ assert_eq!(parent.evaluate(
+ "document.getElementById('host').shadowRoot === null").unwrap(),
+ serde_json::json!(true));
+ }
+
#[test]
fn borrowed_style_getter_keeps_the_elements_declaration() {
let mut parent = page("https://parent.example/", "<html><body></body></html>");
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index 7e1d9c7e..e8f5b4ed 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -1884,13 +1884,18 @@ fn op_script_try_start(state: &OpState, nid: u32) -> bool {
/// tree. Layout intentionally remains unaware of the detached root until
/// scoped style, slot assignment, and composed-tree paint are implemented.
#[op2(fast)]
-fn op_shadow_attach(state: &OpState, host_nid: u32, #[string] mode: String) -> i32 {
+fn op_shadow_attach(
+ state: &OpState,
+ host_nid: u32,
+ #[string] mode: String,
+ frame_id: u32,
+) -> i32 {
let mode = match mode.as_str() {
"open" => ShadowRootMode::Open,
"closed" => ShadowRootMode::Closed,
_ => return -1,
};
- let shared = state.borrow::<SharedState>().clone();
+ let shared = frame_state(state, frame_id);
let state = shared.borrow();
let Some(dom) = state.dom.as_ref() else {
return -1;
@@ -1907,8 +1912,8 @@ fn op_shadow_attach(state: &OpState, host_nid: u32, #[string] mode: String) -> i
/// visibility in bootstrap.js.
#[op2]
#[string]
-fn op_shadow_root_info(state: &OpState, host_nid: u32) -> String {
- let shared = state.borrow::<SharedState>().clone();
+fn op_shadow_root_info(state: &OpState, host_nid: u32, frame_id: u32) -> String {
+ let shared = frame_state(state, frame_id);
let state = shared.borrow();
let Some(dom) = state.dom.as_ref() else {
return String::new();