-
Notifications
You must be signed in to change notification settings - Fork 217
feat: add sandbox flags negotiation #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f37e985
5895acd
4c75c0a
3480b98
3518380
5d242a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { RESOURCE_MIME_TYPE, getToolUiResourceUri, type McpUiSandboxProxyReadyNotification, AppBridge, PostMessageTransport, type McpUiResourceCsp, type McpUiResourcePermissions, buildAllowAttribute, type McpUiUpdateModelContextRequest, type McpUiMessageRequest } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||
| import { RESOURCE_MIME_TYPE, getToolUiResourceUri, type McpUiSandboxProxyReadyNotification, AppBridge, PostMessageTransport, type McpUiResourceCsp, type McpUiResourcePermissions, type McpUiResourceSandbox, buildAllowAttribute, buildSandboxAttribute, type McpUiUpdateModelContextRequest, type McpUiMessageRequest } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||
| import { Client } from "@modelcontextprotocol/sdk/client/index.js"; | ||
| import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js"; | ||
| import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"; | ||
|
|
@@ -72,6 +72,7 @@ interface UiResourceData { | |
| html: string; | ||
| csp?: McpUiResourceCsp; | ||
| permissions?: McpUiResourcePermissions; | ||
| sandbox?: McpUiResourceSandbox; | ||
| } | ||
|
|
||
| export interface ToolCallInfo { | ||
|
|
@@ -151,20 +152,25 @@ async function getUiResource(serverInfo: ServerInfo, uri: string): Promise<UiRes | |
| const uiMeta = contentMeta?.ui ?? listingMeta?.ui; | ||
| const csp = uiMeta?.csp; | ||
| const permissions = uiMeta?.permissions; | ||
| const sandbox = uiMeta?.sandbox; | ||
|
|
||
| return { html, csp, permissions }; | ||
| return { html, csp, permissions, sandbox }; | ||
| } | ||
|
|
||
|
|
||
| export function loadSandboxProxy( | ||
| iframe: HTMLIFrameElement, | ||
| csp?: McpUiResourceCsp, | ||
| permissions?: McpUiResourcePermissions, | ||
| sandbox?: McpUiResourceSandbox, | ||
|
||
| ): Promise<boolean> { | ||
| // Prevent reload | ||
| if (iframe.src) return Promise.resolve(false); | ||
|
|
||
| iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms"); | ||
| // Set sandbox attribute on outer iframe. In practice, these will match the inner | ||
| // iframe sandbox capabilities, but at a minimum they must be a superset of the | ||
| // inner iframe's sandbox allowances. | ||
| iframe.setAttribute("sandbox", buildSandboxAttribute(sandbox)); | ||
idosal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Set Permission Policy allow attribute based on requested permissions | ||
| const allowAttribute = buildAllowAttribute(permissions); | ||
|
|
@@ -214,10 +220,10 @@ export async function initializeApp( | |
| new PostMessageTransport(iframe.contentWindow!, iframe.contentWindow!), | ||
| ); | ||
|
|
||
| // Load inner iframe HTML with CSP and permissions metadata | ||
| const { html, csp, permissions } = await appResourcePromise; | ||
| log.info("Sending UI resource HTML to MCP App", csp ? `(CSP: ${JSON.stringify(csp)})` : "", permissions ? `(Permissions: ${JSON.stringify(permissions)})` : ""); | ||
| await appBridge.sendSandboxResourceReady({ html, csp, permissions }); | ||
| // Load inner iframe HTML with CSP, permissions, and sandbox metadata | ||
| const { html, csp, permissions, sandbox } = await appResourcePromise; | ||
| log.info("Sending UI resource HTML to MCP App", csp ? `(CSP: ${JSON.stringify(csp)})` : "", permissions ? `(Permissions: ${JSON.stringify(permissions)})` : "", sandbox ? `(Sandbox: ${JSON.stringify(sandbox)})` : ""); | ||
| await appBridge.sendSandboxResourceReady({ html, csp, permissions, sandbox }); | ||
|
|
||
| // Wait for inner iframe to be ready | ||
| log.info("Waiting for MCP App to initialize..."); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import type { McpUiSandboxProxyReadyNotification, McpUiSandboxResourceReadyNotification } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||
| import { buildAllowAttribute } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||
| import { buildAllowAttribute, buildSandboxAttribute } from "@modelcontextprotocol/ext-apps/app-bridge"; | ||
|
|
||
| const ALLOWED_REFERRER_PATTERN = /^http:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/; | ||
|
|
||
|
|
@@ -41,12 +41,15 @@ try { | |
| // iframe on a separate origin. It creates an inner iframe for untrusted HTML | ||
| // content. Per the specification, the Host and the Sandbox MUST have different | ||
| // origins. | ||
| const inner = document.createElement("iframe"); | ||
| inner.style = "width:100%; height:100%; border:none;"; | ||
| inner.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms"); | ||
| // Note: allow attribute is set later when receiving sandbox-resource-ready notification | ||
| // based on the permissions requested by the app | ||
| document.body.appendChild(inner); | ||
| // | ||
| // The inner iframe is created lazily when the `sandbox-resource-ready` | ||
| // notification arrives (not at file top) so that its `sandbox` attribute is set | ||
| // to the negotiated value *before* the element is inserted into the DOM. | ||
| // Per the HTML spec, mutating the `sandbox` attribute on an already-attached | ||
| // iframe only takes effect after a navigation; `document.write()` is NOT a | ||
| // navigation, so setting the attribute after attachment would leave the old | ||
| // (or default) flags in effect. | ||
| let inner: HTMLIFrameElement | null = null; | ||
|
|
||
| const RESOURCE_READY_NOTIFICATION: McpUiSandboxResourceReadyNotification["method"] = | ||
| "ui/notifications/sandbox-resource-ready"; | ||
|
|
@@ -85,15 +88,27 @@ window.addEventListener("message", async (event) => { | |
|
|
||
| if (event.data && event.data.method === RESOURCE_READY_NOTIFICATION) { | ||
| const { html, sandbox, permissions } = event.data.params; | ||
| if (typeof sandbox === "string") { | ||
| inner.setAttribute("sandbox", sandbox); | ||
| } | ||
| // sandbox can be a string (raw override) or object (structured flags) | ||
| const sandboxAttr = typeof sandbox === "string" | ||
| ? sandbox | ||
| : buildSandboxAttribute(sandbox); | ||
| console.log("[Sandbox] Setting sandbox attribute:", sandboxAttr); | ||
|
|
||
| // Create the inner iframe with the final sandbox attribute before | ||
| // inserting into the DOM, so negotiated flags are enforced immediately. | ||
| inner = document.createElement("iframe"); | ||
| inner.style.cssText = "width:100%; height:100%; border:none;"; | ||
| inner.setAttribute("sandbox", sandboxAttr); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This concerns me because See #158 (comment). |
||
|
|
||
| // Set Permission Policy allow attribute if permissions are requested | ||
| const allowAttribute = buildAllowAttribute(permissions); | ||
| if (allowAttribute) { | ||
| console.log("[Sandbox] Setting allow attribute:", allowAttribute); | ||
| inner.setAttribute("allow", allowAttribute); | ||
| } | ||
|
|
||
| document.body.appendChild(inner); | ||
|
|
||
| if (typeof html === "string") { | ||
| // Use document.write instead of srcdoc (which the CesiumJS Map won't work with) | ||
| const doc = inner.contentDocument || inner.contentWindow?.document; | ||
|
|
@@ -112,7 +127,7 @@ window.addEventListener("message", async (event) => { | |
| inner.contentWindow.postMessage(event.data, "*"); | ||
| } | ||
| } | ||
| } else if (event.source === inner.contentWindow) { | ||
| } else if (inner && event.source === inner.contentWindow) { | ||
| if (event.origin !== OWN_ORIGIN) { | ||
| console.error( | ||
| "[Sandbox] Rejecting message from inner iframe with unexpected origin:", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.