Repository navigation
Conversation
A contextified vm context exposes its global through a JSGlobalProxy that delegates property access to the sandbox. JSC's default preventExtensions made that global non-extensible, so Object.freeze/seal/preventExtensions on the context's globalThis silently succeeded and then broke the global-to-sandbox delegation (any later global write threw). Override preventExtensions on NodeVMGlobalObject to fail for contextified contexts, matching V8's global proxy, so Object.freeze(globalThis) throws a TypeError and the context stays usable. DONT_CONTEXTIFY contexts keep a real, freezable global. Closes #33075
|
Warning Review limit reached
Next review available in: 10 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (2)
WalkthroughAdds a ChangesVM context behavior
Documentation updates
Suggested reviewers
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
Comment |
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Workflows to automatically generate PRs for you. |
There was a problem hiding this comment.
LGTM — small, focused method-table override following the same pattern as the neighboring overrides, with good test coverage.
Extended reasoning...
Overview
This PR adds a preventExtensions static override to NodeVMGlobalObject so that Object.freeze/seal/preventExtensions(globalThis) inside a contextified node:vm context fail with a TypeError (matching Node/V8) instead of silently making the proxy-backed global non-extensible and corrupting subsequent writes. The substantive change is ~12 lines across NodeVM.h/NodeVM.cpp plus 5 new test cases in vm.test.ts. The two docs/ diffs are pure autofix.ci whitespace reflow (table column realignment, code-block indentation) with no content changes.
Security risks
None. The change makes behavior strictly more conservative — it prevents an operation that previously left the vm global in a broken state. No new data flows, no auth/crypto/permissions surface. The uncheckedDowncast is safe because method-table dispatch guarantees the receiver type, and it's the exact pattern used by every neighboring override (put, defineOwnProperty, deleteProperty, etc.) in the same file.
Level of scrutiny
Low-to-moderate. This is a narrow Node.js-compat fix in the JSC bindings: one new method-table slot that returns false for contextified globals and delegates to Base::preventExtensions for DONT_CONTEXTIFY. CREATE_METHOD_TABLE(NodeVMGlobalObject) picks the static up automatically; no StructureFlags change is needed for preventExtensions. Returning false is the standard JSC contract — ObjectConstructor converts it to a TypeError, and Reflect.preventExtensions will correctly return false, both matching Node.
Other factors
- Tests cover all three entry points, post-rejection usability, and the
DONT_CONTEXTIFYcarve-out. The cross-realm.name === "TypeError"assertion is the right approach. - The CI
build-rustfailures are infrastructure-related (this PR touches no Rust) and not caused by this change. - No CODEOWNERS apply to the touched files.
- The bug-hunting system found no issues.
…Y global vm.runInNewContext(code, DONT_CONTEXTIFY) first contextifies via createContext, which returns the context's special sandbox, then hands that sandbox to Script.runInNewContext. That path re-contextified the sandbox into a fresh contextified global, so globalThis was no longer the vanilla DONT_CONTEXTIFY global and Object.freeze(globalThis) threw instead of succeeding. Detect the special sandbox and run in its existing non-contextified context, matching runInContext and Node. The normal create-a-fresh-context path is unchanged, so contextCodeGeneration options still apply.
There was a problem hiding this comment.
I didn't find issues with the logic, but this touches native JSC bindings for node:vm sandbox semantics (and the latest CI run for bb83f23 isn't reflected in robobun's comment yet), so it's worth a human look before merging.
Extended reasoning...
Overview
This PR adds a preventExtensions override to NodeVMGlobalObject so that Object.freeze/seal/preventExtensions(globalThis) inside a contextified node:vm context returns false (causing JSC's ObjectConstructor to throw a TypeError) instead of silently making the proxy-backed global non-extensible and corrupting subsequent writes. DONT_CONTEXTIFY contexts delegate to the base implementation and remain freezable, matching Node. A follow-up commit (bb83f23) updates scriptRunInNewContext to detect when the context argument is already a NodeVMSpecialSandbox (which happens because the JS-side vm.runInNewContext calls createContext() before forwarding to the native script.runInNewContext) and reuse its parent global instead of re-contextifying it as a vanilla sandbox — without this, the new preventExtensions guard would incorrectly fire on DONT_CONTEXTIFY globals routed through runInNewContext. The two docs files are pure autofix whitespace reflow. New tests cover all three Object.* entry points, post-rejection extensibility, and both DONT_CONTEXTIFY paths.
Security risks
node:vm is sandboxing-adjacent. The change makes the contextified global more restrictive (refusing to become non-extensible), which is a hardening rather than a relaxation, and matches V8/Node semantics. The scriptRunInNewContext change reuses an existing NodeVMGlobalObject rather than creating a fresh one when handed a NodeVMSpecialSandbox; this mirrors what runInContext already does for the same input via getGlobalObjectFromContext. I don't see a new escape vector, but any change to vm global-object method-table hooks deserves a maintainer's eye.
Level of scrutiny
Medium-high. The diff is small and follows the established pattern of the other method-table overrides on NodeVMGlobalObject (put, defineOwnProperty, deleteProperty, getOwnPropertyNames), and CREATE_METHOD_TABLE will pick up the static preventExtensions automatically. Still, this is hand-written C++ in JSC bindings affecting global-object semantics across realms.
Other factors
- robobun's CI comment reports
test-vm-context-dont-contextify.jsfailing across all platforms for commitbc9257b0; commitbb83f23dwas pushed afterward specifically to address that path (theNodeVMSpecialSandboxreuse), and the newvm.test.tscase "runInNewContext with DONT_CONTEXTIFY can still be frozen" exercises exactly that scenario. However, the updated CI results forbb83f23daren't visible in the timeline yet, so green CI on the latest commit should be confirmed. - No CODEOWNERS entries cover these paths.
|
For reviewers: the two |
|
Update (build #66868): this run went broadly red across platforms, but the failing tests are all unrelated to |
vm.runInNewContext(code, context, options) contextifies via createContext before running, and createContext reads options.codeGeneration, but runInNewContext receives the setting under options.contextCodeGeneration. Previously scriptRunInNewContext re-read the correct key on its fall-through path, masking the mismatch. With the DONT_CONTEXTIFY special-sandbox reuse fast-path, the already-built context is reused, so a dropped setting meant eval/Function/wasm stayed enabled when the caller disabled them. Alias contextCodeGeneration to codeGeneration before createContext (mirroring Node's getContextOptions) so the reused context honors it.
The previous commit aliased contextCodeGeneration to codeGeneration before createContext validated it, so an invalid value reported the internal `options.codeGeneration` name (which the caller never passed) instead of `options.contextCodeGeneration`. Validate the value (and its strings/wasm fields) under the user-facing name before aliasing, matching Node.
|
Re-verified this against current main (b5afcac), since the same bug came up again (guest-side
Note for whoever lands this: #34623 removes |
|
#39588 reworks how a contextified global resolves against its sandbox and covers this case. It adds the |
What does this PR do?
Fixes #33075. Inside a contextified
vmcontext,Object.freeze(globalThis)silently succeeded instead of throwing, and the freeze then corrupted the context so any later global write threw.Cause: a contextified context exposes its global through a
JSGlobalProxythat delegates property access to the sandbox.NodeVMGlobalObjectoverrodegetOwnPropertySlot,put,defineOwnProperty,deleteProperty, andgetOwnPropertyNames, but notpreventExtensions. So JSC's defaultJSObject::preventExtensionsran, made the object non-extensible, andObject.freeze/seal/preventExtensionssucceeded.Fix (commit 1): override
NodeVMGlobalObject::preventExtensionsto returnfalsefor contextified contexts, matching V8's global proxy (which can never become non-extensible). JSC'sObjectConstructorthen throws aTypeErrorfor each entry point and the context stays usable.DONT_CONTEXTIFYcontexts have a real global object, which Node lets you freeze, so those delegate to the base implementation.Fix (commit 2): the first fix surfaced a latent bug.
vm.runInNewContext(code, DONT_CONTEXTIFY)first contextifies viacreateContext(which returns the context's special sandbox), then hands that sandbox toScript.runInNewContext, which re-contextified it into a fresh contextified global. SoglobalThiswas no longer the vanillaDONT_CONTEXTIFYglobal andObject.freeze(globalThis)started throwing.scriptRunInNewContextnow detects the special sandbox and runs in its existing non-contextified context, matchingrunInContextand Node. The normal create-a-fresh-context path is unchanged, socontextCodeGenerationoptions still apply.Note: the thrown message is JSC's ("Unable to prevent extension in Object.freeze") rather than Node's exact "Cannot freeze". The error type (
TypeError) and behavior now match;preventExtensionsdoesn't know whichObject.*entry point called it, so exact message parity would need separate plumbing.How did you verify your code works?
New cases in
test/js/node/vm/vm.test.ts:Object.freeze/seal/preventExtensions(globalThis)each throw aTypeErrorin a contextified contextrunInContextandrunInNewContextthrow for a contextified global and allow freezing aDONT_CONTEXTIFYglobal, matching NodeAlso ran the full
node:vmparallel compat suite (test/js/node/test/parallel/test-vm-*.js, 95 files) to green, includingtest-vm-context-dont-contextify.jsandtest-vm-codegen.js.