Skip to content

The headless browser for AI agents and web scraping

fix(cssom): use receiver realm for borrowed document hit tests

SGavrle88edc28a3d6
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index f93bafcd..740cde16 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -6317,11 +6317,6 @@ class Document extends Node {
// Preserve the receiver realm's implementations even if a membrane remaps the
// document's public prototype. The ordinary own-document path makes no op call.
-const _documentMembers = Object.freeze(Object.fromEntries(
- ['URL', 'defaultView', 'readyState', 'getElementById', 'querySelector', 'querySelectorAll', 'open', 'close'].map(name => {
- const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, name);
- return [name, descriptor.value || descriptor.get];
- })));
function _documentRealmMember(receiver, name) {
return receiver === globalThis.document ? undefined
: __obscuraCore.ops.op_document_realm_member(receiver, name, _realmFrameId);
@@ -16088,6 +16083,8 @@ if (typeof Document !== 'undefined' && !Document.prototype.elementFromPoint) {
// querySelectorAll returns tree order, which also makes descendants and
// later siblings replace the matching boxes behind them.
Document.prototype.elementFromPoint = function(x, y) {
+ const method = _documentRealmMember(this, 'elementFromPoint');
+ if (method) return Reflect.apply(method, this, [x, y]);
if (typeof x !== 'number' || typeof y !== 'number' || !isFinite(x) || !isFinite(y)) {
return null;
}
@@ -16181,6 +16178,13 @@ if (typeof ShadowRoot !== 'undefined' && !ShadowRoot.prototype.elementFromPoint)
};
}
+// Capture late-defined document members too, before page code can replace them.
+const _documentMembers = Object.freeze(Object.fromEntries(
+ ['URL', 'defaultView', 'readyState', 'getElementById', 'querySelector', 'querySelectorAll', 'open', 'close', 'elementFromPoint'].map(name => {
+ const descriptor = Object.getOwnPropertyDescriptor(Document.prototype, name);
+ return [name, descriptor.value || descriptor.get];
+ })));
+
globalThis.__obscura_init = function() {
// The host sets __obscura_frameId on a frame realm before calling this.
_realmFrameId = globalThis.__obscura_frameId >>> 0;
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 4c06e913..ed4b6960 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -4989,6 +4989,29 @@ mod tests {
]));
}
+ #[cfg(feature = "render")]
+ #[test]
+ fn borrowed_document_hit_testing_uses_the_receiver_realm() {
+ let mut rt = setup_runtime(
+ "<style>body{margin:4px}#parent{width:45px;height:30px}</style><div id='parent'></div>");
+ assert_eq!(rt.evaluate(r#"(() => {
+ const iframe = document.createElement('iframe');
+ document.body.appendChild(iframe);
+ const d = iframe.contentDocument;
+ d.body.innerHTML = '<style>body{margin:8px}div{width:100px;height:20px}</style><div id="child"></div>';
+ const child = d.getElementById('child'), parent = document.getElementById('parent');
+ const foreign = iframe.contentWindow.Document.prototype;
+ return [document.elementFromPoint(10, 10) === parent,
+ d.elementFromPoint(10, 10) === child,
+ Document.prototype.elementFromPoint.call(d, 10, 10) === child,
+ foreign.elementFromPoint.call(document, 10, 10) === parent,
+ Document.prototype.elementFromPoint.call(d, 400, 10) === null,
+ foreign.elementFromPoint.call(document, 400, 10) === document.body,
+ Document.prototype.elementsFromPoint.call(d, 10, 10)[0] === child,
+ foreign.elementsFromPoint.call(document, 10, 10)[0] === parent];
+ })()"#).unwrap(), serde_json::json!([true, true, true, true, true, true, true, true]));
+ }
+
#[cfg(feature = "render")]
#[test]
fn computed_style_on_a_foreign_frame_element_tracks_its_document() {