Skip to content

The headless browser for AI agents and web scraping

Release discarded native function source metadata

SGavrla36ec393c738
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index ea99c1b5..71f8a138 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -202,28 +202,33 @@ const _dom = (cmd, a1, a2) => {
return result;
};
-const _nativeFns = new Set();
+const _nativeFns = new WeakSet();
+const _nativeFnHas = _nativeFns.has.bind(_nativeFns);
+const _nativeFnAdd = _nativeFns.add.bind(_nativeFns);
// Exact toString override for members whose native form is not just
// `function <name>()`, e.g. accessors (`function get x() { [native code] }`)
// or functions whose `.name` does not match the real builtin.
-const _nativeStr = new Map();
+const _nativeStr = new WeakMap();
+const _nativeStrHas = _nativeStr.has.bind(_nativeStr);
+const _nativeStrGet = _nativeStr.get.bind(_nativeStr);
+const _nativeStrSet = _nativeStr.set.bind(_nativeStr);
const _origToString = Function.prototype.toString;
// Method syntax matches the native function's non-constructible shape and
// does not add an own `prototype` property.
const _functionToString = {
toString() {
- if (_nativeStr.has(this)) { return _nativeStr.get(this); }
- if (_nativeFns.has(this)) {
+ if (_nativeStrHas(this)) { return _nativeStrGet(this); }
+ if (_nativeFnHas(this)) {
return `function ${this.name || ''}() { [native code] }`;
}
return _origToString.call(this);
},
}.toString;
Function.prototype.toString = _functionToString;
-function _markNative(fn) { if (typeof fn === 'function') _nativeFns.add(fn); return fn; }
+function _markNative(fn) { if (typeof fn === 'function') _nativeFnAdd(fn); return fn; }
// Mark a function with an exact native-code toString (used for accessors).
-function _markNativeAs(fn, str) { if (typeof fn === 'function') _nativeStr.set(fn, str); return fn; }
-_nativeFns.add(_functionToString);
+function _markNativeAs(fn, str) { if (typeof fn === 'function') _nativeStrSet(fn, str); return fn; }
+_nativeFnAdd(_functionToString);
// unusualWindowProperties: obscura's internal globals are made non-enumerable
// (see _preHideInternals and __obscura_init), which hides them from
diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rs
index 7c8527be..0752dd9b 100644
--- a/crates/obscura-js/src/runtime.rs
+++ b/crates/obscura-js/src/runtime.rs
@@ -4803,6 +4803,60 @@ mod tests {
);
}
+ #[test]
+ fn native_metadata_does_not_retain_removed_methods() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let value = rt.execute_runtime_script("<removed-native-method>", r#"(() => {
+ const fn = AudioBuffer.prototype.copyToChannel;
+ if (Function.prototype.toString.call(fn) !== 'function copyToChannel() { [native code] }') {
+ throw new Error('Live method lost its native source');
+ }
+ delete AudioBuffer.prototype.copyToChannel;
+ return fn;
+ })()"#.into()).unwrap();
+ let weak = deno_core::v8::Weak::new(rt.runtime().v8_isolate(), &value);
+ rt.collect_garbage();
+ assert!(!weak.is_empty(), "a caller-held method must remain live");
+ drop(value);
+ rt.collect_garbage();
+ assert!(weak.is_empty(), "native-source metadata must not own a removed method");
+ }
+
+ #[test]
+ fn native_metadata_does_not_retain_removed_accessors() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ let value = rt.execute_runtime_script("<removed-native-accessor>", r#"(() => {
+ const fn = Object.getOwnPropertyDescriptor(MouseEvent.prototype, 'x').get;
+ if (Function.prototype.toString.call(fn) !== 'function get x() { [native code] }') {
+ throw new Error('Live accessor lost its native source');
+ }
+ delete MouseEvent.prototype.x;
+ return fn;
+ })()"#.into()).unwrap();
+ let weak = deno_core::v8::Weak::new(rt.runtime().v8_isolate(), &value);
+ rt.collect_garbage();
+ assert!(!weak.is_empty(), "a caller-held accessor must remain live");
+ drop(value);
+ rt.collect_garbage();
+ assert!(weak.is_empty(), "native-source metadata must not own a removed accessor");
+ }
+
+ #[test]
+ fn native_source_survives_page_replacement_of_weak_collection_methods() {
+ let mut rt = setup_runtime("<html><body></body></html>");
+ assert_eq!(rt.evaluate_script(r#"(() => {
+ const method = AudioBuffer.prototype.copyToChannel;
+ const getter = Object.getOwnPropertyDescriptor(MouseEvent.prototype, 'x').get;
+ const fail = () => { throw new Error('Page collection method called'); };
+ WeakSet.prototype.has = WeakSet.prototype.add = fail;
+ WeakMap.prototype.has = WeakMap.prototype.get = WeakMap.prototype.set = fail;
+ return [method.toString(), getter.toString()];
+ })()"#).unwrap(), serde_json::json!([
+ "function copyToChannel() { [native code] }",
+ "function get x() { [native code] }",
+ ]));
+ }
+
// #1013: removeAttribute('id') must update the id_index so getElementById no
// longer returns the (still-attached) element.
#[test]