Skip to content

The headless browser for AI agents and web scraping

Give HTML elements distinct native interface brands

SGavrlccdaf10393db
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 06e8eddb..fd58e92d 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -14559,6 +14559,14 @@ globalThis.HTMLDialogElement = Element;
// Identity-only interfaces still need distinct prototypes. Inheriting Element
// preserves implemented DOM behavior without making unrelated tags pass brand checks.
for (const [name, tags] of Object.entries({
+ HTMLAnchorElement: ['a'], HTMLBRElement: ['br'], HTMLBodyElement: ['body'],
+ HTMLButtonElement: ['button'], HTMLDataListElement: ['datalist'], HTMLDetailsElement: ['details'],
+ HTMLDialogElement: ['dialog'], HTMLDivElement: ['div'], HTMLFieldSetElement: ['fieldset'],
+ HTMLHRElement: ['hr'], HTMLHeadElement: ['head'], HTMLHeadingElement: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
+ HTMLHtmlElement: ['html'], HTMLLabelElement: ['label'], HTMLLegendElement: ['legend'],
+ HTMLLinkElement: ['link'], HTMLParagraphElement: ['p'], HTMLPreElement: ['pre'],
+ HTMLProgressElement: ['progress'], HTMLScriptElement: ['script'], HTMLSpanElement: ['span'],
+ HTMLStyleElement: ['style'], HTMLTemplateElement: ['template'], HTMLUListElement: ['ul'],
HTMLAreaElement: ['area'], HTMLBaseElement: ['base'], HTMLDListElement: ['dl'],
HTMLDataElement: ['data'], HTMLDirectoryElement: ['dir'], HTMLEmbedElement: ['embed'],
HTMLFontElement: ['font'], HTMLFrameElement: ['frame'], HTMLFrameSetElement: ['frameset'],
@@ -14571,7 +14579,7 @@ for (const [name, tags] of Object.entries({
HTMLTableColElement: ['col', 'colgroup'], HTMLTableRowElement: ['tr'],
HTMLTableSectionElement: ['tbody', 'thead', 'tfoot'], HTMLTimeElement: ['time'], HTMLTitleElement: ['title'],
})) {
- if (globalThis[name] !== undefined) continue;
+ if (globalThis[name] !== undefined && globalThis[name] !== Element) continue;
const Interface = class extends Element {
constructor(nid, token) {
if (token !== _htmlInterfaceToken) {
@@ -14582,6 +14590,7 @@ for (const [name, tags] of Object.entries({
}
};
Object.defineProperty(Interface, 'name', {value: name, configurable: true});
+ Object.defineProperty(Interface, 'length', {value: 0, configurable: true});
Object.defineProperty(Interface.prototype, Symbol.toStringTag, {value: name, configurable: true});
Object.defineProperty(globalThis, name, {value: Interface, writable: true, configurable: true});
for (const tag of tags) {
diff --git a/crates/obscura-js/src/frame.rs b/crates/obscura-js/src/frame.rs
index c7e8780c..0b83ad74 100644
--- a/crates/obscura-js/src/frame.rs
+++ b/crates/obscura-js/src/frame.rs
@@ -550,6 +550,28 @@ mod tests {
serde_json::json!(true));
}
+ #[test]
+ fn html_interface_brands_stay_in_the_creation_realm() {
+ let mut parent = page("https://parent.example/",
+ "<html><body><div id='parent'>Parent</div></body></html>");
+ let frame = FrameRealm::new(&mut parent, 1, 0, "https://parent.example/frame",
+ "<html><body><div id='child'>Child</div><span></span></body></html>").unwrap();
+ assert_eq!(frame.evaluate(&mut parent, r#"(() => {
+ HTMLDivElement.prototype.__brandProbe = 'child';
+ const node = document.getElementById('child');
+ return [node.constructor === HTMLDivElement, node instanceof HTMLDivElement,
+ !(node instanceof HTMLSpanElement), document.createElement('div').__brandProbe,
+ document.querySelector('span').__brandProbe === undefined];
+ })()"#).unwrap(), serde_json::json!([true,true,true,"child",true]));
+ assert_eq!(parent.evaluate(r#"(() => {
+ const child = globalThis.__obscura_frameObjects[1].document.getElementById('child');
+ const node = document.getElementById('parent');
+ return [child.constructor.name, child.__brandProbe, child instanceof HTMLDivElement,
+ node.constructor === HTMLDivElement, node.__brandProbe === undefined,
+ document.createElement('div').__brandProbe === undefined];
+ })()"#).unwrap(), serde_json::json!(["HTMLDivElement","child",false,true,true,true]));
+ }
+
#[test]
fn borrowed_select_and_option_methods_keep_the_owning_dom() {
let mut parent = page("https://parent.example/",
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index b263b7df..04cc5ba8 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -4665,6 +4665,82 @@ impl Default for ObscuraJsRuntime {
#[cfg(test)]
mod tests {
+ #[test]
+ fn html_interface_brands_use_distinct_native_prototypes() {
+ let mut rt = setup_runtime("<html><head><style></style><link></head><body><div></div><span></span><a></a><p></p><button></button><h2></h2><template></template></body></html>");
+ assert_eq!(rt.evaluate(r#"(() => {
+ const pairs = [
+ ['a','HTMLAnchorElement'], ['br','HTMLBRElement'], ['body','HTMLBodyElement'],
+ ['button','HTMLButtonElement'], ['datalist','HTMLDataListElement'], ['details','HTMLDetailsElement'],
+ ['dialog','HTMLDialogElement'], ['div','HTMLDivElement'], ['fieldset','HTMLFieldSetElement'],
+ ['hr','HTMLHRElement'], ['head','HTMLHeadElement'], ['h2','HTMLHeadingElement'],
+ ['html','HTMLHtmlElement'], ['label','HTMLLabelElement'], ['legend','HTMLLegendElement'],
+ ['link','HTMLLinkElement'], ['p','HTMLParagraphElement'], ['pre','HTMLPreElement'],
+ ['progress','HTMLProgressElement'], ['script','HTMLScriptElement'], ['span','HTMLSpanElement'],
+ ['style','HTMLStyleElement'], ['template','HTMLTemplateElement'], ['ul','HTMLUListElement'],
+ ];
+ const xhtml = 'http://www.w3.org/1999/xhtml';
+ const nodes = pairs.map(([tag]) => document.createElement(tag));
+ const correct = pairs.map(([tag,name], index) => {
+ const C = globalThis[name];
+ const node = nodes[index];
+ let rejected = false;
+ try { new C(); } catch (error) { rejected = error instanceof TypeError; }
+ return node instanceof C && node.constructor === C
+ && Function.prototype[Symbol.hasInstance].call(C, node)
+ && Object.getPrototypeOf(node) === C.prototype
+ && Object.prototype.toString.call(node) === '[object '+name+']'
+ && C.length === 0 && rejected
+ && node.cloneNode().constructor === C
+ && document.createElementNS(xhtml, 'x:'+tag).constructor === C
+ && !(document.createElementNS(xhtml, tag.toUpperCase()) instanceof C)
+ && !(document.createElementNS('urn:foreign', tag) instanceof C)
+ && nodes.every((other, otherIndex) => (other instanceof C) === (index === otherIndex));
+ });
+ const parsed = pairs.filter(([tag]) => document.querySelector(tag)).every(([tag,name]) => {
+ const node = document.querySelector(tag);
+ return node.constructor === globalThis[name]
+ && Object.getPrototypeOf(node) === globalThis[name].prototype;
+ });
+ HTMLDivElement.prototype.__brandProbe = 42;
+ const isolated = document.querySelector('div').__brandProbe === 42
+ && document.createElement('div').__brandProbe === 42
+ && document.querySelector('span').__brandProbe === undefined
+ && document.querySelector('a').__brandProbe === undefined
+ && Element.prototype.__brandProbe === undefined;
+ delete HTMLDivElement.prototype.__brandProbe;
+ return {correct, parsed, isolated};
+ })()"#).unwrap(), serde_json::json!({
+ "correct":vec![true;24], "parsed":true, "isolated":true,
+ }));
+ }
+
+ #[test]
+ fn html_interface_brands_preserve_existing_specialized_behavior() {
+ let mut rt = setup_runtime("<html><head></head><body><input><select><option>one</option></select><textarea></textarea><form></form><iframe></iframe><div></div></body></html>");
+ assert_eq!(rt.evaluate(r#"(() => {
+ const input = document.querySelector('input');
+ input.value = 'typed';
+ const select = document.querySelector('select');
+ const textarea = document.querySelector('textarea');
+ textarea.value = 'text';
+ const style = document.createElement('style');
+ style.textContent = 'body {color: red}';
+ let target = document.head;
+ if (target instanceof HTMLIFrameElement) target = target.contentDocument.head;
+ target.appendChild(style);
+ return [input instanceof HTMLInputElement, input.value,
+ select instanceof HTMLSelectElement, select.item(0).index,
+ textarea instanceof HTMLTextAreaElement, textarea.value,
+ document.querySelector('form') instanceof HTMLFormElement,
+ document.querySelector('iframe') instanceof HTMLIFrameElement,
+ !(document.querySelector('div') instanceof HTMLIFrameElement),
+ style.parentNode === document.head, style.constructor === HTMLStyleElement];
+ })()"#).unwrap(), serde_json::json!([
+ true,"typed",true,0,true,"text",true,true,true,true,true,
+ ]));
+ }
+
#[test]
fn file_input_validation_is_nonmutating_and_selection_honors_multiple() {
let mut rt = setup_runtime("<input id=single type=file><input id=multi type=file multiple><div id=wrong></div>");
@@ -10387,7 +10463,7 @@ mod tests {
target.element === element]"
)
.unwrap(),
- serde_json::json!(["[object HTMLElement]", "[object SVGElement]", false, true])
+ serde_json::json!(["[object HTMLDivElement]", "[object SVGElement]", false, true])
);
}
diff --git a/docs/Architecture-overview.md b/docs/Architecture-overview.md
index 61de8d11..771382fb 100644
--- a/docs/Architecture-overview.md
+++ b/docs/Architecture-overview.md
@@ -184,6 +184,13 @@ or nested shadow trees. Native selector scoping also handles `:scope`. Omitted
or zero node IDs retain the legacy document fallback; invalid node IDs and
invalid selectors return protocol errors rather than empty successful results.
+Previously aliased HTML interfaces now use the same native wrapper factory as
+table/select interfaces. Parsed, created and cloned HTML nodes receive distinct
+interface prototypes and constructors; interface prototype changes do not affect
+unrelated tags. Namespace/local-name checks avoid assigning those interfaces to
+foreign or differently cased names. Existing specialized interfaces retain their
+behavior. The generic `HTMLElement`/`HTMLUnknownElement` hierarchy is separate work.
+
`window.origin` (including the bare `origin` identifier) reports the owning
document's origin, including inherited blank-frame origins. Page assignment
replaces this property without changing the document's actual origin.