Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/ui/file-preview/src/components/html-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
*
* The rendered preview runs inside a nested sandboxed iframe, which is itself inside
* the MCP app's sandboxed iframe chain. Scripts and external resources (CDNs) are
* allowed since the sandbox isolation prevents any escape.
* allowed for inline interactivity, while CSP blocks outbound requests.
*/
import { renderCodeViewer } from './code-viewer.js';
import { escapeHtml } from './highlighting.js';
import type { HtmlPreviewMode } from '../types.js';

const HTML_PREVIEW_CSP = [
"default-src 'none'",
"script-src 'unsafe-inline'",
"style-src 'unsafe-inline'",
'img-src data: blob:',
'media-src data: blob:',
"connect-src 'none'",
"form-action 'none'",
"base-uri 'none'",
].join('; ');

function resolveThemeFrameStyles(): { background: string; text: string; fontFamily: string } {
if (typeof window === 'undefined' || typeof document === 'undefined') {
return {
Expand All @@ -27,7 +38,7 @@ function resolveThemeFrameStyles(): { background: string; text: string; fontFami

function renderSandboxedHtmlFrame(content: string): string {
const palette = resolveThemeFrameStyles();
const frameDocument = `<!doctype html><html><head><meta charset="utf-8" /><style>html,body{margin:0;padding:0;background:${palette.background};color:${palette.text};}body{font-family:${palette.fontFamily};padding:16px;line-height:1.5;}img{max-width:100%;height:auto;}</style></head><body>${content}</body></html>`;
const frameDocument = `<!doctype html><html><head><meta charset="utf-8" /><meta http-equiv="Content-Security-Policy" content="${HTML_PREVIEW_CSP}" /><style>html,body{margin:0;padding:0;background:${palette.background};color:${palette.text};}body{font-family:${palette.fontFamily};padding:16px;line-height:1.5;}img{max-width:100%;height:auto;}</style></head><body>${content}</body></html>`;
return `<iframe class="html-rendered-frame" title="Rendered HTML preview" sandbox="allow-scripts allow-forms allow-popups" referrerpolicy="no-referrer" srcdoc="${escapeHtml(frameDocument)}"></iframe>`;
}

Expand Down
12 changes: 12 additions & 0 deletions test/test-html-preview-csp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import assert from 'assert';
import { renderHtmlPreview } from '../dist/ui/file-preview/src/components/html-renderer.js';

const rendered = renderHtmlPreview('<script>fetch("https://example.com")</script>', 'rendered');

assert.ok(rendered.html.includes('Content-Security-Policy'));
assert.ok(rendered.html.includes('connect-src &#39;none&#39;'));
assert.ok(rendered.html.includes('form-action &#39;none&#39;'));
assert.ok(rendered.html.includes('sandbox="allow-scripts allow-forms allow-popups"'));
assert.ok(rendered.html.includes('&lt;script&gt;fetch(&quot;https://example.com&quot;)&lt;/script&gt;'));

console.log('PASS: rendered HTML previews carry a restrictive CSP');