What isolates a widget from the host page, the default Content Security Policy, and what the host still decides.
Widget code is written by a model, so treat it as untrusted. This page lists what the package isolates and what is left to the host.
Every widget runs on its own site
By default, each HTML or SVG widget renders in a Safe Content Frame. The frame is served from a hashed subdomain of a domain on the Public Suffix List, so browsers treat it as a separate site from your app: widget scripts cannot reach your page's DOM, cookies, or storage, even though the frame allows scripts. How Safe Content Frame works explains the origin scheme.
product scopes the frame origin (it defaults to "generative-frame"), and frame accepts a preconfigured SafeContentFrame, for example one with useShadowDom.
This guarantee depends on the frame mode. A self-hosted shim keeps it only on a domain on the Public Suffix List, and opaque-origin frames isolate by a null origin instead of a separate site; both are described below.
Hosting the shim yourself
unsafeShimDomain loads the shim from https://<hash>-h184756.<domain>/<product>/shim.html on a domain you serve instead of scf.auiusercontent.com:
createWidget({ container, unsafeShimDomain: "usercontent.example" });Here usercontent.example stands for a domain you have listed as a public suffix. The isolation boundary holds only if <domain> is a public suffix on the Public Suffix List. Otherwise browsers treat every hashed subdomain as the same site: widgets can read and set cookies scoped to the parent domain, which reach each other and your app if it lives under the same registrable domain, and the browser may run them in one renderer process. Use a dedicated domain, and get it on the list before you rely on it.
Serving the shim needs:
- a wildcard DNS record and a wildcard TLS certificate for
*.<domain>, /<product>/shim.htmlfor everyproductyou render with (generative-frameby default),- a shim that implements the handshake in How Safe Content Frame works: it accepts content only from the
originin its query, recomputes the hash from the salt, and compares it with its own hostname, - a redeploy whenever
safe-content-framechanges the shim version in the hostname (h184756today).
A missing or mismatched shim fails with a shim-unavailable or shim-error load error.
Opaque-origin frames
opaqueOrigin: true skips Safe Content Frame and renders the bootstrap document with srcdoc in an iframe sandboxed with allow-scripts only. The frame's origin is null, so widget code cannot reach your page, its own storage, or cookies, and nothing is loaded from a shim domain.
createWidget({ container, opaqueOrigin: true });The tradeoffs:
- No storage.
localStorage,sessionStorage, IndexedDB, and cookies throw or stay empty, soidandclearWidgetStoragedo not apply, and widgets that expect storage fail. - Your page's policy applies. A
srcdocdocument inherits the Content Security Policy of the page that embeds it, on top of the widget policy. The runtime and widget scripts are inline, so a pagescript-srcwithout'unsafe-inline'(for example a nonce- or hash-based policy) blocks them: if it blocks the runtime, the frame never connects and the widget fails with a ready timeout; if it allows the runtime by hash, widget scripts still do not run. - Process isolation is weaker. Desktop Chrome puts sandboxed frames in a process apart from your app, but all of them share it; a Safe Content Frame gets a process of its own.
- Messages to the frame are untargeted. A
nullorigin cannot be named as apostMessagetarget, so the host posts with"*"and accepts messages only from that iframe's window with originnull. After widget code navigates the frame and the new page loads, the host stops exchanging messages with it. This is best-effort: the new page can post before it finishes loading, which grants it nothing the widget code could not already do. Widget code can navigate the frame, so anything it could read before navigating it can carry along.
How code reaches the frame
The frame loads one bootstrap document: the Content Security Policy, the theme, and the in-frame runtime. The runtime announces itself to the host origin baked into that document, and the host answers once with a MessageChannel port. Widget code then streams over that private port as JSON-RPC 2.0 messages.
The host also answers the same requests when they arrive as window messages, because widgets built on the MCP Apps SDK talk to window.parent. Those are accepted only when the message's source is the frame's own window and its origin is the frame's origin.
Theme variables written into the bootstrap are filtered: names must be plain custom property names and values cannot contain ;, {, }, <, or >.
The default policy
Every frame enforces a CSP <meta> tag that widget code cannot lift. By default (buildCsp()):
| Directive | Allowed |
|---|---|
default-src | 'none' |
script-src | inline scripts, and the CDN origins |
style-src | inline styles and the CDN origins |
img-src | data:, blob:, and the CDN origins |
font-src | data: and the CDN origins |
media-src | data:, blob:, and the CDN origins |
connect-src | 'none': no fetch, XHR, or WebSocket |
frame-src | 'none' |
worker-src | blob: |
object-src, base-uri, form-action | 'none' |
The CDN origins are https://cdnjs.cloudflare.com, https://cdn.jsdelivr.net, https://unpkg.com, and https://esm.sh (DEFAULT_CDN_ORIGINS). A widget can load any package published to those CDNs, so the allowlist limits where code comes from, not what it does once loaded. eval and new Function are blocked.
Configure the policy with csp on createWidget:
createWidget({
container,
csp: {
cdnOrigins: ["https://cdn.jsdelivr.net"],
connectOrigins: ["https://api.example.com"],
imageOrigins: ["https://images.example.com"],
frameOrigins: [],
allowEval: false,
},
});| Option | Effect |
|---|---|
cdnOrigins | Replaces the CDN list for scripts, styles, images, fonts, and media. |
fontStyleOrigins | Extra font stylesheet origins, added only to style-src. Empty by default. |
fontOrigins | Extra font file origins, added only to font-src. Empty by default. |
connectOrigins | Origins fetch, XHR, and WebSocket may reach. wss:// origins are accepted here. |
imageOrigins | Extra image origins, such as an image proxy. |
frameOrigins | Origins that nested frames may load. |
allowEval | Adds 'unsafe-eval', which some charting libraries need. |
csp: { cdnOrigins: [] } blocks external scripts, stylesheets, font files, images, and media unless you explicitly add resource origins. Inline styles and scripts, data: fonts, and data:/blob: images and media still work.
Google Fonts requires an explicit opt-in for both its stylesheets and font files:
const csp = {
cdnOrigins: [],
fontStyleOrigins: ["https://fonts.googleapis.com"],
fontOrigins: ["https://fonts.gstatic.com"],
};
createWidget({ container, csp });
buildWidgetGuidance(csp);The font stylesheet allowance cannot load scripts, images, or font files, and the font file allowance cannot load scripts, stylesheets, or images. For opaque-origin frames, the parent page's CSP is also enforced: adding these origins to the widget policy cannot relax the parent's restrictions.
Sources that are not plain http(s) origins (with a *. wildcard allowed) or the keywords 'self', 'none', data:, blob:, and https: are dropped. You can also pass a complete policy string, which is used as is. Keep the guidance in sync, so the model does not reach for something the policy blocks.
Scripts wait for complete code
Scripts are held while code streams and run once, in document order, after end(). This exists so scripts never run against half-written markup. It is not a security boundary: complete code runs with everything the frame allows.
What the host decides
The frame isolates the widget from your page. What a widget can do through the bridge is set by the handlers you pass:
onPrompt/onMessage. A widget can callsendPrompt(text)at any time, with or without a click; the assistant-ui toolkit's defaultonPromptadds the text to the thread composer, after any draft, for the user to review and send. Without either handler, the call fails, and a host that supplies its own handler decides whether to send the message.onOpenLink. Link clicks,window.open, andopenLink(url)all go through it. Onlyhttp:andhttps:URLs reach the handler. Without a handler, the URL opens in a new tab withnoopener,noreferrerand no confirmation. A URL can carry data in its query, so a host that wants a confirmation step passes its own handler.onCopyText. Off unless you pass it.genframe.copyText(text)andnavigator.clipboard.writeText(text)in a widget reach it, and the host cannot tell whether the user asked for the copy: a cross-origin frame can take focus without a user action and send the request right after a click elsewhere on the page. Confirm in host UI, such as a Copy control the host renders, before writing the clipboard, or a widget can replace what the user copies. A widget never reads the clipboard.onCallTool. Off unless you pass it. The widget chooses the tool name and arguments, so allowlist names and validate arguments in the handler.onUpdateModelContext. Off unless you pass it, except in the assistant-ui toolkit, which adds what a widget shares to the system prompt of later requests as a section that marks the value as data, JSON-encoded with<escaped, and holds at most 4,000 characters of it. The content comes from model-written code and can carry text from tool results, so treat it as untrusted input; passing your ownwidget.onUpdateModelContexttakes this over, after which the toolkit shares nothing and your handler decides what, if anything, reaches the model.onRequestDisplayModeandonWidgetState. Without a handler, display-mode requests return the current mode and widget state is dropped.onInitialized. Receives what an MCP Apps widget declares inui/initialize, such as itsappInfoand the display modes it supports. Widget code writes all of it, so use it to narrow what you grant, never to identify or trust the widget.- Tool data. Whatever you forward with
notifyToolInputandnotifyToolResultis readable by widget code. - Policy.
connectOriginsgives widget code network access to those origins, andallowEvalre-enableseval.
What a widget can send out
connect-src 'none' stops fetch, XHR, and WebSockets, but a widget can still move what it knows out of the frame:
- in the text it passes to
sendPrompt, - in the URLs it passes to
openLinkor puts on links, - in the URLs of images, styles, fonts, and scripts it loads from the allowlisted CDNs, which can carry data in their path or query.
What a widget knows is what the model wrote into it plus what you send it. Theme variables are low-sensitivity; conversation content in the widget code or tool data is what matters. Treat these channels like any other model output: confirm openLink in its handler, show or confirm prompts that should not send silently, and narrow cdnOrigins and imageOrigins when widgets do not need them.
Spec mode runs no model code
In spec mode there is no frame and no model-written code. The model writes data: which of your catalog's components to render, with which props, bound to which of your actions. With the catalog passed to SpecRenderer, props are validated before rendering. Expressions only read and write the spec's state, and every action other than the built-in setState runs through a handler you wrote. The components and handlers you supply are the whole attack surface.
For sensitive host actions, set confirm: true in the catalog, pass that catalog to SpecRenderer, and supply a host owned confirmAction callback. Without the catalog, the gate does not apply. The spec cannot change the gate, and a watch cannot trigger it.
The model chooses every image URL, so loading an image sends a request to an origin the model picked, and that URL can carry anything the model has read. When your app handles private data, pass imageOrigins to toSpecCatalog with the origins you trust; the built-in Image renders its alt text for an image from any other origin. The built-in Image also loads with referrerPolicy="no-referrer", so the request does not carry the page's address. A custom component that renders images must apply both itself.