Skip to content

The headless browser for AI agents and web scraping

fix(js): exclude iframe ids from Window named access

SGavrl667eb5991f1d
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 0a815d4e..c7995c74 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -13160,9 +13160,9 @@ function _nodeList(els) {
// small legacy set of HTML elements, as properties of the WindowProxy. V8's
// global object cannot be replaced with a WindowProxy after snapshot startup,
// so install lazy accessors for the supported names present in this document.
-// The accessor resolves against the live tree: one match returns that element
-// (or an iframe's Window), while duplicates return a live-shaped
-// HTMLCollection in tree order.
+// The accessor resolves against the live tree: named child contexts take
+// priority, otherwise one match returns its element and duplicate elements
+// return a live-shaped HTMLCollection in tree order.
const _windowNamedPropertyNames = new Set();
const _windowNamedNameTags = new Set(["embed", "form", "iframe", "img", "object"]);
@@ -13205,11 +13205,15 @@ function _windowNamedCandidates(name) {
function _windowNamedValue(name) {
const matches = _windowNamedCandidates(name);
if (matches.length === 0) return undefined;
+ for (const element of matches) {
+ if (element.localName === "iframe" && _windowNameEligibleElement(element)
+ && element.getAttribute("name") === name) {
+ const window = element.contentWindow;
+ if (window) return window;
+ }
+ }
if (matches.length > 1) return HTMLCollection._from(matches);
- const element = matches[0];
- return element.localName === "iframe" && element.contentWindow
- ? element.contentWindow
- : element;
+ return matches[0];
}
function _ensureWindowNamedProperty(name) {
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index e712c961..19d23603 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -7947,6 +7947,35 @@ mod tests {
);
}
+ #[test]
+ fn window_named_access_distinguishes_iframe_ids_from_child_context_names() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let result = rt.evaluate(r#"(() => {
+ const byId = document.createElement('iframe'); byId.id = 'idOnlyFrame';
+ const collision = document.createElement('div'); collision.id = 'namedContext';
+ const first = document.createElement('iframe');
+ first.id = 'namedFrameId'; first.name = 'namedContext';
+ const second = document.createElement('iframe'); second.name = 'namedContext';
+ document.body.append(byId,collision,first,second);
+ const results = [window.idOnlyFrame===byId, window.namedFrameId===first,
+ window.namedContext===first.contentWindow];
+ first.remove();
+ results.push(window.namedContext===second.contentWindow);
+ second.remove();
+ results.push(window.namedContext===collision);
+ byId.name = 'idOnlyFrame';
+ results.push(window.idOnlyFrame===byId.contentWindow);
+ byId.removeAttribute('name');
+ results.push(window.idOnlyFrame===byId);
+ const duplicate = document.createElement('div'); duplicate.id = 'idOnlyFrame';
+ document.body.appendChild(duplicate);
+ results.push(window.idOnlyFrame instanceof HTMLCollection,
+ window.idOnlyFrame.length===2, window.idOnlyFrame[0]===byId);
+ return results;
+ })()"#).unwrap();
+ assert_eq!(result, serde_json::json!([true,true,true,true,true,true,true,true,true,true]));
+ }
+
#[test]
fn window_named_access_tracks_dynamic_ids_and_fragment_parsing() {
let mut rt = setup_runtime("<html><body><div id='host'></div></body></html>");