Skip to content

Give frames origins and messages, and load cross-origin frames in isolates - #190

Open
brainkim wants to merge 27 commits into
mainfrom
feat/frame-origins
Open

brainkim wants to merge 27 commits into
mainfrom
feat/frame-origins

Conversation

@brainkim

@brainkim brainkim commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

Frames get origins, cross-origin windows and postMessage, and an <iframe src> of another origin loads in a V8 isolate of its own where isolated-vm is installed. The branch adds no options, exports, package entries or event types: a page reaches a frame as the web has it, through load, postMessage, sandbox and frame-src.

DuckDuckGo's result click is what started this: it posts to a hidden frame, which checks event.source === parent and sets parent.location.href.

Origins and isolation

  • An <iframe sandbox> without allow-same-origin gets an opaque origin of its own, so its origin is "null". A frame inside a sandboxed frame stays sandboxed.
  • srcdoc and about:blank frames take their creator's origin. Before, every srcdoc frame reported "null".
  • Across origins, contentDocument is null, and contentWindow, frames[i], named frame access, and a frame's parent and top give a cross-origin window. It allows only HTML's cross-origin properties and child frames by index. Its location allows only setting href and replace(). Anything else throws SecurityError. In a cross-origin frame, frameElement is null.

Messages and navigation

  • postMessage works both ways, with HTML's target-origin check, and is delivered as a task. origin is the sender's, and source is the sender as the receiver sees it.
  • Without node:vm (isolated-vm, for one), frames get a real window in the engine's realm. They're also drawn, which they weren't before.
  • A frame setting parent.location goes through the parent's navigate algorithm, so its navigate event fires. A sandboxed frame can navigate its top only with allow-top-navigation, or allow-top-navigation-by-user-activation during a user action.

Frames in isolates

  • An <iframe src> with an http: or https: URL of another origin than the page's loads its document in an isolate, where the isolated-vm package is installed and loads. That means Node 24 or later only, started with --no-node-snapshot. Elsewhere the frame shows an empty about:blank, as every src frame did before. There is no option: installing isolated-vm turns it on.
  • The frame's document, stylesheets, images, scripts and modules are loaded by the page, under its csp (now including frame-src, with the child-src fallback) and through its fetch listeners, without the headers Fetch forbids.
  • The frame lays out, runs its scripts and paints in its isolate. The page draws the cells it posts, treating them as untrusted: it rebuilds each cell's text and width and drops control characters, bad code points and image placeholders the frame didn't post an image for. Images go out in the kitty protocol where the terminal draws it, and in half blocks elsewhere.
  • Keys, the mouse, pastes and focus go to a focused frame. To the page its window is always cross-origin: contentDocument is null, and the two talk through postMessage. A link or form in the frame loads the next document in a new isolate.
  • Each isolate has 256 MB of heap and each call into it 30 seconds. A frame past either is stopped and shows nothing, and the page goes on.

Behavior changes for embedders

  • A sandboxed frame without allow-same-origin can't be read from its parent: contentDocument is null, and contentWindow.getSelection() throws. Adding allow-same-origin restores both.
  • srcdoc and about:blank frames resolve URLs against the parent's base URL, per HTML's fallback base URL.
  • <iframe name=x> makes window.x the frame's window, and the frame's window.name starts as the attribute.
  • With isolated-vm installed, a cross-origin <iframe src> that showed nothing now loads and runs.

Packaging

  • isolated-vm is an optional peer dependency.
  • build also bundles the frame runtime into dist/isolate-frame.js. A checkout bundles it from source on first use, with esbuild.

Limit: without node:vm, a message sent by calling postMessage directly on a same-origin window is attributed to the top window, because TermDOM can't tell whose code is calling. Messages through a cross-origin window are attributed correctly.

Tests and CI

  • New: tests/frame-origins.test.ts (one test runs the DuckDuckGo flow in a child process with node:vm hidden), tests/isolated-frames.test.ts, tests/isolated-vm-frames.test.ts, and one test in tests/iframe-realms.test.ts.
  • All seven CI jobs pass.
  • isolated-vm 7 asks for Node 24. On the Node 20.19 job its install script tried to build a binary and failed the install, so that job installs with --ignore-scripts and the tests that need an isolate skip below Node 24. They run on the Node 26 job.

The frames section in docs/guides/06-images-and-canvas.md is new.

