Skip to content

Latest commit

 

History

History
241 lines (195 loc) · 28 KB

sandbox-protocol.md

File metadata and controls

241 lines (195 loc) · 28 KB

The sandbox's frame protocol

Status: accepted (2026-10-30; revised the same day from a star, where every frame believed the host's page, to a tree; decisions 5 to 7 added 2026-11-24, from the codebase audit). The messages between the windows of the sandbox: who sends what to whom, and what each end checks. It builds on @compmd/render's frame protocol (render README, "In a frame") and replaces the playground's own messages (editor/src/protocol.ts).

Proposed decisions

  1. The protocol is a tree. Each frame has one port, to its parent, the window that made it, and talks to nothing else. Everything it gets (its level, policy, code, part, host, pings) comes from its parent; everything it sends (set, links, errors, its size) goes to it. A parent passes on what concerns the windows above it.
  2. A frame believes its parent only, by its window (event.source === window.parent), never by its origin: the host's page may have an opaque origin itself. Parents are never less trusted than their children (sandbox, decision 1), and the frame documents' policies make sure of it, so a message from a parent is one the frame may act on.
  3. The protocol is a layer of its own, with no DOM. The message types, with their version, and each end's rules as plain state machines over @compmd/render's Port: a frame takes code only after level; a parent gives host only to children it was told to; set is checked against the use's node. It is tested in Node, apart from the browser, and its messages are published as a JSON Schema (npm-packages/sandbox/protocol.schema.json), so a frame runtime or host runtime written elsewhere, a host's own or a sandbox outside a browser, can check itself against it.
  4. Two runtimes implement it, the frame runtime (one end in each frame) and the host runtime (the other end, in the host's page and in every frame that has children). The browser's part, making frames, adding the policy, the ping timer, is theirs, not the protocol's.
  5. A frame's port is for one load (2026-11-24, sandbox, decision 10). A parent posts hello only after a load it started. A load it didn't start, the frame navigating itself or its element moved, gets nothing. For a U, the parent gives the frame its src again and says hello after that load, and three such loads within 10 s fail the frame. P and C are greeted once, and a second load fails them at once. A frame whose ready names another version is removed too, and reported with the reason version.
  6. Compile requests carry ids (2026-11-24, revised with CMD-273). compile carries an id, H's own count, which compiled and failed repeat. An answer settles only the request with its id, and one with no request waiting is dropped, so two components of one name, a document's and an example's (spec §7.8), each get their own code, cached under their own source. A compile not answered within 30 s of H's awake time fails, and its component's uses show their fallbacks; C isn't pinged, and this stands for it. image stays matched by its URL: the same URL wants the same file, and only H answers it. highlight already carries an id, with its timeout.
  7. What a frame sends is checked before it is passed on (2026-11-24): set only for an own key of the inputs that a use the parent sent the frame binds, the page frame included; link only when safeUrl takes it, against the host's schemes, and never as a reader's click on its own; code for a permissive frame only its own pack's permissive code and strict code.
  8. @compmd/render's protocol stays as it is, for a page's tree: render, splices, theme, ready, set, error, rendered, from its version 3, where the page came from: reveal, rects, point, pointed; and from its version 4, where each line is and when the page has drawn: lines, layout and drawn. The sandbox's messages travel on the same port, with their own version, as the playground's do today.

The windows

Window Made by Runs May make
H, the host's page the application; may itself be a sandboxed frame the host runtime; the renderer and trusted components in host mode P, U at any level, C
P, the page frame (frame mode) H, one per document the frame runtime, the renderer, the document's strict components nothing
U, a use frame, strict.html or permissive.html H; a permissive U, for a strict use in its slots the frame runtime, the renderer for one use and its slots, components of its level a permissive U: strict U
C, the compile frame H, one per document the compilers, no components nothing

In our example, in frame mode: H makes P and C. In host mode: H renders the page, makes C, and makes U frames for bar-chart (with sparkline inside it), mermaid, map and rating-summary; a strict legend in map's slot would be a U that map's frame makes.

The port

When a frame loads, for a load its parent started (decision 5), its parent posts hello to its window with one end of a new MessageChannel. The frame takes the first window message whose source is its parent, and listens on that port only; it ignores every other window message. The parent knows the frame by the window it made (iframe.contentWindow).

Messages

A parent to its frame:

Message Says To
hello {version} (a window message, with the port) this is your port every frame
level {level, policy?, schemes?, packs?, timeout?} your level; for permissive.html, the second policy, with the pack's origins, which the frame adds before anything else; the host's own URL schemes; where the host serves the packs' files, ending in /, which a link or an image from a pack's definition is under by its place (spec §12.3); and how long to wait for highlighted, in ms (5,000 without it) every frame, first
code {components} each component's code: compiled text with its binding (react, svelte, plain) and CSS, a built module's file as a Blob with the plain binding and its place (pack@version/path), or a module's {url, integrity}, with a binding (react, svelte) for a pack's source component compiled ahead, whose module exports a Svelte component's CSS as css; text and Blobs with the library packs they import, in libraries, by name, each with its place, its file as a Blob and its own libraries (sharing library packs). Each component has its level, its pack, its example number when it is an example's, and print: "fallback" when its uses print their fallback. A permissive frame gets its own pack's permissive code and the strict code, which it passes on to its strict children; another pack's permissive component comes without code, and its uses are not-here P, U
render, splices @compmd/render's P
scheme {scheme} the colour scheme of the page around the frame, light or dark, which the frame sets on its document and passes to its own frames; again when it changes P, U
part {node, inputs} the use to render, with its slots as expanded nodes, and the document's inputs; a code block a code renderer renders is its own node, with its alt and code, which are its fallback (spec §7.7) U
theme {tokens} @compmd/render's P, U
type-style {style, fonts?} the use's box's type in the page (spec §7.4), which the frame's root takes, with the files of the document's own fonts its family names (spec §9.4) U
lang {lang} the use's box's language in the page (spec §7.4), a BCP 47 tag, which the frame's root takes, so the component's lang is it U
reveal {offset}, rects {id, offset}, point {on} @compmd/render's: scroll to and mark what the text at offset drew, give its boxes, report the source of clicks or stop P
lines {id, from, count} @compmd/render's: give up to count lines' places in the page, from line from P
styles {urls} the host's stylesheets P, U
host {port} a port for host, as calls and events U frames of permissive packs given host, from H only
ping {n} answer, or be stopped P, U; C isn't pinged, since each compile times out instead (decision 6)
compile {id, name, language, runtime, code} compile this; id is H's own count (decision 6) C
observe {on} record what passes in you and report it, asking your own frames too; or stop (observing) P, U
fetched {url, image?, refused?} the answer to image for the same url: the image's file as a Blob, or none when H can't or won't give it, or refused with its origin when the reader hasn't allowed that origin (spec §15) P, from H
images-allowed {origin} the reader allowed origin: the frame's images refused for it ask again P, from H
highlighted {id, lines?} the answer to highlight with the same id: each line's tokens, or none for a language the host's highlighter doesn't cover, a block the parent didn't send, or a failure (the host's code highlighter) P, U
box {width} the width of the use's box's inside, when the frame loads and when the box changes size: the frame measures its component's natural size again and reports it (size) U

A frame to its parent:

Message Says From
ready {sandbox} the frame listens, with the sandbox protocol's version; a page frame's renderer then sends @compmd/render's own ready too every frame
loaded the code last sent is loaded, or shows fallbacks P, U
set {input, value} a component set an input, in @compmd/render's shape; the parent checks that the input is an own key of the document's inputs and that one of the uses it sent the frame binds a param to it, P's page included (decision 7), and passes it up P, U
error {reason, component, use?, message} @compmd/render's, plus failed (code that didn't compile or load) and not-here (a use whose level this frame can't hold, or another permissive pack's, Q2 of the design); use is the use's id in the frame, which the parent puts the frame's own key in front of as it passes it up. A parent reports a frame whose ready names another version itself, naming that version, as it reports one that navigated itself P, U
recovered {component, use} a use that failed draws again, by the use its error named: H takes the failure off what it listed P, U
uncaught {message} the use's component threw after its mount or update returned, nothing catching it: an error or unhandledrejection event of the frame's window, each message once until the next part (spec §7.4, A component that throws later). The parent names the component from its own records, never the frame's code, cuts the message to 2,000 characters, records it as threw in its observer and reports W118, the drawing kept. A browser's notice with no error (Chrome's ResizeObserver loop) is none; WebKit gives a blob: module's error as "Script error.", which is sent as "an error the browser shows only in its console". P holds many components and can't tell whose an error is: it sends none U
link {url} the frame says a link was clicked. Its URL and its timing come from the frame's code, so the parent passes it on only when safeUrl takes it, against the host's schemes, and H shows the reader the URL before anything opens (spec §15 rule 4) P, U
rendered {ms} @compmd/render's, from P; from U, its use's mount has run with its code loaded, its size sent just before: the parent shows the use's fallback in its box until then, and the drawing in its place after (spec §7.4, A component not drawn yet) P, U
rects {id, rects}, pointed {source} @compmd/render's: the boxes asked for, in P's viewport; the source of what the reader clicked, only after point {on: true} P
lines {id, lines, done}, layout {}, drawn {seen} @compmd/render's: a batch of lines' places, in P's page, and whether it was the last; what P shows moved, at most once an animation frame; P's uses have drawn P
size {width, height} a use frame's component's natural size, which the parent gives the use's box (sizing a component in its box): its height at the width box gave, and its width laid out with none U
settled {seen} the frame has settled after the first seen messages that change what it shows (code, render, splices, part, scheme, theme, styles): its uses drawn, their busy promises settled, its own frames settled and sized, its images, stylesheets and fonts loaded, then a frame with nothing moving (settled) P, U
pong {n} the answer to ping: a whole number above the last answered and at most the last sent P, U
compiled {id, name, code, css?}, failed {id, name, message} the result of the compile with the same id; only it is cached C
observed {events} what the frame recorded since its last report, its own frames' reports in it; only after observe {on} P, U
image {url} the page needs an image by its src, which it can't fetch: H fetches it, only if it is an image of the trees and splices H sent P, and answers with fetched P
allow-origin {origin} the reader asks, by the button beside a refused image, to see the images of origin: H asks them with its own question, only about an origin of images it sent P (spec §15) P
highlight {id, code, language} a code block's tokens, by its text and fence language, id the frame's own count: the frame loads no highlighter. The parent answers with highlighted only for a block of the trees, splices or part it sent the frame; a permissive U asks H for its strict children's, as its own. H highlights one block a task, for all its frames together. Without an answer within timeout, the block keeps its text P, U

A parent passes its children's set, link and error up, each with what it knows of where they came from, so H gets them all.

Observing

For developer tools (the Developer Tools, decisions 5 and 6, and its threat model). Only H starts it, when its host observes a view; each window then records its own part, and a frame reports it to its parent:

  • What a window records: the frames it makes (frame, with the level, the network and what it renders; gone, with why), a copy of each message on their ports (message, down or up), the components it mounts (mounted, updated, unmounted, each with its time) and their fallbacks (fallback), each set it refused (refused, of an input none of the frame's uses binds), and dropped when it held too many. Each has its time, at, in milliseconds since the epoch. Pings, pongs, observe and observed aren't copied: one a second from every frame would push the rest out. Values longer than a limit are kept by their size ({ $text: length, start }, { $blob: size, mime }, { $more: count }).
  • Ids are paths: in what a window records, "" is the window itself and "2" its second frame. A parent puts its child's id in front of each id the child reports ("" becomes "2", "1" becomes "2/1"), so a frame can only report on itself and the frames under it.
  • A frame starts when it gets observe {on: true}: it reports its live frames and components first, asks its frames, and sends observed every 100 ms with at most 2,000 events, counting what it drops. observe {on: false} stops it and its frames, and drops what waits.
  • A parent takes observed only while it asked, at most 2,000 events of one, checks each event against the schema's observedEvent, rebuilt from its own fields, drops what isn't one, and nests the rest under the frame's id. It acts on none of it: levels, set, host and which frames to stop come from the checks above, never from a report. H keeps the last 2,000 events of each source, itself and each of its frames, and marks what a frame reported as that frame's.

Settled

For a host that waits until its page stops moving (sandbox README, When the page has settled). Each window settles bottom-up, and a parent counts what it asked for:

  • A parent counts each message it sends a frame that changes what the frame shows: code, render, splices, part, scheme, theme, styles. The frame counts what it takes, in the same order, since the port keeps it.
  • A frame sends settled {seen} each time it settles, with its count: its code loaded, its uses drawn (@compmd/render's drawn), its own frames settled for everything it sent them and their sizes applied, its images and stylesheets loaded or failed, its fonts loaded, then an animation frame with nothing moving. It sends its last size before it.
  • A parent takes it only when seen is what it sent, counting @compmd/render's messages it posts on the port too: a settled for an older render is dropped, and the frame isn't settled until the next. A frame it stopped (W118) counts as settled, and so does one that hasn't said it after 10 s.
  • H is settled when its own page is, by the same rules, and every frame it made is. A frame's code can send settled early or never: the page then settles early, or 10 s later, and layout still says when it moves. Nothing H shows depends on it.

Sequences

Opening a document, frame mode. H makes C and P, each a strict.html. Each sends ready; H sends level {strict}, then C each component's source, and P code and render. P renders the page and mounts bar-chart, sparkline and mermaid; stars, rating-summary and map show their fallbacks, and P sends error {not-here} for each.

Opening a document, host mode. H renders the page; stars mounts in it. For bar-chart it makes a strict.html frame in the chart's box and sends hello, level {strict}, code (the bar chart's and the sparkline's) and part (the chart's node, the sparkline in its slot). For map it makes a permissive.html frame and sends level {permissive, policy} with tiles.example.com, then code and part; for rating-summary the same, plus host.

A strict use inside a permissive one. map's frame renders its slot, finds legend, makes a strict.html frame there, and sends it hello, level {strict}, code (from what H sent it) and part. It pings it, and passes its set, link and error up to H.

An edit. H sends P splices (frame mode) or renders them (host mode), and sends part to every frame whose use changed; a frame does the same for its children. Uses keep their frames through edits, as the renderer's keys keep its views, so an edit updates a frame instead of making it again. A use that is gone loses its frame; a new one gets one. When the document's components change, H compiles them in C and sends code to the frames that have them, which pass on their children's.

A frame that hangs. Each parent pings its frames every second; a frame that hasn't answered within timeout is removed, and its use shows its fallback (W118). So is one that hasn't said ready within timeout (5 s at least) of its document loading: its runtime never loaded. Both times are the parent's awake time, counted a tick at a time: a page and its frames may share a thread (WebKit; Chromium's frames in the page's process), and while it is busy, with another frame's large module say, a frame can neither load nor answer, so a busy stretch of any length counts as one tick against it.

Strict code tries to break out.

It tries What happens
to make a frame of permissive.html or strict.html refused: strict.html has frame-src 'none'
a srcdoc frame it inherits the strict policy: no network, whatever it is sent
to send a frame a second policy, code or params the frame takes them only on its port, from its parent
to post set for an input its use isn't bound to its parent checks set against the use's node, and drops it
to get host only H gives host, and only to a permissive.html frame of a pack given host
to have H fetch a URL for it, through image H fetches only the images of the trees and splices it sent P, so a URL code made up, with data in it, is never fetched
to send H a pointed or rects of its own H takes from them only a source and boxes that are numbers; a made-up click can move an editor's caret, nothing more
to navigate its own frame, with data in the URL the host's page's frame-src keeps it to the frames' origin; its parent says nothing to what loads; a U is given its own document again, and failed after three within 10 s, and a P or C is failed (decision 5)
to send a link with a javascript: URL, or one no reader clicked the parent passes on only what safeUrl takes, and H shows the reader the URL before anything opens
for C, to answer a compile it wasn't asked, or answer one with another's result an answer settles only the compile with its id (decision 6)
to send H lines of its own, or layout again and again H takes only whole line numbers and rects that are numbers, and asks for lines at most once an animation frame however often layout comes; made-up places move a gutter's numbers, nothing more

The sequences, drawn

Opening a document in frame mode. The page and its strict components share one frame; the compile frame runs no components.

sequenceDiagram
    participant H as H, the host's page
    participant C as C, the compile frame
    participant P as P, the page frame
    H->>C: hello (window message, with the port)
    C-->>H: ready {sandbox}
    H->>C: level {strict}
    H->>C: compile {chart, jsx, …}
    C-->>H: compiled {chart, code}
    H->>P: hello (window message, with the port)
    P-->>H: ready {sandbox}
    H->>P: level {strict}
    H->>P: code {chart: strict, map: permissive, stars: trusted}
    P-->>H: loaded
    H->>P: render {tree}
    Note over P: mounts chart; map and stars show their fallbacks
    P-->>H: error {map, not-here}, error {stars, not-here}
    P-->>H: rendered {ms}
    P-->>H: set {region, eu}
    Note over H: checks that a use binds region, then processes again
Loading

Host mode, with a strict component in a permissive one's slot. Each frame believes its parent only; the permissive frame makes the strict one and passes on what H gave it.

sequenceDiagram
    participant H as H, the host's page
    participant M as map's frame (permissive)
    participant L as legend's frame (strict)
    Note over H: renders the page; stars mounts inline
    H->>M: hello (with the port)
    M-->>H: ready {sandbox}
    H->>M: level {permissive, connect-src tiles.example.com …}
    Note over M: adds its second policy before any code
    H->>M: host (a port, if the pack is given host)
    H->>M: code {map: permissive, legend: strict}
    H->>M: part {map's node, legend in its slot}
    M->>L: hello (with the port)
    L-->>M: ready {sandbox}
    M->>L: level {strict}
    M->>L: code {legend}, part {legend's node}
    L-->>M: set {layer, roads}
    Note over M: checks that its part binds layer
    M-->>H: set {layer, roads}
Loading

Strict code poses as a parent. A frame takes its hello from the window that made it, and nothing else.

sequenceDiagram
    participant H as H, the host's page
    participant S as a strict frame
    participant B as a frame H made, not yet given its port
    S->>B: hello, through top.frames (window message, with a port of its own)
    Note over B: event.source isn't its parent: ignored
    S->>B: level {permissive, connect-src *}, code {…}
    Note over B: not on a port it took: ignored
    H->>B: hello (with the port)
    Note over B: from its parent: this is its port
Loading

A frame that hangs.

sequenceDiagram
    participant H as the frame's parent
    participant U as a use's frame
    loop every second
        H->>U: ping {n}
        U-->>H: pong {n}
    end
    Note over U: a component loops forever
    H->>U: ping {n+1}
    H->>U: ping {n+2}
    Note over H: no answer within the timeout
    H->>H: removes the frame; the use shows its fallback (W118)
Loading

Checks, by end

  • A frame: it starts only with an opaque origin (self.origin === "null"), so a frame document embedded without sandbox runs nothing; its port comes in a window message from its parent; it adds the level policy before it loads any code; it runs code and renders parts only from its port; it answers ping.
  • A parent: it says hello only after a load it started; it gives a U that loaded anything else its src again, failing it after three within 10 s, and fails a P or C that loads again (decision 5); it makes children only of a lower level than its own (a permissive frame, only strict ones); it sends each child only its own use's part and code, a permissive frame only its own pack's permissive code; it checks each set against the uses it sent, as an own key; H matches each compile's answer to its request by id, and fails a compile after 30 s; it passes on a link only when safeUrl takes it; it pings its children; it takes observed only after it asked, and checks each event.
  • H: it gives host only to permissive frames of packs the configuration gives host; it never acts on link without showing the reader its URL first; it asks P for clicks (point) only once its host listens for pointed, and passes on only a well-formed source and boxes; it fetches for image only the images of the trees and splices it sent P, without credentials or a Referer, and answers only with a file that is an image; it highlights for highlight only a block of the trees, splices and parts it sent the frame (a permissive U checks its own children's the same way).

Versions

The sandbox's version travels in hello and ready, beside @compmd/render's. A host may serve the frame documents from an older deploy than its page: a frame with another version gets nothing, and its parent removes it and reports it with the reason version.

  • Version 2 added highlight and highlighted and the level's timeout: the frames ask the host's page for their code blocks' tokens and load no highlighter (the host's code highlighter). A host's page and the frames it serves upgrade together.
  • Version 6 (2026-12-02, CMD-535) adds uncaught {message}: a use frame says its component threw after it drew, which was W118 nowhere before but the browser's console (spec §7.4).
  • Version 5 (2026-11-30, CMD-488) adds lang {lang}: the parent says the use's box's language when the frame loads and when it changes, as it says its type, so a component writes dates and numbers in the document's language (spec §7.4).
  • Version 4 (2026-11-24, decisions 6 and 7) gives compile, compiled and failed an id, and code's components their pack.
  • Version 3 made a use frame's size its component's natural size, and added box {width}, with which the parent says the width of the box's inside when the frame loads and when the box changes size (sizing a component in its box). The parent says it because a browser pauses a frame's own observers while it is off screen, and may not give it its new size then; a message always arrives.

From the playground's messages

Playground (editor/src/protocol.ts) Sandbox
FRAME_HELLO with the port hello {version}
components {components}, compiled in the frame code {components}, compiled in C
components-loaded loaded
component-failed error {reason: "failed"}
host-style {style} styles {urls}; the playground's stylesheet picker becomes a list of URLs
source-link {fragment}, open-link {url} link {url}; the playground handles #:~:text= links itself

Open

  • @compmd/render's protocol could take the sandbox's messages into its own, or stay as it is with the sandbox's beside it on the same port (recommended, decision 8): a host that builds its own sandbox can still use connectFrame and serveFrame alone.