Skip to content

Prototype: the sandboxed frame and the shell broker on the same host #175

Closed

Description

@allisonmahmood

Question

Does a tier 1 patch rendered in a sandboxed frame on the same host, with the SDK reaching Patchy only through the shell over postMessage, hold up in real browsers on the ergonomics that matter, and is the broker a design that carries to tier 2?

Build, on a throwaway branch: the shell at the patch's path renders the content URL in <iframe sandbox="allow-scripts"> with no allow-same-origin; the content response carries Content-Security-Policy: sandbox allow-scripts so opening it directly is equally sandboxed; a broker in the shell that checks event.source, exposes two operations (read rows from one table, fetch one stored file's bytes) and refuses everything else; the API rejecting Origin: null and missing origins on a mutation; one SDK call from inside the frame that round-trips a typed result.

Then click through: a deep link into a route inside the patch and back/forward; an image from the patch's own files shown via broker-fetched bytes and a blob URL; a download triggered from inside the frame; window.print() from inside the frame versus shell-owned printing; a public patch opened signed-out; a signed-in viewer from another company. Record what works, what the shell fixes cheaply, and what is unacceptable.

Then the second-model review (see the map's Notes): attach the prototype's files and ask astra whether the broker's envelope is generic enough to carry tier 2 request correlation, streaming and cancellation without redesign, and where it would break first.

Recommendation: the decision on the map already picks this design; this ticket is the evidence. If printing or deep links fail in a way the shell cannot fix, the tier 1 serving ticket revisits the decision with that evidence rather than assuming.

Activity

  1. added
    wayfinder:prototype
    on Sep 6, 2026
  2. allisonmahmood commented on Sep 6, 2026

    @allisonmahmood
    OwnerAuthor

    Prototype findings (2026-09-06)

    Built on proto/frame-broker: one plain Node host with a session cookie, three fixture patches (a company patch, a public patch, a probe with a loose CSP), the shell with <iframe sandbox="allow-scripts">, a broker, the SDK, and an API that logs the Origin, cookie and Sec-Fetch-Site it sees. pnpm proto:frame serves it, pnpm proto:frame:click drives the ticket's click-through in Playwright. Throwaway; delete the branch when this closes.

    Verdict: the design holds. Nothing on the ergonomics list is unacceptable. Printing is the one item that needs a decision (below). Astra's review found two real holes in the first cut, both fixed and re-checked, and a bootstrap qualification for the build ticket.

    What real browsers did

    Chromium 151 and Firefox 153, headless. WebKit could not run on this machine (its Playwright build wants ICU 74; Arch ships newer), so Safari is untested and is the hand-check to do.

    Check Chromium Firefox
    Deep link /acme/inventory/items/2 frame renders item 2 same
    In-frame navigation, back, forward address bar follows; back/forward re-render the right route same
    SDK round-trip (tables.read) with a typed result works works
    Mutation via broker API saw the exact shell Origin, cookie present, Sec-Fetch-Site: same-origin same
    Own file as <img> via broker bytes + blob URL blob:null/…, renders same
    Download, shell-owned download fires same
    Download, in-frame anchor blocked silently blocked silently
    window.print() in frame ignored: "sandboxed, allow-modals not set" ignored, silently
    window.print() in frame with allow-modals prints the frame's own document opens a print dialog (blocked the headless run)
    Print via shell frame content is inline, clipped to one page unless the shell first grows the iframe to the content height (1 page → 5 pages on a long patch) not observable headless
    Public patch, signed out renders; tables.read → 401 "anonymous readers get nothing"; no session script in the shell same
    Viewer from another company company patch → 403 door; public patch renders, tables.read → 403 same
    From inside the frame top.document, document.cookie, localStorage: SecurityError; window.origin is null; top navigation refused; fetch blocked by CSP same
    Frame fetching the API directly (loose CSP) arrives with Origin: null, no cookie, Sec-Fetch-Site: cross-site; the browser then rejects the response for lack of CORS same
    Content URL opened directly at top level opaque origin, document.cookie throws, fetch blocked same
    API guard from outside POST with no Origin, Origin: null, or another origin → 403; exact → 200 —
    Broker: window messages from the shell itself or a sibling frame dropped dropped
    Table name as a path (../../deck/tables/slides), inherited op name (toString) refused refused
    Frame navigates itself to another document broker revoked; the replacement document never gets an answer same

    What the shell fixes cheaply

    • Deep links and history. A route bridge: the shell hands the frame its route on bootstrap and on popstate; the frame asks the shell to pushState. Works in both browsers with about twenty lines.
    • Files. Broker-fetched bytes to a frame-local blob URL, as assumed. Works.
    • Downloads. Shell-owned: the anchor is created on the trusted origin. Works in both browsers. In-frame downloads stay blocked without allow-downloads.
    • The content CSP. With connect-src 'none' on the content response, patch code cannot fetch anything: "every data call goes through Patchy" becomes enforced, not promised. Even without it, a fetch from the opaque frame reaches our API with Origin: null and no cookie, so the API guard's null-origin rule is doing real work.

    What astra's review changed

    Round one (gist; the full exchange is in scratch):

    • Two holes, both real, both fixed. The broker built URLs from the table name, so ../../deck/tables/slides would have normalised into another patch's URL before the server looked; and ops[m.op] admitted inherited names like toString. Now identifiers are validated in the shell and dispatch is own-property only. The server enforces declarations per call regardless; the shell check is defence in depth.
    • A navigated frame inherited the broker. A document that replaces the first one keeps the sandbox flags, so event.source === frame.contentWindow still matched and in-flight replies posted with "*" would land on it. Fixed by moving the broker onto a MessageChannel: the shell hands a port to the first document once, all requests and replies ride the port, a second load closes it and revokes. Checked: a self-navigated frame's replacement document never gets an answer. This also removes the wildcard targetOrigin from everything but the one bootstrap message.
    • Envelope. Fields carry to tier 2; the lifecycle needs pinning now: unary result | error; streams chunk* then exactly one end | error; cancel targets an id and is best effort, never a rollback; bounded outstanding chunks. Written as contract in the README, not built.
    • Per-op broker versus one generic call. Keep browser-owned ops (route, download, print) in the shell. For tier 2 server capabilities, one structured invocation { capability, method, args } that the server resolves against the shell-bound patch, its declarations and the viewer; those strings name authority, they never confer it. Never a URL, header or credential from the frame.

    Round two, on the revision: "the architecture carries to tier 2; the implementation still has a tier-1 bootstrap hole." The shell issues the port to whichever document produces the first load, so a redirect or self-navigation before the first load would receive it. The build must bind the bootstrap to the intended content document (a nonce the shell puts in the content URL and the document echoes in its ready), make bootstrap failure bounded and visible instead of an SDK that queues forever, and coordinate readiness rather than rely on catching the iframe load from a script that loads after it. The first tier 2 thing that breaks is the long-running request: closing the port does not abort a fetch or undo a committed mutation, so cancellation needs server behaviour, not only a message kind.

    Decisions this leaves for you

    1. Printing. Options: (a) grant allow-modals on the sandbox and in the content CSP so patch code prints its own document; (b) shell-owned print that grows the iframe to the content's reported height first; (c) the shell opens the content URL in a print-only top-level page.
    Product context: a 2,000-row inventory printed from a patch is an ordinary ask for an internal tool.
    Consequences: (a) prints the frame's document with native pagination and needs no guessed pixel height, but also unlocks alert/confirm/prompt, so a buggy patch can nag; (b) keeps the sandbox tightest but couples pagination to a height the shell guesses and printed position: fixed chrome gets odd; (c) loses the broker, so the page would need its own data bootstrap. Astra picks (a). Recommendation: (a). The nuisance is bounded to the frame and is the same tradeoff every tier 1 patch already accepts by running script.

    2. connect-src 'none' on tier 1 content. Product context: it makes "every data call goes through Patchy" a property the browser enforces, and it also closes the obvious exfiltration channel. Consequences: web fonts, hot-linked images and map tiles do not load in a patch unless brokered or platform-hosted; opening img-src https: or font-src would reopen an outbound channel, so any later egress should be an explicit per-patch permission, not a maps exception. Recommendation: yes, keep it closed for tier 1, with img-src blob: data: and style-src 'unsafe-inline'; fonts and images ride the files primitive.

    3. Two hand-checks I could not run here. Safari on the shell URL above (deep link, the image, a download, print), and headed Firefox for the print dialog and the in-frame download. Both are five minutes on a laptop; I will add the results to this ticket if you paste them.

    For the tier 1 serving build ticket

    • The content CSP sandbox intersects with the iframe attribute; any token the shell grants must be listed in both or it is silently absent.
    • Nonce-bound bootstrap; bounded bootstrap failure; idempotent revocation; identifiers validated at the shell and declarations enforced at the server.
    • Acceptance checks astra insists on: navigation during pending reads and writes; malformed, oversized and duplicate-id messages; real cookies through login, logout and expiry; simple cross-site POSTs, not only preflighted JSON; CSP and admission on redirects, error pages and uploaded active content; a 2,000-row print with the first and last row checked.

    Next: answer 1 and 2 (or just "go with the recommendations"), paste the Safari result if you have it, and I will close this with the resolution and add the decisions to the map.

  3. allisonmahmood commented on Sep 6, 2026

    @allisonmahmood
    OwnerAuthor

    Prototype PR, not for merge, so the team can run it: #185

  4. allisonmahmood commented on Sep 6, 2026

    @allisonmahmood
    OwnerAuthor

    Resolved 2026-09-06 with Allison. The design holds: a tier 1 patch in <iframe sandbox="allow-scripts allow-modals"> on the same host, content served with the same sandbox in its CSP, the SDK reaching Patchy only through the shell's broker on a document-bound MessageChannel. Chromium and Firefox pass the whole ergonomics list (findings above); Safari is Allison's hand-check and gets appended here.

    Two decisions taken on the evidence:

    1. Printing is the frame's own. The sandbox grants allow-modals, in the iframe attribute and the content CSP alike, so window.print() prints the patch's document with native pagination. Cost accepted: the patch can also open alert/confirm/prompt, a visible nuisance bounded to the frame. Shell-owned printing was measured (one clipped page unless the shell grows the frame to a guessed height) and rejected; a print-only top-level page loses the broker and was rejected.
    2. connect-src 'none' on tier 1 content, closed by default. Patch code cannot reach the network; every data call goes through the broker and is logged. Fonts, images and libraries ride the files primitive or are bundled at publish. Any later egress (maps, embeds) is a declared, visible per-patch permission built when a real patch needs it, never a default and never a maps exception.

    Carried into the build (Tier 1 serving): the envelope and its lifecycle (result | error; chunk* then end | error; best-effort cancel), browser-owned ops in the shell and one structured { capability, method, args } for server capabilities at tier 2, a nonce-bound bootstrap with a bounded failure, idempotent revocation, identifiers validated at the shell and declarations enforced at the server, and astra's acceptance list. The prototype lives on #185, not for merge, for the team to try; delete the branch when the build ticket is filed.

  5. allisonmahmood commented on Sep 8, 2026

    @allisonmahmood
    OwnerAuthor

    Hand-check 2026-09-08 (Allison). Ran the click-through by hand in the browsers on the laptop; everything on the list held. Safari remains untested: nobody on the team has a Mac right now. When we have access to Safari, run the shell URL from pnpm proto:frame through the four items (deep link plus back/forward, the broker-fetched image, a download, window.print() from inside the frame) and append the result here or on Tier 1 serving. The design is taken as holding until then; it is not blocked on it.

    PR #185 closes as not for merge; the branch stays until the build tickets have lifted what they need from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

wayfinder:prototype

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions