Skip to content

The agent that grows with you

fix(webapp): open large HTML previews in the browser through srcdoc

David Dudok de Wit7075e2f99b95
diff --git a/apps/desktop/src/lib/browser-desktop-bridge.test.ts b/apps/desktop/src/lib/browser-desktop-bridge.test.ts
index 6bf90d445aa..914ec1b911f 100644
--- a/apps/desktop/src/lib/browser-desktop-bridge.test.ts
+++ b/apps/desktop/src/lib/browser-desktop-bridge.test.ts
@@ -778,6 +778,30 @@ describe('browser-hosted Desktop bridge', () => {
expect(stagedFile.type).toBe('text/plain')
})
+ it('opens a 2 MB HTML page through srcdoc, byte-exact and inert to the wrapper, never through a frame URL', async () => {
+ const win = mutableWindow()
+ seedWebappSession('served-token')
+ const blobs: Blob[] = []
+
+ vi.spyOn(URL, 'createObjectURL').mockImplementation(blob => {
+ blobs.push(blob as Blob)
+
+ return 'blob:http://127.0.0.1:9119/large'
+ })
+
+ expect(installBrowserDesktopBridge()).toBe(true)
+ // Chromium will not navigate a frame to a URL over 2 MiB: the old data: carrier left this blank.
+ const page = `<!doctype html><p title="a&amp;b">"q" &lt;x&gt;</p><!-- "></iframe><script>parent.x=1</script> --><script>/*${'x'.repeat(2_000_000)}*/</script>`
+ await win.hermesDesktop!.saveImageBuffer(new TextEncoder().encode(page), '.html')
+ const wrapper = new DOMParser().parseFromString(await blobs[0]!.text(), 'text/html')
+ const frame = wrapper.querySelector('iframe')!
+
+ expect(wrapper.querySelectorAll('iframe, script')).toHaveLength(1)
+ expect(frame.getAttribute('sandbox')).toBe('allow-scripts')
+ expect(frame.hasAttribute('src')).toBe(false)
+ expect(frame.getAttribute('srcdoc')).toBe(page)
+ })
+
it('stages non-image buffers as browser-local object URLs', async () => {
const win = mutableWindow()
seedWebappSession('served-token')
@@ -802,7 +826,8 @@ describe('browser-hosted Desktop bridge', () => {
expect(wrapper).toContain('sandbox="allow-scripts"')
expect(wrapper).not.toContain('allow-same-origin')
expect(wrapper).not.toContain('<h1>preview</h1>')
- expect(wrapper).toContain('data:text/html;charset=utf-8;base64,PGgxPnByZXZpZXc8L2gxPg==')
+ expect(wrapper).toContain('srcdoc="&lt;h1&gt;preview&lt;/h1&gt;"')
+ expect(wrapper).not.toMatch(/\ssrc="data:/)
await win.hermesDesktop!.openPreviewInBrowser!(staged)
expect(open).toHaveBeenCalledExactlyOnceWith(staged, '_blank', 'noopener,noreferrer')
diff --git a/apps/desktop/src/lib/browser-uploads.ts b/apps/desktop/src/lib/browser-uploads.ts
index 651cc3b6e53..d42f420c721 100644
--- a/apps/desktop/src/lib/browser-uploads.ts
+++ b/apps/desktop/src/lib/browser-uploads.ts
@@ -1,5 +1,5 @@
import type { HermesApiRequest, HermesSelectPathsOptions, HermesStagedUpload } from '@/global'
-import { blobToDataUrl, bytesToBase64 } from '@/lib/base64'
+import { blobToDataUrl } from '@/lib/base64'
import { type BrowserBootstrap, browserFetch } from '@/lib/browser-transport'
const STAGED_UPLOAD_CACHE_LIMIT = 256
@@ -46,15 +46,24 @@ function rememberBounded<K, V>(cache: Map<K, V>, key: K, value: V): void {
}
}
+// The page reaches its opaque-origin frame through srcdoc, never a URL: Chromium refuses to
+// navigate a frame to a URL over 2 MiB, so a base64 data URL left any page above ~1.5 MB blank.
+// Decoded as the data URL's charset=utf-8 did, and escaped for a double-quoted attribute (`&`
+// first) so no byte of the page parses as markup of this same-origin wrapper.
function sandboxedHtmlBlob(bytes: Uint8Array): Blob {
- const source = `data:text/html;charset=utf-8;base64,${bytesToBase64(bytes)}`
+ const srcdoc = new TextDecoder('utf-8')
+ .decode(bytes)
+ .replace(/&/g, '&amp;')
+ .replace(/"/g, '&quot;')
+ .replace(/</g, '&lt;')
+ .replace(/>/g, '&gt;')
const wrapper = [
'<!doctype html>',
'<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">',
'<style>html,body,iframe{box-sizing:border-box;width:100%;height:100%;margin:0;border:0}</style>',
'</head><body>',
- `<iframe sandbox="allow-scripts" referrerpolicy="no-referrer" src="${source}"></iframe>`,
+ `<iframe sandbox="allow-scripts" referrerpolicy="no-referrer" srcdoc="${srcdoc}"></iframe>`,
'</body></html>'
].join('')