fix(runtime): safe-marshal hook ctx so circular host handles don't crash the sandbox (#2674)#2676
Merged
Merged
Conversation
…ash the sandbox (#2674) `QuickJSScriptRunner.installCtx` serialised the hook context into the sandbox with a bare `JSON.stringify`. A live `setTimeout`/`setInterval` handle reachable from `ctx` links back on itself (`Timeout._idlePrev -> TimersList._idleNext -> …`), so `JSON.stringify` threw `TypeError: Converting circular structure to JSON` and took the whole hook down. Route all three host→VM marshalling paths (`setGlobalJson`, `setObjectJson`, `jsonToHandle`) through a shared `safeJsonStringify` that drops circular back-edges via a path `WeakSet` and coerces `BigInt` to a string. Only JSON-safe leaves cross the boundary — unserialisable host objects are stripped rather than fatal — and the body still runs. Closes #2674. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 1 package(s): 15 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a sandboxed hook executes,
QuickJSScriptRunner.installCtxmarshals the hookctxinto the QuickJS sandbox viasetGlobalJson/setObjectJson, each of which calls a bareJSON.stringify(v ?? null). If the context (or anything reachable from it) holds a livesetTimeout/setIntervalhandle, the timer object links back on itself (Timeout._idlePrev -> TimersList._idleNext -> …) andJSON.stringifythrows:The throw happens before the existing
evalCodeerror guard, so it escapes as an opaque circular-reference crash that takes the whole hook down (#2674).Fix
Introduce a shared
safeJsonStringifyhelper and route all three host→VM marshalling paths through it —setGlobalJson,setObjectJson, andjsonToHandle. It uses aJSON.stringifyreplacer that:WeakSetand drops circular back-edges (the timer case), andBigIntto a string (plainJSON.stringifythrows onBigInt).Only JSON-safe leaves are meant to cross the sandbox boundary anyway, so anything unserialisable is stripped rather than fatal, and the hook body still runs.
Tests
Added two regression tests to
quickjs-runner.test.ts:ctx.inputcontaining a live circularsetIntervalhandle marshals without crashing and the body reads the sibling fields.BigIntinctx.inputis coerced to a string instead of throwing.pnpm vitest run src/sandbox/quickjs-runner.test.ts→ 26 passed.turbo run build --filter=@objectstack/runtime(incl. strict DTS) green.Closes #2674.
🤖 Generated with Claude Code