Repository navigation
fix(example): render guest values without executing guest code - #28
Merged
Merged
Conversation
Inspecting a value to draw it is the same problem shape as serializing it: the host reaches into values the guest built, so every dynamic lookup dispatches through guest-patchable prototypes and runs guest code — here, during render. The serialization work in vercel/workflow#3257 surfaced several instances the inspector still had. - `toString()` on a Date/RegExp/symbol performed a JavaScript string conversion, dispatching to the guest's `toString`. It now calls the intrinsic captured before user code ran: identical output, unpatchable lookup. (A patched `Date.prototype.toString` previously rendered its own return value.) - Map/Set iteration went through `Symbol.iterator` plus repeated `.next()` calls — arbitrary guest code per render. Genuine Maps and Sets now iterate with the captured `forEach`, which walks the internal entry list and never touches the iterator protocol. Non-branded iterables still use the protocol, since that is where their data lives. - `getConstructorName` read `.constructor.name`: two [[Get]]s that can invoke guest getters, and trivially spoofed (`{ constructor: Map }` rendered as "Map"). Branded types now report their engine brand, and the unbranded fallback reads both hops through descriptors. - `length` and `getFunctionName` used [[Get]], so a `length`/`name` accessor ran while sizing a preview. Both read own data properties now. Also fixes a latent bug: `isObjectPrototype` compared `handle.ptr` against a cached `Object.prototype` pointer, but `.ptr` is each handle's own JSValue box — it differs for every handle, including two handles to the same object, so the guard never fired (and the cached pointer had already been freed). It now compares `handle.identity`. Intrinsics are captured immediately after VM creation, before user code runs, and the Map/Set visitor uses `newEphemeralFunction` so re-rendering does not accumulate host callbacks.
Contributor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reading the value's own `constructor` still honoured a spoof:
`{ constructor: Map }` rendered as "Map". Take the constructor from the
prototype instead, which is how DevTools derives the label — real class
instances still report their class, and the spoof reports Object.
The change is confined to examples/browser, which is not part of the published package (`files` is dist + quickjs.wasm + extensions), so no release is needed.
This branch was successfully deployed
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.
Summary
Rendering a guest value in the browser example is the same problem shape as serializing one: the host reaches into values the guest built, so every dynamic lookup dispatches through guest-patchable prototypes and executes guest code — here, while drawing the UI. Guest code can therefore both observe that it is being inspected and control what the inspector displays.
#24 and #26 fixed the classification and property-read paths (engine brand checks, descriptor reads, trap-free proxy rendering). This PR closes the remaining lookups, which the side-effect-free serialization work in vercel/workflow#3257 surfaced while auditing the same class of problem.
What was still executing guest code
toString(Date/RegExp/symbol)toString. A patchedDate.prototype.toStringrendered its own return valueiterate(Map/Set)Symbol.iterator+ repeated.next()— arbitrary guest code per render, and free to misreport the contentsforEach, which walks the internal entry list and never touches the iterator protocolgetConstructorName.constructor.name— two[[Get]]s that can invoke getters, and spoofable:{ constructor: Map }rendered as "Map"length[[Get]], so alengthaccessor ran just to size a previewgetFunctionName[[Get]]onname, so anamegetter could inject text into the UIIntrinsics are captured immediately after
QuickJS.create(), before any user code runs. The Map/Set visitor usesnewEphemeralFunction, so re-rendering a tree of Maps doesn't accumulate host callbacks.Latent bug fixed
isObjectPrototype— the guard that stops__proto__recursion — comparedhandle.ptragainst a cachedObject.prototypepointer. But.ptris each handle's own JSValue box, so it differs for every handle, including two handles to the same object; and the cached one had already been freed by thedispose()on the following line. The guard never fired. Measured:Now compares
handle.identity, which is what that API was added for.Reproducing it
Paste into the demo editor and Run, then expand the
map/setrows:☠️ PATCHED Date.prototype.toStringTue Nov 14 2023 …☠️ PATCHED RegExp.prototype.toString/ab+c/giSymbol(hi){☠️ fake key → never in the map}{k → v}{☠️ fake element}{1, 2}MapObjectƒ ☠️ PATCHED function name getter()ƒ ()PointPoint(control — the useful case is unaffected)ƒ ()is the intended new behavior rather than a regression: the patchednameis an accessor, and refusing to invoke it means the guest can no longer inject text into the UI, at the cost of rendering that pathological function as anonymous. Functions with an ordinarynameare unaffected.Validation
tscandvite buildclean.Not addressed
vm.withScope()isn't applicable at those boundaries yet.