Skip to content

fix(example): render guest values without executing guest code - #28

Merged
TooTallNate merged 3 commits into
mainfrom
inspector-hardening
Jul 31, 2026
Merged

TooTallNate merged 3 commits into
mainfrom
inspector-hardening

Conversation

@TooTallNate

@TooTallNate TooTallNate commented Jul 31, 2026 •

Copy link
Copy Markdown
Collaborator

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

Hook Before Now
toString (Date/RegExp/symbol) JS string conversion → guest toString. A patched Date.prototype.toString rendered its own return value intrinsic captured at VM boot, invoked with an explicit receiver — identical output, unpatchable lookup
iterate (Map/Set) Symbol.iterator + repeated .next() — arbitrary guest code per render, and free to misreport the contents captured forEach, which walks the internal entry list and never touches the iterator protocol
getConstructorName .constructor.name — two [[Get]]s that can invoke getters, and spoofable: { constructor: Map } rendered as "Map" engine brand for branded types; otherwise the constructor is taken from the prototype (how DevTools derives it), read through descriptors
length [[Get]], so a length accessor ran just to size a preview own-data-property read
getFunctionName [[Get]] on name, so a name getter could inject text into the UI own-data-property read

Intrinsics are captured immediately after QuickJS.create(), before any user code runs. The Map/Set visitor uses newEphemeralFunction, so re-rendering a tree of Maps doesn't accumulate host callbacks.

Latent bug fixed

isObjectPrototype — the guard that stops __proto__ recursion — compared handle.ptr against a cached Object.prototype pointer. But .ptr is 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 the dispose() on the following line. The guard never fired. Measured:

ptr matches (old check):         false
identity matches (new API):      true
two live handles, ptr equal:     false
two live handles, identity eq:   true

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/set rows:

Date.prototype.toString = () => '☠️ PATCHED Date.prototype.toString';
RegExp.prototype.toString = () => '☠️ PATCHED RegExp.prototype.toString';
Symbol.prototype.toString = () => '☠️ PATCHED Symbol.prototype.toString';

Map.prototype[Symbol.iterator] = function () {
  return [['☠️ fake key', 'never in the map']][Symbol.iterator]();
};
Set.prototype[Symbol.iterator] = function () {
  return ['☠️ fake element'][Symbol.iterator]();
};

const sneaky = function realName() {};
Object.defineProperty(sneaky, 'name', {
  configurable: true,
  get: () => '☠️ PATCHED function name getter',
});

({
  'date → should show Nov 14 2023': new Date(1700000000000),
  'regexp → should show /ab+c/gi': /ab+c/gi,
  'symbol → should show Symbol(hi)': Symbol('hi'),
  'map → expand: should be k → v': new Map([['k', 'v']]),
  'set → expand: should be 1, 2': new Set([1, 2]),
  'fake map → should be labelled Object': { constructor: Map },
  'function → patched name must not render': sneaky,
  'class instance → should still say Point': new (class Point {
    constructor() { this.x = 1; }
  })(),
});
Row Before After
date ☠️ PATCHED Date.prototype.toString Tue Nov 14 2023 …
regexp ☠️ PATCHED RegExp.prototype.toString /ab+c/gi
symbol (blank) Symbol(hi)
map {☠️ fake key → never in the map} {k → v}
set {☠️ fake element} {1, 2}
fake map Map Object
function ƒ ☠️ PATCHED function name getter() ƒ ()
class instance Point Point (control — the useful case is unaffected)

ƒ () is the intended new behavior rather than a regression: the patched name is 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 ordinary name are unaffected.

Validation

  • The before/after table above was produced by running both hook implementations against the same VM, not by inspection.
  • A separate harness patching every member above reports zero guest side effects across the new paths.
  • Suite 1887 passing; tsc and vite build clean.

Not addressed

  • Non-branded iterables still use the iterator protocol — that is where their data lives, the same unavoidable case as getters in the serializer.
  • Handles returned to react-inspector are still owned by it rather than scoped, so vm.withScope() isn't applicable at those boundaries yet.

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.
Copilot AI review requested due to automatic review settings July 31, 2026 18:24
@vercel

vercel Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
quickjs-wasi Ready Ready Preview Jul 31, 2026 6:42pm

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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.
@TooTallNate
TooTallNate merged commit 8439ab7 into main Jul 31, 2026
6 checks passed
@TooTallNate
TooTallNate deleted the inspector-hardening branch July 31, 2026 18:43

This branch was successfully deployed

1 active deployment
Preview — 26c8098d Deployed Jul 31, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants