Lightpanda: the headless browser designed for AI and automation
uaf: Extend closed popup's lifetime from Frame to Page
Karl Seguin762e21a8f363
diff --git a/src/browser/Page.zig b/src/browser/Page.zigindex 76e739b76..689c6db0b 100644--- a/src/browser/Page.zig+++ b/src/browser/Page.zig@@ -110,6 +110,11 @@ frame: Frame,// to the original page like this.popups: std.ArrayList(*Frame) = .empty,+// Popup Frames that have been closed. The window can still be referenced / used+// from JS, so we defer shutting them down until page tear down (which isn't+// ideal from a memory point of view).+closed_frames: std.ArrayList(*Frame) = .empty,+// Lifecycle state. A Page is `.pending` while we hold it as the in-flight// destination of a root navigation — its V8 context exists but is not yet the// session's active context. Flipped to `.active` by Session.commitPendingPage@@ -142,6 +147,11 @@ pub fn deinit(self: *Page) void {}self.popups = .empty;+ for (self.closed_frames.items) |frame| {+ frame.deinit();+ }+ self.closed_frames = .empty;+self.frame.deinit();const session = self.session;diff --git a/src/browser/Session.zig b/src/browser/Session.zigindex 1070b044d..99d3ce406 100644--- a/src/browser/Session.zig+++ b/src/browser/Session.zig@@ -73,7 +73,6 @@ _active: ?*Page = null,_pending: ?*Page = null,_page_destruction_queue: std.ArrayList(*Page) = .{},-_frame_destruction_queue: std.ArrayList(*Frame) = .{},// Loader IDs are scoped to the Session: each new BrowserContext gets a// fresh counter. Frame IDs (`frame_id_gen`) live on `Browser` instead so@@ -224,16 +223,6 @@ pub fn drainConsoleMessages(self: *Session) []const u8 {}pub fn processDestroyQueues(self: *Session) void {- {- const queue = self._frame_destruction_queue.items;- if (queue.len > 0) {- for (queue) |frame| {- frame.deinit();- }- self._frame_destruction_queue.clearRetainingCapacity();- }- }-{const queue = self._page_destruction_queue.items;if (queue.len > 0) {@@ -266,10 +255,6 @@ fn queuePageDestruction(self: *Session, page: *Page) void {self._page_destruction_queue.append(self.arena, page) catch @panic("OOM");}-pub fn queueFrameDestruction(self: *Session, frame: *Frame) void {- self._frame_destruction_queue.append(self.arena, frame) catch @panic("OOM");-}-// Tear down the currently-active Page. Dispatches `frame_remove` first// so CDP can clear inspector state while the OLD page is still walkable,// then frees the slot and notifies Navigation. Used by removePage anddiff --git a/src/browser/tests/window/open.html b/src/browser/tests/window/open.htmlindex 864339ac8..7b99f13e4 100644--- a/src/browser/tests/window/open.html+++ b/src/browser/tests/window/open.html@@ -108,6 +108,32 @@}</script>+<script id=stale_window_after_close>+{+ // A closed popup's Window is often still referenced by JS. We must NOT tear+ // the Frame down (free its V8 context / arena / refcounted Location URL)+ // while that reference is live, or a later access is a use-after-free. The+ // Frame is kept alive (neutered) until Page.deinit instead.+ //+ // Crash condition: do NOT read .location before close(), so the popup's URL+ // holds only the single internal ref. Pre-fix, the deferred Frame.deinit+ // released it (refcount -> 0, URL freed) and reading .location afterwards+ // re-wrapped the dangling Location into a double-free (release overflow).+ const w = window.open('about:blank');+ const doc = w.document;+ const node = doc.createElement('section');++ w.close();+ testing.expectEqual(true, w.closed);++ // Everything reachable through the stale Window stays valid and inert.+ testing.expectEqual('about:blank', w.location.href);+ testing.expectEqual('BODY', doc.body.tagName);+ testing.expectEqual('SECTION', node.tagName);+ testing.expectEqual('P', doc.createElement('p').tagName);+}+</script>+<script id=popup_self_close type=module>{// Popup navigates to a real URL (not about:blank, which takes a synchronousdiff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zigindex 49510a081..ed7b695a6 100644--- a/src/browser/webapi/Window.zig+++ b/src/browser/webapi/Window.zig@@ -108,8 +108,10 @@ _cross_origin_wrapper: CrossOriginWindow,// we're still alive. Only valid if `!_opener.?._closed`._opener: ?*Window = null,-// True after our Frame has been deinit'd by window.close. Many things on the-// window become invalid once this is true.+// True after window.close. The Frame itself stays alive (parked in+// page.closed_frames until Page.deinit) so cached references to this Window+// don't dangle, but the popup is neutered: dropped from page.popups, its+// transfers aborted, scheduler reset, and unreachable for events / name lookup._closed: bool = false,// Popup name (owned by page.arena)@@ -601,9 +603,8 @@ pub fn close(self: *Window) void {_ = page.popups.swapRemove(popup_index);- // Drop any pending queued navigation for this frame. Frame.deinit will- // release the QueuedNavigation arena, but the entry in page.queued_navigation- // would otherwise have processQueuedNavigation re-deinit the popup.+ // Drop any pending queued navigation for this frame, otherwise+ // processQueuedNavigation would try to re-navigate the closed popup.if (frame._queued_navigation != null) {for (page.queued_navigation.items, 0..) |f, i| {if (f == frame) {@@ -614,13 +615,15 @@ pub fn close(self: *Window) void {}frame.js.scheduler.reset();-- // We can't tear the Frame down here — close() is invoked from JS still- // running on top of this Frame's V8 context, often deep inside a script- // eval whose parser is still holding the Frame. Destroying the context- // now leaves dangling pointers in the unwinding script eval (load event- // dispatch, runMacrotasks, etc.). Defer to Page.deinit instead.- page.session.queueFrameDestruction(frame);+ frame.abortTransfers();++ // Do not teardown the frame. The Window can still be referenced in JS+ // (window.open(...) returns the window). It would be nice to be able to+ // free this now (just like it would be nice to be more pro-active about+ // freeing workers and iframes), but, for now, we don't have the+ // infrastructure to do this safely so doing it on page tear down is our+ // only option.+ page.closed_frames.append(page.frame_arena, frame) catch @panic("OOM");}pub fn postMessage(self: *Window, message: js.Value, target_origin: ?[]const u8, transfer: ?[]const *MessagePort, frame: *Frame) !void {