The headless browser for AI agents and web scraping
fix(js): stop frame realms segfaulting on rejection / dynamic import
Andrey Mnatsakanovb185a44edd8b
diff --git a/crates/obscura-js/src/frame.rs b/crates/obscura-js/src/frame.rsindex 15f2978e..0b67cdb6 100644--- a/crates/obscura-js/src/frame.rs+++ b/crates/obscura-js/src/frame.rs@@ -65,6 +65,10 @@ impl FrameRealm {return None;}parent.copy_identity_to_realm(&context);+ // A snapshot-created realm has null deno_core per-context state slots, so+ // a promise rejection or dynamic import() in the frame would segfault in+ // deno_core's global callbacks (#850, #841). Alias the main realm's state.+ parent.share_deno_context_state_with_realm(&context);// Only a same-origin frame is reachable from the page. Cross-origin// keeps its own security token, so V8 answers `undefined` for any@@ -914,6 +918,46 @@ mod tests {);}+ // #850 / #841 — a promise rejection or dynamic import() inside a frame realm+ // used to null-deref deno_core's global callbacks (which read per-context+ // state from V8 embedder slots) and segfault the whole process. The realm+ // must now alias the main realm's state so these run without crashing.+ #[test]+ fn a_frame_rejection_does_not_crash_the_process() {+ let mut parent = page("https://parent.example/", "<html><body></body></html>");+ let frame = FrameRealm::new(+ &mut parent,+ 1,+ 0,+ "https://parent.example/f",+ "<html><body></body></html>",+ )+ .expect("frame realm");+ // Reaching this line at all (no SIGSEGV) is the regression check.+ frame+ .execute_script(&mut parent, "Promise.reject(new Error('boom')); 'ok'")+ .unwrap();+ }++ #[test]+ fn a_frame_dynamic_import_does_not_crash_the_process() {+ let mut parent = page("https://parent.example/", "<html><body></body></html>");+ let frame = FrameRealm::new(+ &mut parent,+ 1,+ 0,+ "https://parent.example/f",+ "<html><body></body></html>",+ )+ .expect("frame realm");+ frame+ .execute_script(+ &mut parent,+ "import('data:text/javascript,export default 1').catch(() => {}); 'ok'",+ )+ .unwrap();+ }+/// A frame posting to `parent` must reach the page, arrive trusted, and/// carry the frame's origin. Turnstile and every widget like it drop an/// untrusted message silently, so an untrusted delivery is not a cosmeticdiff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 6d59cbdf..a7c0ae2d 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -664,6 +664,46 @@ impl ObscuraJsRuntime {/// ops table, and its bootstrap captured that exact object, so filling the/// `ops` table on it is enough to give every shim in that realm a working/// op surface. The functions are shared, not copied: same isolate.+ /// deno_core's isolate-global promise-reject and dynamic-`import()`+ /// callbacks read per-context state from V8 embedder-data slots 1+ /// (`ContextState`) and 2 (`ModuleMap`) of the *current* context. A+ /// snapshot-created frame realm leaves those slots null, so a promise+ /// rejection or dynamic import inside a frame null-dereferenced in+ /// deno_core's `clone_rc_raw` and segfaulted the whole process (#850, #841).+ /// Alias the main context's slot pointers into the frame context so those+ /// callbacks resolve to the main realm's state instead of crashing.+ ///+ /// Safe: the frame context only *borrows* these pointers. The callbacks+ /// `clone_rc_raw` (increment then drop) them — net-zero on the strong count+ /// — and obscura never tears a frame context down through deno_core's+ /// `Rc::from_raw` destroy path (only the main realm owns and frees the Rc),+ /// so no double-free is possible.+ pub(crate) fn share_deno_context_state_with_realm(+ &mut self,+ realm: &deno_core::v8::Global<deno_core::v8::Context>,+ ) {+ use deno_core::v8;+ // deno_core::runtime::jsrealm CONTEXT_STATE_SLOT_INDEX / MODULE_MAP_SLOT_INDEX.+ const CONTEXT_STATE_SLOT_INDEX: i32 = 1;+ const MODULE_MAP_SLOT_INDEX: i32 = 2;++ let main = self.runtime().main_context();+ let mut entered = self.runtime();+ let isolate = entered.v8_isolate();+ let scope = &mut v8::HandleScope::new(isolate);+ let main_ctx = v8::Local::new(scope, main);+ let realm_ctx = v8::Local::new(scope, realm);+ // SAFETY: slots 1/2 on the main context are `Rc::into_raw` pointers set+ // by deno_core at runtime construction; they outlive every frame realm.+ // We only copy (alias) them and never reconstruct the Rc from the frame.+ unsafe {+ let cs = main_ctx.get_aligned_pointer_from_embedder_data(CONTEXT_STATE_SLOT_INDEX);+ let mm = main_ctx.get_aligned_pointer_from_embedder_data(MODULE_MAP_SLOT_INDEX);+ realm_ctx.set_aligned_pointer_in_embedder_data(CONTEXT_STATE_SLOT_INDEX, cs);+ realm_ctx.set_aligned_pointer_in_embedder_data(MODULE_MAP_SLOT_INDEX, mm);+ }+ }+pub(crate) fn share_ops_with_realm(&mut self,realm: &deno_core::v8::Global<deno_core::v8::Context>,