Skip to content

The headless browser for AI agents and web scraping

fix(js): make document read-only

yzyf131291fe4de3f92b
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index 6fccfe2c..6cd885a6 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -7665,7 +7665,15 @@ function _wrapEl(nid) {
globalThis._wrap = _wrap;
globalThis.self = globalThis;
-globalThis.document = null;
+// Match Window's read-only document attribute. Script assignment is ignored in
+// sloppy mode and throws in strict mode, while navigation updates the backing
+// value through __obscura_init below.
+let _document = null;
+Object.defineProperty(globalThis, "document", {
+ get() { return _document; },
+ enumerable: true,
+ configurable: false,
+});
function _resolveUrl(url) {
url = String(url);
if (!url) return url;
@@ -18345,13 +18353,14 @@ globalThis.__obscura_init = function() {
_installWasmStreamingFallback();
const documentNid = +_dom("document_node_id");
- globalThis.document = new Document(documentNid);
+ const document = new Document(documentNid);
+ _document = document;
// parentNode on <html> reaches the backing document node. Keep that wrapper
// canonical so getRootNode(), isConnected, and identity comparisons return
// the same Document object exposed as globalThis.document.
- _cache.set(documentNid, globalThis.document);
+ _cache.set(documentNid, document);
const previousWindowNames = new Set(_windowNamedPropertyNames);
- _registerWindowNamedTree(globalThis.document.documentElement);
+ _registerWindowNamedTree(document.documentElement);
_reconcileWindowNamedProperties(previousWindowNames);
const scr = _fp('screen');
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 1ca6135c..953574ec 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -20456,6 +20456,48 @@ mod tests {
);
}
+ #[test]
+ fn global_document_cannot_be_replaced_by_page_script() {
+ let mut rt = setup_runtime(r#"<html><body id="live"></body></html>"#);
+
+ let descriptor = rt
+ .evaluate(
+ "(() => { \
+ const d = Object.getOwnPropertyDescriptor(globalThis, 'document'); \
+ return [typeof d.get, d.set === undefined, d.configurable, d.enumerable]; \
+ })()",
+ )
+ .unwrap();
+ assert_eq!(
+ descriptor,
+ serde_json::json!(["function", true, false, true])
+ );
+
+ rt.evaluate("document = { probe: true };").unwrap();
+ let after_assignment = rt
+ .evaluate(
+ "[ \
+ document === globalThis.document, \
+ document instanceof Document, \
+ document.body.tagName, \
+ typeof document.cookie \
+ ]",
+ )
+ .unwrap();
+ assert_eq!(
+ after_assignment,
+ serde_json::json!([true, true, "BODY", "string"])
+ );
+
+ let strict_error = rt
+ .execute_script(
+ "strict-document-assignment",
+ "'use strict'; document = {};",
+ )
+ .unwrap_err();
+ assert!(strict_error.contains("TypeError"), "{strict_error}");
+ }
+
// #940: op_navigate must not move the document's cookie context before the
// navigation actually commits. A queued navigation to another origin must
// not let document.cookie read that origin's cookies (SOP bypass).