🤖 Generated with Claude Code

brainkim and others added 2 commits November 9, 2026 19:14
A sandbox without allow-same-origin gives a frame an opaque origin, as
HTML's sandboxed origin browsing context flag does, and a frame inside a
sandboxed one keeps the sandbox. A srcdoc or about:blank frame takes the
origin of the document that made it. Windows of two origins hold each
other through a cross-origin WindowProxy that lets through only HTML's
CrossOriginProperties and throws a SecurityError for the rest, so a
sandboxed frame's contentDocument is null and its parent cannot reach
into it, or it into the parent. Each viewer gets one proxy per window,
so contentWindow compares equal to itself.

postMessage checks the target origin against these origins, gives the
event the sender's serialized origin, and gives as its source the sender
as the receiver holds it, so a frame's event.source === parent holds.
A frame may set its parent's location across origins, which goes through
the parent's navigate event, and a sandbox without allow-top-navigation
stops it with a SecurityError. A srcdoc frame resolves URLs against its
parent's base URL, as HTML's fallback base URL does.

Where the runtime has no node:vm, as in isolated-vm, a frame got a bare
object with no postMessage, location or parent. It gets a whole window
built in the engine's realm, which is what DuckDuckGo's result click
needs: a message to a hidden frame that sends the page to the result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
The guides said nothing about iframes. A reader needs to know that a
frame's src is not loaded, where a frame gets a realm of its own, what a
sandbox does to its origin, what a window of another origin lets through,
and how messages and a parent's navigation cross between frames.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
brainkim and others added 18 commits November 10, 2026 13:26
A frame's engine read the terminal's colors, cell and loader off the
document its iframe is in. A frame drawn in another isolate has no such
document, so createFrameEngine takes them as settings, and the iframe's
factory fills them from its document.

exportGrid and importGrid carry a painted grid to another heap with the
text of the clusters it interns. importGrid takes the cells as hostile:
it rebuilds each cell's text and width, and drops control characters,
bad code points and image placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
TermDOM never loaded an iframe's src, so an embedder that wanted pages
in frames, with limits on what each could take, ran a TermDOM per page
and showed it through a pty embed. Every frame then went out as escape
codes and came back through an emulator, and the page could not treat
the program as a frame.

The isolatedFrames option takes a function that starts a document in an
isolate. An iframe whose src is an http: or https: URL loads it under
the page's policy, now including frame-src, and its fetch listeners, and
starts it there. The page draws the cells the frame paints, checked as
untrusted, and hands it keys, the mouse, pastes and focus as an embed's
program takes them. The page holds its window as one of another origin,
so the two reach each other through postMessage, and the frame's own
loads come back through the page's listeners without forbidden headers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
The runtime a bare V8 isolate needs before TermDOM loads in it: timers,
events, text encoding, Blob and FormData, fetch through the host,
decompression, streams, crypto and console, each from a plain callback
the host provides. The frame entry builds the document from its markup,
paints it with createFrameEngine and posts the cells, and dispatches the
input, messages and responses the host sends it.

A blank window with the page's origin stands in for the frame's parent,
so parent, top, postMessage's origin check and parent.location go
through the engine's own cross-origin windows. What the frame posts to
it, or navigates it to, goes to the host.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
createIsolatedVMFrames resolves to a FrameIsolator. Each frame gets an
isolate with a memory limit, and every call into it a time limit. The
backend drives the frame's timers, randomness and logs, and runs the
scripts the frame reports in document order: classic scripts, modules
through the page's import maps, and dynamic imports. Their sources, and
everything else the frame fetches, are loaded by the page.

Inside the isolate, the frame copies its window onto the global object
for its scripts, and sets currentScript, readyState and the load events
as the backend reports each script's start and end.

The build bundles the runtime into dist/isolate-frame.js. A checkout
bundles it from source on first use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
The frame runtime and the isolated-vm backend speak the page's protocol:
the frame learns its parent's origin, says whether it navigates itself
or its parent, and says when its isolate stopped. "auto" loads the
backend with the first frame, as an optional peer dependency, and a
frame whose backend cannot load crashes and says why.

