ADR-0051: Mini apps: the sandboxed frame and its bridge¶
Status: Accepted Date: 2026-10-18 Deciders: Alex Nodeland Amends: ADR-0040's ceiling for the app's total
Context¶
lattice RFC-0005's second phase runs mini apps: one HTML file an agent wrote, run for everyone who opens it, with no person reviewing it first (its Decision O6). The sandbox and its policy carry the whole weight, so each layer assumes the one before it failed. artifactr.ui offers the agent create_app and update_app while ui:mini-apps is on, refuses HTML that names anything to load, and lets a person change an app's state and nothing else (artifactr ADR-0082). Its tools' description is the bridge's contract, on MCP Apps' method names, as the RFC's U4 has it: portalr's own host, not @modelcontextprotocol/ext-apps or @mcp-ui/client.
The RFC left the browser's behaviour unverified. A spike in Chromium, with the portal, the apps' origin and a sink each a real origin on 127.0.0.1, settled it:
- An inner
srcdocframe inherits the sandbox's policy. With the sandbox's script allowed by its hash, as the RFC wrote, the app's own inline scripts were refused too: nothing ran. frame-src 'none'doesn't stop asrcdocframe, which isn't fetched, and does stop the app's frame navigating itself to another address, the likeliest way out:location.href, a link it clicks, and<meta http-equiv="refresh">each ended on Chromium's error page, and the sink heard nothing.- WebRTC is not a policy's to refuse in Chromium. It doesn't know CSP's
webrtcdirective, and a peer connection the app made sent STUN to the sink over UDP. Removing everyRTC*constructor before the app's script runs stopped it in the app's own window, and anabout:blankframe the app makes is of another opaque origin, so the app can't take them back from one. But asrcdocframe the app makes runs a script of its own, in a window of its own that still has them, under the inherited policy, and its peer connection reached the sink. No policy, sandbox flag or permission refuses that in Chromium, and a script that watches for frames can't be shown to run first in every case (a declarative shadow root's frame, for one). - Everything else failed as designed:
fetch, XHR, WebSocket, EventSource,sendBeacon, an image, a CSS beacon, prefetch, preconnect and DNS prefetch,window.open, a form's submit, the top's or the parent's navigation, service workers,document.cookie,localStorageand the parent's DOM; andWebAssembly.compile, with no'wasm-unsafe-eval', was refused.
Decision¶
- Two frames, on an origin of their own. The portal frames the sandbox on the apps' origin,
apps.<host>, withsandbox="allow-scripts allow-same-origin", noallow, and no referrer; the sandbox runs the app in a frame of its own withsandbox="allow-scripts"alone, an opaque origin, with no forms, popups, top navigation or downloads. Site(apps=...)names the apps' origin: https, or http on loopback, a host with no path, user, query or fragment, kept as its origin.config.jsoncarries it asapps, null where the deployment runs none. The UIs' policy gainsframe-srcnaming it alone, and keepsframe-ancestors 'none'.SandboxOrigin(app, apps=, portal=), an ASGI middleware, answers every request whoseHostis the apps' origin's, whatever its scheme, since an edge that ends TLS hands https on as http:GET /is the sandbox page, anything else 404 and a write 405, a WebSocket closed, and nothing from the application reaches it. It refuses an apps' origin that is the portal's. Every answer carries the sandbox's policy,nosniff, no referrer,X-DNS-Prefetch-Control: off, aPermissions-Policythat allows no powerful feature, andno-cache; nothing sets a cookie.- One policy, the app's, for both documents:
default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; img-src data:; font-src data:; base-uri 'none'; form-action 'none', and for the sandboxframe-ancestorsthe portal's origin.default-src 'none'refuses every fetch, socket, worker and frame, and so the app's frame navigating; there is no'wasm-unsafe-eval'or'unsafe-eval', so no WebAssembly and noeval, though portalr's own policy allows first-party WebAssembly (ADR-0041). The sandbox's script is inline, not allowed by its hash, since the app inherits the policy and a hash would refuse it; the sandbox puts no untrusted markup in its own page, so that costs nothing.web/ui/csp/app-policy.txtholds it, and portalr's and ui's tests hold the server's and the sandbox's copies to it. - The app's document begins with the host's: its own copy of the policy,
x-dns-prefetch-controloff, and a script that removes everyRTC*,webkitRTC*andmozRTC*constructor from the app's window, measures the app's height for it (ADR-0052), and gives itwindow.host, the bridge's shapes as functions (host.connect,host.call,host.request,host.notify,host.onAnswer,host.onContext); then the app's HTML. All three are the app document's own inline script, under its policy: nothing is loaded. - What the sandbox doesn't hold is said: an app can send to an address of its choosing over UDP, by WebRTC's ICE from a
srcdocframe it makes. It reads nothing of the person's (no cookie, session or other origin's data) and loads nothing, so what it could send is what it was given: its own HTML and state, and what the person types into it. The label says it runs sandboxed, apart from the person's session, not that it can't reach the network. - The sandbox page is built by app's build, from
ui/src/sandbox-page.ts, bundled and minified by Vite into its one inline script (@portalr/ui/sandbox), and written tostatic/sandbox/index.htmlbeside the UIs; the wheel check fails without it. It runs only when framed with?host=naming the portal's origin, takes the app's HTML from its parent alone, at that origin, and relays: the app's messages to the portal as JSON text, dropped past 64 KB or where JSON can't hold them, and the portal's to the app. - The bridge is portalr's, in
ui'sapp-bridge.ts, on MCP Apps' methods (2026-01-26): it takes a message only from the sandbox's window and the apps' origin, as JSON text within 64 KB, a JSON-RPC 2.0 request or a notification it knows, and 60 a minute a frame; past them it stops answering, and the chrome says so. It answersui/initializewith the host's context,tools/callforget_state,set_stateandsubmitand no other tool, andui/open-link; any other request is "method not found". It hearsui/notifications/size-changed, sizing the frame up to 2,000 pixels, a height below 120 being none, and the sandbox's own measure of the app's height, paced apart and not counted against the app (ADR-0052), and sendsui/notifications/host-context-changedwhen the scheme, colour-blind mode, forced colours or reduced motion change. - The host gives the app on every ready and every load, and says when an app doesn't start. The sandbox says
ui/notifications/sandbox-proxy-readyonce, as its page loads, and a ready the host didn't hear left the frame blank with no error. The bridge listens from a layout effect, in the task that put the frame into the page, and sendsui/notifications/sandbox-resource-readyon each ready and on each of the frame'sloadevents, when a sandbox's inline script already listens. The sandbox runs the first app it is given and drops any later one, rather than pass it to the app, so a post the browser dropped, a reload or a restarted process never leaves it without one. The app's document saysportalr/app-startedbefore the app's HTML. If nothing from it is heard within ten seconds of the frame going in, the sandbox answering, or the frame loading, the card says "This app didn't start. Nothing it held is lost. Try again, or ask the agent to rebuild it." Its details say whether the sandbox answered, and Try again makes the frame afresh. Added after an app's frame stayed blank on an iPhone over a tailnet while the same page ran in desktop Chromium and WebKit. - The host's context is MCP Apps'
hostContext:theme, andstyles.variables, portalr's colours resolved to what the browser draws, under portalr's names (--paper,--fg,--accent,--chart-1and the rest) and MCP Apps' (--color-background-primaryand the like), withlocale,timeZone,platformanddisplayMode, and portalr'scolourBlind,forcedColoursandreducedMotion. - State is coalesced, as the RFC words it: a patch applies at once to what the app sees, and is written as the person's edit of
/state, replaced whole on the version it was read at, at most once every five seconds. A write on a head that moved on waits for the new head, and writes the patches on it again; one refused for another reason puts the state back as the head has it, and the chrome says why: off in the workspace, or the person may not change it. - A submission and a link are the person's. Each is asked in the host's own dialog, outside the frame: a submission shows its values as JSON text, and is sent only once they agree, as a
submissionwithapp_id, the version, a nonce and its derived id; a link shows its full address, opens only if it is http, https ormailto:, and in a new tab with no opener or referrer. One question is open at a time; another is declined. - The chrome is the host's: the app's title as a heading; "Made by the agent. Runs sandboxed, apart from your session." with its version; its summary, its text equivalent; a border round the frame, which the app can't draw over; the frame titled "
: an app made by the agent"; and a link to skip past it, which the keyboard reaches first, to a point after it. - Nothing runs where the app is archived ("This app is switched off: it was archived."), where mini apps are off in the workspace (the kit's "Not available in this workspace: mini apps are off."), or where the deployment names no apps' origin, or names the portal's own. The label and the summary are shown instead.
- The kill switch stops a running app. The app gives a page's apps an
AppHost,useAppHost(config), and, while an app's page is open, reads the workspace's integrations again every half minute, so mini apps switched off, by the deployment, the tenant or the workspace, stop it at the next read; a write refused asnot_enabledreads them at once, as every command's does (ADR-0045); archiving stops it as the artifact's event arrives. - Running an app is an add-on,
apps(ui/src/on-demand/apps.tsx, ADR-0044): the frame, the bridge and the dialog load with the first app a page runs, so no page that runs none holds a bridge. Its ceiling is 9 kB, total and largest; it measured 4.9 kB. The chrome is in the app's own bundle, which it grew by 4.1 kB, mostly in the small chunks the add-on now shares with the routes; with whatmainhas gained since, the app's total is 473.5 kB, past the 470 kB ceiling. As ADR-0040 sets ceilings about ten percent above the sizes at the time, the total's is 520 kB, amending it; the initial and largest are unchanged.
Options considered¶
| Choice | Option | For | Against |
|---|---|---|---|
| The sandbox's script | Inline, under the app's policy (chosen) | One policy for both documents; the app's scripts run | 'unsafe-inline' on a page with no untrusted markup |
| By its hash, as the RFC wrote | The narrowest policy for the sandbox | The app inherits it, and none of its scripts run | |
| A third document, fetched with a policy of its own | Each policy its own | Another page, a loader that writes the app into itself, and a frame-src to allow it |
|
| WebRTC | Its constructors removed first (chosen) | Closes the app's own window in Chromium today | A script of the host's in the app's document; a srcdoc frame the app makes keeps WebRTC |
CSP's webrtc 'block' |
The policy alone | Chromium ignores it | |
| Also watching for frames the app makes, to take WebRTC from each | Closes the common case | Can't be shown to run before a frame's script in every case; a claim it would hold invites the label back | |
| Serving the sandbox | A middleware on the application, by Host (chosen) |
The same release's build, one process, and a second port locally | The application answers the apps' origin |
| The edge serving a file | No application in the path | The edge needs the build, and a policy of its own to keep | |
| Running an app | An add-on (chosen) | No bridge where no app runs; the app's total grows 4 kB | Small shared chunks |
| In the app's bundle | No add-on | The total grows 5.4 kB, and every page holds the bridge |
Consequences¶
- Easier: an agent's app runs where an operator names an apps' origin and a workspace switches mini apps on, and nowhere else.
- Easier: what the policy refuses is held in Chromium by
ui's CSP tests (csp/sandbox.test.ts): the sandbox page as built, on an origin of its own, framed throughAppFrameand the bridge, runs the app's scripts, and a sink on 127.0.0.1 hears none of fetch, XHR, WebSocket, EventSource, a beacon, an image, CSS and a font, a link's ping, prefetch, preconnect, DNS prefetch and preload, a worker, a popup, a form, the top's navigation, the frame's own and a refresh, a frame of the sink's, adata:,javascript:orsrcdocframe's request, or WebRTC in the app's window. - Harder: WebRTC from a
srcdocframe the app makes reaches the network, and a test holds that it does, so a Chromium that refuses it is noticed. Until a browser refuses it, an app can send what it was given to an address of its choosing. This is an accepted risk (recorded 2026-10-22, after a security review): WebRTC from asrcdocframe the app makes inside its own is not contained, and is weighed against what the app holds, which is only its own HTML and state and what the person types into it, never the person's session or another origin's data. An app that must not reach the network can't be run as a mini app until a browser offers a control for it. - Harder: an app's frame can hold the keyboard, as any page can; the link before it is the way past it, and the label and summary stay its text equivalent.
- Harder: an app whose state two people change at once keeps each person's patches, written again on the other's, never merged.
- Harder: a running app is stopped within half a minute of being switched off, not at once, unless it writes.
Amended (2026-10-27) by ADR-0084: a portal reached at several origins has an apps' origin beside each,
Site.aliases;SandboxOrigin(app, apps=, portal=)andsandbox_policytake one origin or several, answer every apps' origin alike and are framed by every portal origin, and the UIs'frame-srcis the union;config.jsoncarriesoriginandaliases, and at an address the deployment names no place for, the app says where to open the portal. A string is served as before.Amended (2026-10-27): what went wrong inside an app is said. An app's frame stayed blank on an iPhone over a tailnet after the host heard
portalr/app-started, so the app's document ran, and nothing said why. The app's document now begins, afterportalr/app-started, with listeners forerror(a resource that didn't load among them),unhandledrejectionand the document'ssecuritypolicyviolation, which sayportalr/app-error(kind, one oferror,load,rejectionandpolicy;messageup to 300 characters,sourceup to 120,line,directive,blockedup to 200) for the first five that differ by their message or what was refused, one kept for a thrown error. WebKit hides an error's text from the app's own inline scripts, which run in an opaque origin, as "Script error.": there it says that an error was thrown, not which, while a refused promise's reason, a resource that didn't load and a refusal of the policy are said in full in both engines. It also saysportalr/app-boot, what the app drew, 1.5 s afterDOMContentLoaded, again at 5 s if it had drawn nothing yet, and once more when it first draws: its visible content, its largest visible canvas's size, the context the app asked of it, and, for a 2D one alone, whether nine pixels on a 3 by 3 grid across it are transparent; the window's size and pixel ratio, the user agent up to 120 characters, and which ofAudioContext,webkitAudioContext,ResizeObserverandrequestAnimationFrameit has; anddrawn, "nothing" only where both looks found no visible text or element besides a canvas, and no visible canvas, one the app never asked a context of, or a 2D one transparent at all nine.getContextandtransferControlToOffscreenare wrapped to remember which context each canvas was given, an offscreen one, drawn in a worker, counting as drawn; the look never asks a canvas for one, so an app that asks for WebGL later is given it, and a WebGL canvas's pixels are never read. The sandbox relays them as any message. The bridge reads them as notifications it knows, counted against the frame's 60 a minute, keeps only those fields, text cut short by characters, never half of one, with control, format and separator characters (bidi marks and tags among them) made spaces, numbers finite and within 0 to 100,000, and passes eight at most a frame to theappsadd-on. The add-on writes each to the console as one line,[app-frame] …, where Web Inspector shows it; and, once the first look arrives, keeps a quiet, collapsed disclosure under the frame, "What the app reported", holding the latest look and any errors as plain text, its summary "The app reported a problem" where there are errors, so a person on a phone sees what the app said without a console; an error WebKit hides is said as "the app threw an error; this browser doesn't say which"; and, while the app'sdrawnis "nothing", says "The app hasn't drawn anything yet. Try again, or ask the agent to rebuild it." with Try again, until it draws. None of it is sent anywhere, counted, or kept (lattice RFC-0008): it is the app's own text, untrusted, shown as text alone. It grows theappsadd-on from 6.0 kB to 6.8 kB, and the app's initial bundle by one byte, of its chunks' hashed names (203,432 of 203,500).Amended (2026-10-28): the app's frame never depends on percentage resolution. On that iPhone (iOS 18.7), the look said the app drew on its canvas, but its window was 0 by 0. The app's frame was sized
width:100%;height:100%in the sandbox page's body atheight:100%, in the portal's frame, whose width was a percentage too. A width of 0 contradicts a cause in the percentage heights alone, since a block's width doesn't hang on them: the sandbox's window, or how the frames were laid out, was at nothing, and which is still inferred, not seen; Chromium and Playwright's WebKit lay it out whole. So no frame in the chain is sized by percentages now. The portal's frame is given its container's width in pixels, measured by aResizeObserver, beside its height, and says its size to the app's context as MCP Apps'viewport. The sandbox places the app's frame against its window,position:absolute;inset:0, and sizes it in pixels, each side the window's (documentElement.clientWidthandclientHeight, orinnerWidthandinnerHeight), or where that measures 0 the host'sviewport, read from the context it relays, or the last size measured, or 320 by 360; as it is made, onresize, on itsload, whenever aResizeObserversees the sandbox's root change, and whenever the host says its size. The app's measure of its own height is unchanged. The look now carries what the sandbox saw, set over anything the app says by those names:sandboxWidthandsandboxHeight, its window, andframeWidthandframeHeight, the app's frame as laid out; and the add-on ends its line withouter=, the portal's frame, andcontainer=, its container's width. A blank reading then says which was at nothing. A test leaves the sandbox's body at its own height, where a frame at100%of it was 150 tall, and finds the app's frame the size of the sandbox's window; it fails on the frame as it was, and passes in Chromium and in Playwright's WebKit at a phone's viewport. Theappsadd-on is 7.0 kB (6,982 of 9,000); the app's initial bundle is 203,330 of 203,500.Amended (2026-10-28): an app that stopped after an error says so, with the error. A game an agent wrote froze on an iPhone a few seconds into play; the report line said only "the app threw an error; this browser doesn't say which", and the person couldn't tell what to do. The app's document now wraps the callbacks the app gives
requestAnimationFrame,setTimeout,setIntervalandaddEventListener(a function listener; ahandleEventobject and a string timer are passed as they are). A wrapper calls the app's callback with its ownthis, arguments and return value; a listener is wrapped once, kept in aWeakMap, soremoveEventListenerwith the app's function removes it. An error the callback throws is said asportalr/app-error, itsmessagethe error's ownname: message, and a new field,stack, its first four lines that aren't the message, joined by " / " and cut to 400 characters (the bridge cuts it again, as it does every text field, to one plain line); then it is thrown again, so the app, its ownerrorlisteners and the console see what they did before. The window's report of that same error isn't said again: one whoseerroris the object thrown, or WebKit's "Script error." within 100 ms of one said. An error thrown elsewhere, as by an inlineonclickor the app's top-level script, is said as before. After an uncaught error, where the app was animating, ten or more of its own frame callbacks in the second before the error, a loop and not a frame drawn now and then on a tap, the document waits 2 s and saysportalr/app-stopped,stopped"yes" where no frame of the app's ran in that time and "no" where one did, with the error that set it off (messageandstack, cut as an error's are), and says it again only when that changes, as when frames come back; not where the document was hidden, when browsers pause frames. Frames alone count: timers don't, since the sandbox's own scripts use them, so an app driven by timers alone, one with no loop, or one that draws on demand and sits idle is never said to have stopped. The bridge countsportalr/app-stoppedapart from its eight other reports a frame, four of its own, so earlier errors and looks can't crowd out the word that the app stopped, or the error it stopped after, which comes with it. Theappsadd-on shows a card under the frame for the app's latest thrown error or refused promise: an error, The app stopped after an error, only wherestoppedis "yes", and otherwise a warning, The app reported an error, which claims nothing about whether it runs: the error as the app said it (a masked one as "This browser doesn't say which error."), a collapsed Details with its stack and the latest look, Reload app, which makes the frame afresh as Try again does, and the error it stopped after kept over any later one, and Copy the error for the assistant, which writes "The app froze after an error: … Please find the cause and fix it." to the clipboard where it stopped, and "The app threw an error: …" otherwise, and says so, or, where the clipboard refuses (a page over plain HTTP has none), shows that sentence, selected once as it appears, to copy by hand. The thread's composer has no way to be filled from outside it without more of the initial bundle, so the sentence is copied, not put there; nothing is ever sent for the person. The report under the frame, "The app reported a problem", stays, but leaves out what the card says, its error and the look, so neither is shown twice. What is proven: in Chromium, byui's unit tests (each failing on the document as it was) and the CSP tests (a game whose frame loop throws on its thirtieth frame shows the card with its own error, copies the sentence and reloads; one that draws on is never said to have stopped; the healthy apps show no card); and in Playwright's WebKit, at an iPhone's viewport, on the saved game itself in a frame sandboxed as the sandbox frames it: the document as it was said "Script error.", and now saysTypeError: null is not an object (evaluating 'p.dir.x')with its stack, andstopped"yes". What is inferred: that iOS Safari on a phone behaves as Playwright's WebKit does here, which no test reaches; and that a frame loop that stops after an error stopped the app, where an app could stop its own loop then on purpose, or a browser stop frames to a frame scrolled out of view; and the error on a card that says nothing stopped is the latest the document said, the first five that differ. The error's text is the app's own, untrusted: shown as text and put on the clipboard when the person asks, never sent anywhere, counted or kept. Theappsadd-on grows from 6,982 to 7,737 bytes of 9,000; the app's initial bundle is 203,330 of 203,500, unchanged but for its chunks' hashed names.
Action items¶
-
Site.apps,config.json'sapps,frame-src,SandboxOrigin, the sandbox page and the wheel's check. -
ui's bridge, sandbox,AppFrameand theappsadd-on; the app's host and kill switch; tests and screenshots. - stackr: the apps' origin at the edge and in the template, locally on its own port (lattice RFC-0005, phase 2).
- WebRTC from a frame the app makes: refused where a browser lets a page refuse it, and the label and RFC-0005's exit criterion met then.