Repository navigation
fix: crash when a preload removes its own iframe asynchronously - #53838
Conversation
|
@claude review |
…tarts A nodeIntegrationInSubFrames frame's NodeBindings only initializes its dummy_uv_handle_ from the task that first runs the frame's loop. A frame removed before that task ran destroyed a uv_async_t that never went through uv_async_init(), so uv_close() ran on uninitialized memory. UvHandle now value-initializes its handle, and reset() frees one that was never initialized instead of closing it. Fixes #53789.
Preload scripts run inside the callbacks Blink and content use to set up a frame's script contexts. A preload promise reaction or process.nextTick() callback that removed its own frame ran underneath those callbacks and crashed the renderer. - The frame observer holds off microtask checkpoints until those callbacks return. - process.nextTick() callbacks queued during bootstrap run from a microtask afterwards, keeping their existing order after promise reactions. - A frame released from inside one of its own Node.js callbacks has its environment and loop freed from a fresh task.
e9ace6c to
f67d4ef
Compare
6b396f4 to
710611a
Compare
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. Because it's a subtle change to renderer-process Node.js environment lifecycle and microtask/teardown ordering (exactly the kind of crash-fix code where a use-after-free or ordering bug would be easy to miss), a human look would still be worthwhile.
What was reviewed: the new DeferMicrotasksScope wrapping DidClearWindowObject()/DidInstallConditionalFeatures() in electron_render_frame_observer.cc; the InternalCallbackScope-wrapped LoadEnvironment() call and the new RunTicksQueuedDuringSetup() microtask in electron_renderer_client.cc; and the deferred FreeReleasedEnvironments() path in WillReleaseScriptContext() guarding teardown from inside an environment's own callback. Also checked whether the deferred-free guard's own_node_bindings condition could skip the shared/primary-loop frame, and whether FreeFrameEnvironment() needed to enter env->context() explicitly before resetting — both were examined and did not turn up an independently confirmable defect beyond what the inline findings (if any) already cover.
Extended reasoning...
Overview
This PR modifies renderer-process Node.js environment lifecycle handling across three C++ files: shell/renderer/electron_render_frame_observer.cc, shell/renderer/electron_renderer_client.cc, and its header. It adds a stack-allocated DeferMicrotasksScope around Blink's DidClearWindowObject()/DidInstallConditionalFeatures() callbacks, wraps LoadEnvironment() in a node::InternalCallbackScope that skips async hooks/task queues so bootstrap-queued process.nextTick() callbacks don't run underneath Blink's own setup, and reworks WillReleaseScriptContext() to defer freeing a frame's Node environment to a fresh task when the frame is released from inside its own Node.js callback (via a new released_environments_ vector and FreeReleasedEnvironments()/FreeFrameEnvironment()). Test coverage is extended in spec/node-spec.ts with new fixtures for frame self-removal and task ordering.
Security risks
The change is squarely about preventing a use-after-free: a preload script that removes its own iframe asynchronously (from a promise reaction, process.nextTick(), or setImmediate()) previously could free the RenderFrame/window proxy while Blink/content code above it on the stack was still using them. The fix's own correctness is the security surface here — an incomplete fix could leave a narrower UAF window rather than closing it. No new externally-reachable API surface, IPC, or contextBridge exposure is introduced.
Level of scrutiny
This warrants a full, careful human read: it changes ordering guarantees around V8 microtask checkpoints and Node.js's InternalCallbackScope/CallbackScope, touches when/how a node::Environment and its uv loop are torn down, and interacts with GC/lifetime concerns of the kind this repo's CLAUDE.md flags as high-risk (careful ownership around WeakPtr, task-posted teardown, can_call_into_js(false)). It is exactly the category of complex, lifecycle-sensitive renderer code the review guidelines say should not be rubber-stamped by an automated pass alone, even with no findings from this run's bug hunt.
Other factors
The bug-hunting run reported no confirmed bugs, and two candidate concerns (whether FreeFrameEnvironment() needs to enter env->context() before resetting, and whether the own_node_bindings guard could miss the frame sharing the primary/process-wide loop) were investigated and ruled out without independent confirmation of a defect. New spec coverage (preload-remove-own-frame.js, preload-task-order.js, task-order.html, and additions to node-spec.ts) exercises the frame-removal and task-ordering scenarios the PR describes, which is a positive signal, but does not substitute for a human review of the C++ lifecycle logic itself.
This review covers commit e9ace6c, which is no longer the latest commit on this pull request; later commits are not covered by it.
A document Blink finishes inside the commit (about:blank, a media or plain-text response) reaches RunScriptsAtDocumentStart in the same task that created the frame's environment, before any microtask checkpoint, so the nextTick callbacks queued during setup are still pending. The callback scope in EmitEvent then runs them outside any MicrotasksScope, which trips V8's scoped-microtasks check in dcheck builds. Mirror RunScriptsAtDocumentEnd and hold a MicrotasksScope around the emit, and cover the about:blank case in the spec.
|
Release note looks good now. |
|
Generated by Claude Code |
|
Release Notes Persisted
|
Follow-up to #53833, and based on it.
An iframe with
nodeIntegrationInSubFrameswhose preload removed its own frame asynchronously crashed the renderer, whether the removal ran from a promise reaction,process.nextTick(),setImmediate()or another Node.js callback. This predates #52964: 42.3.1 crashes the same way.DidClearWindowObject()/DidInstallConditionalFeatures(). A preload's promise reactions used to run from any native call it made, underneath those callbacks, and one that detached the frame freed theRenderFrameand window proxy they return to. They now run once those callbacks return, still before page scripts.process.nextTick()callbacks queued during bootstrap no longer run as bootstrap's callback scope closes, for the same reason. A microtask queued behind the preload's own promise reactions runs them, keeping the existing order.nextTickcallbacks and page scripts.A preload that removes its own frame synchronously still crashes: that runs inside the same Blink and content callbacks and needs a different fix.
Notes: Fixed a renderer crash when a
nodeIntegrationInSubFramespreload removed its own iframe from a promise,process.nextTick()or other Node.js callback.