Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(dom): capture native exception stacks without author lookup

ldm0991175f273f9
diff --git a/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs b/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs
index 5238e76bd..b5fe84e44 100644
--- a/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs
+++ b/moli-renderer-v8/src/context_bootstrap/constructors/dom_exception.rs
@@ -462,6 +462,16 @@ fn initialize_dom_exception<'s>(
dom_exception_declaration(scope, message, name)
.initialize(scope, exception)
.expect("DOMException declaration should initialize");
+ capture_dom_exception_stack(scope, exception);
+}
+
+pub(super) fn capture_dom_exception_stack(
+ scope: &mut v8::PinScope<'_, '_>,
+ exception: v8::Local<'_, v8::Object>,
+) {
+ // V8 captures lazily formatted stack data on the native object, without
+ // looking up the mutable Error.captureStackTrace function in JavaScript.
+ let _ = v8::Exception::capture_stack_trace(scope.get_current_context(), exception);
}
fn dom_exception_declaration<'s>(
@@ -507,6 +517,7 @@ fn initialize_quota_exceeded_error<'s>(
quota_exceeded_error_declaration(scope, message, quota, requested)
.initialize(scope, exception)
.expect("QuotaExceededError declaration should initialize");
+ capture_dom_exception_stack(scope, exception);
}
fn websocket_error_declaration<'s>(
@@ -546,10 +557,11 @@ pub(crate) fn new_dom_exception_value<'s>(
let _ = exception.set_prototype(scope, prototype.into());
return exception.into();
}
- dom_exception_declaration(scope, message, name)
+ let exception = dom_exception_declaration(scope, message, name)
.bind(scope)
- .expect("DOMException declaration should bind")
- .into()
+ .expect("DOMException declaration should bind");
+ capture_dom_exception_stack(scope, exception);
+ exception.into()
}
pub(crate) fn throw_dom_exception_value(
@@ -670,10 +682,11 @@ pub(crate) fn new_quota_exceeded_error_value<'s>(
quota: Option<f64>,
requested: Option<f64>,
) -> v8::Local<'s, v8::Value> {
- quota_exceeded_error_declaration(scope, message, quota, requested)
+ let exception = quota_exceeded_error_declaration(scope, message, quota, requested)
.bind(scope)
- .expect("QuotaExceededError declaration should bind")
- .into()
+ .expect("QuotaExceededError declaration should bind");
+ capture_dom_exception_stack(scope, exception);
+ exception.into()
}
pub(crate) fn initialize_websocket_error<'s>(
@@ -686,6 +699,7 @@ pub(crate) fn initialize_websocket_error<'s>(
websocket_error_declaration(scope, message, close_code, reason)
.initialize(scope, exception)
.expect("WebSocketError declaration should initialize");
+ capture_dom_exception_stack(scope, exception);
}
pub(crate) fn new_websocket_error_value<'s>(
@@ -694,10 +708,11 @@ pub(crate) fn new_websocket_error_value<'s>(
close_code: Option<u16>,
reason: &str,
) -> v8::Local<'s, v8::Value> {
- websocket_error_declaration(scope, message, close_code, reason)
+ let exception = websocket_error_declaration(scope, message, close_code, reason)
.bind(scope)
- .expect("WebSocketError declaration should bind")
- .into()
+ .expect("WebSocketError declaration should bind");
+ capture_dom_exception_stack(scope, exception);
+ exception.into()
}
pub(crate) fn websocket_error_close_info<'s>(
diff --git a/moli-renderer-v8/src/script_vm/tests/mod.rs b/moli-renderer-v8/src/script_vm/tests/mod.rs
index b0640ff23..a7f30d530 100644
--- a/moli-renderer-v8/src/script_vm/tests/mod.rs
+++ b/moli-renderer-v8/src/script_vm/tests/mod.rs
@@ -2159,6 +2159,7 @@ mod lazy_storage;
mod lazy_window_surfaces;
mod midi_owner_interfaces;
mod mouse_snapshot;
+mod native_exception_stack_capture;
mod no_cors_header_fill;
mod observer_callbacks;
mod observer_receivers;
diff --git a/moli-renderer-v8/src/script_vm/tests/native_exception_stack_capture.rs b/moli-renderer-v8/src/script_vm/tests/native_exception_stack_capture.rs
new file mode 100644
index 000000000..e7ac3eb7c
--- /dev/null
+++ b/moli-renderer-v8/src/script_vm/tests/native_exception_stack_capture.rs
@@ -0,0 +1,25 @@
+use super::*;
+
+#[test]
+fn native_exception_stacks_ignore_mutated_javascript_capture_helpers() {
+ let mut vm = new_parsed_test_vm(
+ "https://dom-exception-native-stacks.test/",
+ "<!doctype html><body></body>",
+ );
+ assert_eq!(vm.eval(r#"(() => {
+ const child=document.body.appendChild(document.createElement('iframe')).contentWindow;
+ for(const realm of [window,child]) {
+ const previous=Object.getOwnPropertyDescriptor(realm.Error,'captureStackTrace');
+ Object.defineProperty(realm.Error,'captureStackTrace',{configurable:true,get(){throw Error('author lookup');}});
+ try {
+ for(const error of [new realm.DOMException('message','AbortError'),new realm.QuotaExceededError('full'),new realm.WebSocketError('closed')]) {
+ const descriptor=Object.getOwnPropertyDescriptor(error,'stack');
+ if(typeof error.stack!=='string'||!error.stack.startsWith(error.name+': '+error.message)||!descriptor||descriptor.enumerable)throw Error('native stack');
+ }
+ let error;try{realm.document.querySelector('[');}catch(caught){error=caught;}
+ if(!(error instanceof realm.DOMException)||typeof error.stack!=='string')throw Error('factory stack');
+ } finally {if(previous)Object.defineProperty(realm.Error,'captureStackTrace',previous);else delete realm.Error.captureStackTrace;}
+ }
+ return 'ok';
+})()"#).expect("native regression"), "ok");
+}