Skip to content

The headless browser for AI agents and web scraping

fix(js): give HTMLIFrameElement its own brand instead of aliasing Element

wstczyw9ba4ccf0b43a
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index c6eff996..d84711b6 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -7091,6 +7091,10 @@ function _elementClassFor(nid) {
&& _domParse("namespace_uri", nid) === "http://www.w3.org/1999/xhtml") {
return globalThis.HTMLSlotElement;
}
+ if (tag === "IFRAME" && globalThis.HTMLIFrameElement
+ && _domParse("namespace_uri", nid) === "http://www.w3.org/1999/xhtml") {
+ return globalThis.HTMLIFrameElement;
+ }
if (tag === "IMG") return HTMLImageElement;
if (tag === "CANVAS" && globalThis.HTMLCanvasElement) return globalThis.HTMLCanvasElement;
if (tag === "AUDIO") return HTMLAudioElement;
@@ -7114,6 +7118,7 @@ function _elementClassForKnownName(namespace, qualifiedName) {
if (tag === "TEXTAREA" && globalThis.HTMLTextAreaElement) return globalThis.HTMLTextAreaElement;
if (localName === "meta" && globalThis.HTMLMetaElement) return globalThis.HTMLMetaElement;
if (tag === "SLOT" && globalThis.HTMLSlotElement) return globalThis.HTMLSlotElement;
+ if (tag === "IFRAME" && globalThis.HTMLIFrameElement) return globalThis.HTMLIFrameElement;
if (tag === "IMG") return HTMLImageElement;
if (tag === "CANVAS" && globalThis.HTMLCanvasElement) return globalThis.HTMLCanvasElement;
if (tag === "AUDIO") return HTMLAudioElement;
@@ -13112,7 +13117,11 @@ for (const proto of [HTMLInputElement.prototype, HTMLTextAreaElement.prototype])
Object.defineProperty(proto, 'readOnly', { enumerable: true });
}
globalThis.HTMLTableElement = Element;
-globalThis.HTMLIFrameElement = Element;
+// <iframe> needs its own brand: with `HTMLIFrameElement = Element` every
+// element passed `instanceof HTMLIFrameElement`, so style-loader's
+// "insert target is an iframe" branch took `contentDocument.head` of <head>
+// itself and failed with "Couldn't find a style target".
+globalThis.HTMLIFrameElement = class HTMLIFrameElement extends Element {};
globalThis.HTMLCanvasElement = Element;
// HTMLVideoElement and HTMLAudioElement are defined above with canPlayType support.
globalThis.HTMLScriptElement = Element;
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index b7c574ab..80ac9505 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -8663,6 +8663,31 @@ mod tests {
);
}
+ #[test]
+ fn iframe_brand_does_not_match_other_elements() {
+ // style-loader resolves its insert target with
+ // `found instanceof HTMLIFrameElement ? found.contentDocument.head : found`;
+ // with the interface aliased to Element, <head> took the iframe branch.
+ let mut rt = setup_runtime(
+ r#"<html><head></head><body><iframe id="frame"></iframe><div id="plain"></div></body></html>"#,
+ );
+ let result = rt.evaluate(r#"
+ const frame = document.getElementById('frame');
+ const created = document.createElement('iframe');
+ const nodes = [document.head, document.documentElement, document.body,
+ document.getElementById('plain'), document.createElement('div'),
+ document.createElementNS('urn:test', 'IFRAME')];
+ return [frame instanceof HTMLIFrameElement, created instanceof HTMLIFrameElement,
+ frame.cloneNode() instanceof HTMLIFrameElement,
+ frame instanceof HTMLElement && frame instanceof Element,
+ nodes.map(node => node instanceof HTMLIFrameElement)];
+ "#).unwrap();
+ assert_eq!(result, serde_json::json!([
+ true, true, true, true,
+ [false, false, false, false, false, false]
+ ]));
+ }
+
#[test]
fn meta_http_equiv_reflection_supports_snapshot_csp_filtering() {
let mut rt = setup_runtime(