The headless browser for AI agents and web scraping
fix(js): preserve Function.toString native shape
xrip846ed7db912c
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex e8495742..285bcc58 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -132,17 +132,22 @@ const _nativeFns = new Set();// or functions whose `.name` does not match the real builtin.const _nativeStr = new Map();const _origToString = Function.prototype.toString;-Function.prototype.toString = function toString() {- if (_nativeStr.has(this)) { return _nativeStr.get(this); }- if (_nativeFns.has(this)) {- return `function ${this.name || ''}() { [native code] }`;- }- return _origToString.call(this);-};+// 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)) {+ 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; }// 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(Function.prototype.toString);+_nativeFns.add(_functionToString);// unusualWindowProperties: obscura's internal globals are made non-enumerable// (see _preHideInternals and __obscura_init), which hides them fromdiff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 5e593cf9..d47dc978 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -2737,6 +2737,40 @@ mod tests {rt}+ #[test]+ fn function_to_string_has_native_function_shape() {+ let mut rt = setup_runtime("<html><body></body></html>");++ assert_eq!(+ rt.evaluate(+ r#"(() => {+ const fn = Function.prototype.toString;+ let constructible = true;+ try {+ Reflect.construct(function () {}, [], fn);+ } catch (error) {+ constructible = false;+ }+ return {+ source: fn.toString(),+ name: fn.name,+ length: fn.length,+ hasOwnPrototype: Object.prototype.hasOwnProperty.call(fn, "prototype"),+ constructible,+ };+ })()"#,+ )+ .unwrap(),+ serde_json::json!({+ "source": "function toString() { [native code] }",+ "name": "toString",+ "length": 0,+ "hasOwnPrototype": false,+ "constructible": false,+ })+ );+ }+#[test]fn iframe_content_window_exposes_realm_globals() {let mut rt = setup_runtime("<html><body></body></html>");