-
Notifications
You must be signed in to change notification settings - Fork 49
feat: enhance sandbox capability negotiation #158
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
Open
idosal
wants to merge
4
commits into
main
Choose a base branch
from
feat/sandbox-capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+747
−118
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
699549e
feat: enhance sandbox capability negotiation
idosal 7aef0d8
Update specification/draft/apps.mdx
idosal 0e4e524
Merge remote-tracking branch 'origin/main' into feat/sandbox-capabili…
idosal e038020
add all csp and permissions to host capabilities + update SEP
idosal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
idosal marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <!-- CSP is set via HTTP response headers in serve.ts --> | ||
| <!-- Note: No CSP meta tag here - CSP is set on the actual remote HTML content --> | ||
| <!-- and needs to be super relaxed otherwise nothing loads --> | ||
| <title>MCP-UI Proxy</title> | ||
| <style> | ||
| html, | ||
| body { | ||
| margin: 0; | ||
| height: 100vh; | ||
| width: 100vw; | ||
| } | ||
| body { | ||
| display: flex; | ||
| flex-direction: column; | ||
| } | ||
| * { | ||
| box-sizing: border-box; | ||
| } | ||
| iframe { | ||
| background-color: transparent; | ||
| border: 0px none transparent; | ||
| padding: 0px; | ||
| overflow: hidden; | ||
| flex-grow: 1; | ||
| } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <script> | ||
| // Security checks | ||
| if (window.self === window.top) { | ||
| throw new Error("This file is only to be used in an iframe sandbox."); | ||
| } | ||
| if (!document.referrer) { | ||
| throw new Error("No referrer, cannot validate embedding site."); | ||
| } | ||
| if (!document.referrer.match(/^http:\/\/(localhost|127\.0\.0\.1)(:|\/|$)/)) { | ||
| throw new Error(`Embedding domain not allowed in referrer ${document.referrer} (update the validation logic to allow your domain)`); | ||
| } | ||
|
|
||
| // Try to break out of iframe (security test) | ||
| try { | ||
| window.top.alert("If you see this, the sandbox is not setup securely."); | ||
| throw new Error("Managed to break out of iframe, the sandbox is not setup securely."); | ||
| } catch (e) { | ||
| // Expected to fail | ||
| } | ||
|
|
||
| const inner = document.createElement('iframe'); | ||
| inner.style = 'width:100%; height:100%; border:none;'; | ||
| // sandbox will be set from postMessage payload; default minimal before html arrives | ||
| // Use allow-same-origin for document.write to work | ||
| inner.setAttribute('sandbox', 'allow-scripts allow-same-origin'); | ||
| document.body.appendChild(inner); | ||
|
|
||
| // Build iframe allow attribute from permissions | ||
| function buildAllowAttribute(permissions) { | ||
| if (!permissions) return ''; | ||
| const allowList = []; | ||
| if (permissions.camera) allowList.push('camera'); | ||
| if (permissions.microphone) allowList.push('microphone'); | ||
| if (permissions.geolocation) allowList.push('geolocation'); | ||
| return allowList.join('; '); | ||
| } | ||
|
|
||
| window.addEventListener('message', async (event) => { | ||
| if (event.source === window.parent) { | ||
| if (event.data && event.data.method === 'ui/notifications/sandbox-resource-ready') { | ||
| const { html, sandbox, permissions } = event.data.params || {}; | ||
| // Note: csp is not extracted here - CSP is set via HTTP response headers in serve.ts | ||
| if (typeof sandbox === 'string') { | ||
| // Ensure allow-same-origin is present for document.write to work | ||
| let finalSandbox = sandbox; | ||
| if (!finalSandbox.includes('allow-same-origin')) { | ||
| finalSandbox = finalSandbox + ' allow-same-origin'; | ||
| } | ||
| inner.setAttribute('sandbox', finalSandbox); | ||
| } | ||
| // Set Permission Policy allow attribute if permissions are provided | ||
| const allowAttribute = buildAllowAttribute(permissions); | ||
| if (allowAttribute) { | ||
| inner.setAttribute('allow', allowAttribute); | ||
| } | ||
| if (typeof html === 'string') { | ||
| // Use document.write instead of srcdoc to avoid CSP base-uri issues | ||
| // document.write allows the browser to resolve relative URLs correctly | ||
| // Match the pattern from mcp-ui proxy script (https://github.com/MCP-UI-Org/mcp-ui/pull/140) | ||
| // This is the main change to support the double iframe exactly the same way as ChatGPT | ||
| // The iframe is rendered first (created at page load), then we write when HTML arrives | ||
| try { | ||
| if (inner.contentDocument) { | ||
| inner.contentDocument.open(); | ||
| inner.contentDocument.write(html); | ||
| inner.contentDocument.close(); | ||
| } else { | ||
| // Fallback to srcdoc if contentDocument is not accessible | ||
| inner.srcdoc = html; | ||
| } | ||
| } catch (e) { | ||
| console.error('Failed to write HTML to iframe:', e); | ||
| // Fallback to srcdoc if document.write fails | ||
| inner.srcdoc = html; | ||
| } | ||
| } | ||
| } else { | ||
| if (inner && inner.contentWindow) { | ||
| inner.contentWindow.postMessage(event.data, '*'); | ||
| } | ||
| } | ||
| } else if (event.source === inner.contentWindow) { | ||
| // Relay messages from inner to parent | ||
| window.parent.postMessage(event.data, '*'); | ||
| } | ||
| }); | ||
|
|
||
| // Notify parent that proxy is ready to receive HTML | ||
| window.parent.postMessage({ | ||
| jsonrpc: '2.0', | ||
| method: 'ui/notifications/sandbox-proxy-ready', | ||
| params: {} | ||
| }, '*'); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's make these objects (as in capabilities) for future extensions?
e.g. what if one day there's fine-grained vers. coarse geolocation, or front vs. back camera permission, etc.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like CSP, Permissions are coupled to the browser spec (Permissions Policy). I don't think we should diverge at this point. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 40+ Permission Policies you can stuff in an iFrame. Consider keeping
permissionsvery simple and flexible, just have it be astring[].Benefits:
allow. This is safe because invalid strings are silently ignored.permissions.contains("camera");.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively create some interface and have permission be an array of that interface if type safety and enforcement is of high importance.
permissions?: Permissions[]