A frame that sends its parent elsewhere is held to its sandbox through
the parent's own navigation, and the page gives the frame the cell size
in CSS pixels its layout divides by.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
The frame's stand-in exchange gets editing and the document its
autofocus, as a TermDOM's own do, and the reduced motion the page has.
A click arrives in the frame's cells from zero and goes to the engine
from one, as a terminal reports it. A frame stopped for time, memory or
a failed start posts crash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
TermDOM leaves <link rel=stylesheet> to whoever loads the document. The
frame fetches each one through the page, puts its rules after the link
with URLs made absolute, and holds its first paint up to five seconds
for them. Links added later load as they come.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
A form posted from a frame navigates with its method, its fields in
the encoding its enctype names, and their content type. pushState,
replaceState and a fragment change send history with the new URL, and
no longer pass for navigations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
FrameInit.limits overrides the backend's memory and time limits for
one frame. stats() gives the frame's CPU time, its longest task and
its heap, or null once it is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
Where the page sends kitty images, the frame holds its images in a
table of its own and paints the placeholder cells that name them, as a
page does. After each paint it posts the images placed since the last
one, with their pixels, and the ids it no longer shows, and the page
places them under its own ids. Frames in the page's own heap still
paint images in cells, since their framebuffers have no table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
A frame in an isolate posts its images' pixels and paints placeholder
cells that name them by its own ids. The page keeps each image it posts
once its size checks out, and importGrid hands each placeholder to the
frame, which turns them into the grid images a pty embed's program
shows. So the page sends them in the kitty protocol where the terminal
draws it, and in half blocks elsewhere, and a placeholder for an image
the frame never posted draws nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
The engine names a request's destination as a property of its own, and
the frame's fetch copied the request from its internal state instead,
so an image went to the page with no destination.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
The page draws a frame's images in kitty or in cells as its terminal
can, so the frame paints placeholders whenever the page asks for kitty,
and not only in 24-bit color.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv648YAe52aBTWbgPfn
A page that shows web pages can keep an address bar and a tab title
for its frame, and can keep the frame from going somewhere. A form the
frame posts loads its next document with that POST. An embedder that
wants other limits, or the frames' logs, takes createIsolatedVMFrames
from @b9g/termdom/isolated-vm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
A page that bundles TermDOM for a platform other than Node failed on
the isolate backend's imports of node:crypto and node:fs/promises, and
would go on to isolated-vm and esbuild. They come through the process
now, as node-pty does, and randomness comes from the web's crypto.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
An iframe whose src is of another origin loads in an isolate where
isolated-vm is installed and loads, and shows about:blank elsewhere. Its
page reaches it as the web has it: load, postMessage, sandbox and
frame-src. The isolatedFrames option, the exported message types, the
@b9g/termdom/isolated-vm entry and the iframe's titlechange, navigate
and historychange events go.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5bxvWWH7aApVCbS3z
Nothing sets FrameInit.limits or calls stats() now that the page starts
the isolated-vm backend itself, with no option an embedder reaches. Each
frame is held to the backend's limits, as the guide says.

Co-Authored-By: Claude <noreply@anthropic.com>
@brainkim brainkim changed the title Give frames origins, cross-origin windows and messages Give frames origins and messages, and load cross-origin frames in isolates Nov 11, 2026
brainkim and others added 2 commits November 12, 2026 13:10
isolated-vm 7 asks for Node 24. On CI's Node 20.19 leg its install
script tried to build a binary and failed the install, so no test ran.
That leg now installs without scripts, the tests that need an isolate
skip under Node 24, and the guide gives the version. The lockfile
takes the optional peer it was missing.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig
A URL's pathname on Windows is /D:/..., which no file has.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig
@brainkim
brainkim marked this pull request as ready for review November 12, 2026 18:17
brainkim and others added 3 commits November 12, 2026 14:29
A checkout bundles the runtime with esbuild, which was handed a URL's
pathname. On Windows that is /D:/..., which esbuild could not resolve,
so no frame loaded in an isolate there.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig
Under Bun on Windows the message had not arrived by then.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig
brainkim and others added 2 commits November 12, 2026 16:26
The test held every call into a frame to 200 milliseconds so that the
frame that loops forever was stopped quickly. The limit is on the clock
and covers a frame's start as well, its first parse, layout and paint in
a cold isolate, and on a busy machine the calm frame took longer than
that and was stopped too, so the test never saw it draw. With every core
busy the old limit failed 11 runs of 15, and limits of 500 milliseconds
and up failed none.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GVbRv5n8btiCtaRBtLrLig

This branch has not been deployed

No deployments
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.

1 participant