A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Frame the design through a blob: URL so previews with images are not blank
Esteban Suarez32826dad015a
diff --git a/app/api/artifact-frame/route.ts b/app/api/artifact-frame/route.tsindex c6e9433..e9244d3 100644--- a/app/api/artifact-frame/route.ts+++ b/app/api/artifact-frame/route.ts@@ -1,4 +1,4 @@-import { DESIGN_ARTIFACT_EMBEDDED_CSP, DESIGN_FRAME_CSP, prepareDesignArtifactHtml, remoteImagesEnabled } from '../../../lib/design-artifact'+import { DESIGN_ARTIFACT_EMBEDDED_CSP, DESIGN_FRAME_CSP, designFrameHtml, prepareDesignArtifactHtml, remoteImagesEnabled } from '../../../lib/design-artifact'import { embedRemoteImages } from '../../../lib/remote-image'import { cachedRemoteImage } from '../../../lib/remote-image-cache'import { readDesignArtifactAt } from '../../../lib/journal'@@ -42,60 +42,7 @@ export const GET = withSessionRoute(async (request, { id: sessionId, user }) =>artifact != null ? 'This design could not be prepared for preview.': past ? 'This version is not in the history.' : 'No design artifact has been published for this session.'}</p></body></html>`- const artifactUrl = `data:text/html;base64,${Buffer.from(preparedArtifact, 'utf8').toString('base64')}`- const serializedArtifactUrl = JSON.stringify(artifactUrl)- const serializedReadyToken = JSON.stringify(readyToken)- // The wrapper paints nothing of its own, so a design that has not painted yet- // shows the app's background. Sharing the app's color scheme keeps it- // transparent; a design that sets no background still gets the browser's white.- const html = `<!doctype html>-<html>-<head>-<meta charset="utf-8">-<meta name="viewport" content="width=device-width,initial-scale=1">-<title>Design artifact preview</title>-<style>:root{color-scheme:light dark}html,body,#frame-root,iframe{width:100%;height:100%;margin:0;border:0;overflow:hidden}</style>-</head>-<body>-<div id="frame-root"></div>-<script>-const artifact=${serializedArtifactUrl};-const token=${serializedReadyToken};-const frame=document.createElement('iframe');-frame.title='Interactive design artifact';-frame.setAttribute('sandbox','allow-scripts');-frame.referrerPolicy='no-referrer';-frame.setAttribute('credentialless','');-let readySignals=0;-let consumedSignals=0;-let lastHeartbeat=Date.now();-const restore=()=>{lastHeartbeat=Date.now();frame.src=artifact;};-addEventListener('message',event=>{- if(event.source!==frame.contentWindow||event.data?.token!==token)return;- lastHeartbeat=Date.now();- if(event.data.type==='ready')readySignals++;- // The design paints in its own process, after this wrapper's load event; the app- // swaps a new revision on screen only once it has.- if(event.data.type==='painted')parent.postMessage({type:'fx0-artifact-painted'},location.origin);- // The design can only ask to open a link: its scripts can read the token and send- // this at any time. The app shows the address and opens it on the user's own click- // there. Recent user activation only filters out asks nobody prompted; it is not proof.- if(event.data.type==='open'&&typeof event.data.url==='string'&&navigator.userActivation?.isActive!==false){- parent.postMessage({type:'fx0-artifact-open',url:event.data.url},location.origin);- }-});-frame.addEventListener('load',()=>setTimeout(()=>{- if(readySignals>consumedSignals){consumedSignals=readySignals;return;}- restore();-},100));-setInterval(()=>{- if(document.visibilityState==='visible'&&Date.now()-lastHeartbeat>3000)restore();-},500);-frame.src=artifact;-document.querySelector('#frame-root').append(frame);-</script>-</body>-</html>`+ const html = designFrameHtml(preparedArtifact, readyToken)// Streamed: embedded images can take it past the buffered response limit.const bytes = new TextEncoder().encode(html)if (embedded.images) log(sessionId, 'artifact:images', { images: embedded.images, complete: embedded.complete, embedMs, bytes: bytes.length })diff --git a/lib/design-artifact.ts b/lib/design-artifact.tsindex 8922822..1b72b44 100644--- a/lib/design-artifact.ts+++ b/lib/design-artifact.ts@@ -35,11 +35,11 @@ export const DESIGN_ARTIFACT_EMBEDDED_CSP = ["form-action 'none'",].join('; ')-// The wrapper that frames the design. Its data: frame inherits this policy on top of+// The wrapper that frames the design. Its blob: frame inherits this policy on top of// the embedded one, so anything the design may load must be allowed here too.export const DESIGN_FRAME_CSP = ["default-src 'none'",- 'frame-src data:',+ 'frame-src blob:',"script-src 'unsafe-inline'","style-src 'unsafe-inline'",'img-src data:',@@ -51,6 +51,68 @@ export const DESIGN_FRAME_CSP = ["frame-ancestors 'self'",].join('; ')+// The page artifact-frame serves under DESIGN_FRAME_CSP: it frames the prepared design+// and relays its heartbeat, paint and link messages to the app.+export function designFrameHtml(preparedArtifact: string, readyToken: string): string {+ // The design reaches its frame as a blob: URL the wrapper makes from this string,+ // not as a data: URL: browsers refuse data: URLs past 2 MiB, which embedded images+ // reach. A blob: document is local like data:, so it inherits this wrapper's CSP and+ // resolves no relative URLs against fx0. "<" is escaped so the string cannot end the script.+ const serializedArtifact = JSON.stringify(preparedArtifact).replace(/</g, '\\u003c')+ const serializedReadyToken = JSON.stringify(readyToken)+ // The wrapper paints nothing of its own, so a design that has not painted yet+ // shows the app's background. Sharing the app's color scheme keeps it+ // transparent; a design that sets no background still gets the browser's white.+ return `<!doctype html>+<html>+<head>+<meta charset="utf-8">+<meta name="viewport" content="width=device-width,initial-scale=1">+<title>Design artifact preview</title>+<style>:root{color-scheme:light dark}html,body,#frame-root,iframe{width:100%;height:100%;margin:0;border:0;overflow:hidden}</style>+</head>+<body>+<div id="frame-root"></div>+<script>+const artifact=URL.createObjectURL(new Blob([${serializedArtifact}],{type:'text/html;charset=utf-8'}));+const token=${serializedReadyToken};+const frame=document.createElement('iframe');+frame.title='Interactive design artifact';+frame.setAttribute('sandbox','allow-scripts');+frame.referrerPolicy='no-referrer';+frame.setAttribute('credentialless','');+let readySignals=0;+let consumedSignals=0;+let lastHeartbeat=Date.now();+const restore=()=>{lastHeartbeat=Date.now();frame.src=artifact;};+addEventListener('message',event=>{+ if(event.source!==frame.contentWindow||event.data?.token!==token)return;+ lastHeartbeat=Date.now();+ if(event.data.type==='ready')readySignals++;+ // The design paints in its own process, after this wrapper's load event; the app+ // swaps a new revision on screen only once it has.+ if(event.data.type==='painted')parent.postMessage({type:'fx0-artifact-painted'},location.origin);+ // The design can only ask to open a link: its scripts can read the token and send+ // this at any time. The app shows the address and opens it on the user's own click+ // there. Recent user activation only filters out asks nobody prompted; it is not proof.+ if(event.data.type==='open'&&typeof event.data.url==='string'&&navigator.userActivation?.isActive!==false){+ parent.postMessage({type:'fx0-artifact-open',url:event.data.url},location.origin);+ }+});+frame.addEventListener('load',()=>setTimeout(()=>{+ if(readySignals>consumedSignals){consumedSignals=readySignals;return;}+ restore();+},100));+setInterval(()=>{+ if(document.visibilityState==='visible'&&Date.now()-lastHeartbeat>3000)restore();+},500);+frame.src=artifact;+document.querySelector('#frame-root').append(frame);+</script>+</body>+</html>`+}+function designArtifactBootstrap(readyToken?: string): string {// The sandbox grants no popups, so a clicked https link asks the app to open it// (see app/api/artifact-frame): the app shows the address and opens it only on thediff --git a/tests/design-artifact.browser.mjs b/tests/design-artifact.browser.mjsindex 616f250..74582d9 100644--- a/tests/design-artifact.browser.mjs+++ b/tests/design-artifact.browser.mjs@@ -1,13 +1,15 @@import assert from 'node:assert/strict'import test from 'node:test'import { execFile } from 'node:child_process'+import http from 'node:http'import { existsSync } from 'node:fs'import { mkdtemp, rm, writeFile } from 'node:fs/promises'import { tmpdir } from 'node:os'import { join } from 'node:path'import { pathToFileURL } from 'node:url'import { promisify } from 'node:util'-import { DESIGN_ARTIFACT_EMBEDDED_CSP, DESIGN_FRAME_CSP, prepareDesignArtifactHtml, validateDesignArtifactHtml } from '../lib/design-artifact.ts'+import { embedRemoteImages } from '../lib/remote-image.ts'+import { DESIGN_ARTIFACT_EMBEDDED_CSP, DESIGN_FRAME_CSP, designFrameHtml, prepareDesignArtifactHtml, validateDesignArtifactHtml } from '../lib/design-artifact.ts'// Opt-in: runs real Chrome headless on this machine. Set CHROME_PATH for another browser binary.const CHROME = process.env.CHROME_PATH ?? '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'@@ -93,15 +95,16 @@ test('in Chrome, every published or stored design keeps its CSP in <head> and ca}})-// The wrapper's own policy reaches the design's data: frame, so a design is rendered-// here under both policies, as in production. An embedded image must load; a remote-// one must still be refused by the policy, not merely fail to connect.+// The page artifact-frame serves, with its policy as a real response header, framing a+// published design the way production does. The design reports to the top page (the+// wrapper relays only its own messages). An embedded image must load and a remote one+// must be refused by the policy; a design past 2 MiB, which a data: URL cannot carry,+// must still render and report painted.const PIXEL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='-const imageDesign = `<!doctype html><html><head><title>design</title></head><body>-<img id="embedded" alt="" src="data:image/png;base64,${PIXEL}">+const reporting = (name, body) => `<!doctype html><html><head><title>design</title></head><body>${body}<script>const results = {}-const report = (name, value) => { results[name] ??= value; parent.postMessage(results, '*') }+const report = (key, value) => { results[key] ??= value; top.postMessage({ design: ${JSON.stringify(name)}, results }, '*') }addEventListener('securitypolicyviolation', (event) => {if (event.blockedURI.startsWith('data:')) report('embedded', 'blocked')if (event.blockedURI.startsWith('http:')) report('remote', 'blocked')@@ -113,28 +116,61 @@ remote.onload = () => report('remote', 'loaded')remote.src = 'http://127.0.0.1:9/pixel.png'</script></body></html>`-test('in Chrome, a published design shows embedded images inside the wrapper and still refuses remote ones', async () => {+test('in Chrome, the real wrapper shows embedded images, refuses remote ones, and frames designs past 2 MiB', async () => {assert.ok(existsSync(CHROME), `Chrome not found at ${CHROME}; set CHROME_PATH`)- const dir = await mkdtemp(join(tmpdir(), 'fx0-design-frame-csp-'))+ // Large: a stored design naming three remote images, each embedded at about 1.1 MiB,+ // as artifact-frame would serve it.+ const big = `data:image/png;base64,${'A'.repeat(1.1 * 1024 * 1024 * 4 / 3 - (1.1 * 1024 * 1024 * 4 / 3) % 4)}`+ const stored = reporting('large', `<img id="embedded" alt="" src="data:image/png;base64,${PIXEL}">` ++ [1, 2, 3].map((n) => `<img alt="" src="https://example.com/big${n}.png">`).join(''))+ const large = await embedRemoteImages(validateDesignArtifactHtml(stored).html, async () => big)+ assert.equal(large.html.split(big).length - 1, 3)+ const prepared = {+ small: published('small', reporting('small', `<img id="embedded" alt="" src="data:image/png;base64,${PIXEL}">`)),+ large: prepareDesignArtifactHtml(large.html, DESIGN_ARTIFACT_EMBEDDED_CSP, 'browser-test'),+ }+ assert.ok(prepared.large.length > 3 * 1024 * 1024, 'past what a data: URL can carry')+ const server = http.createServer((request, response) => {+ const url = new URL(request.url, 'http://test')+ const design = prepared[url.searchParams.get('design')]+ if (url.pathname === '/frame' && design) {+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'content-security-policy': DESIGN_FRAME_CSP })+ response.end(designFrameHtml(design, 'browser-test'))+ } else if (url.pathname === '/hold') {+ // Holds the app page's load, so Chrome dumps it only after the designs painted.+ setTimeout(() => response.writeHead(404).end(), 3000)+ } else if (url.pathname === '/') {+ response.writeHead(200, { 'content-type': 'text/html' }).end(`<!doctype html><html><head><title>app</title></head><body><pre id="out">{}</pre><script>+const out = { painted: [] }+addEventListener('message', ({ data, source }) => {+ if (data?.design) out[data.design] = data.results+ if (data?.type === 'fx0-artifact-painted') out.painted.push([...document.querySelectorAll('iframe')].findIndex((frame) => frame.contentWindow === source))+ document.getElementById('out').textContent = JSON.stringify(out)+})+for (const name of ['small', 'large']) {+ const frame = document.createElement('iframe')+ frame.src = '/frame?design=' + name+ document.body.append(frame)+}+</script><img alt="" src="/hold"></body></html>`)+ } else {+ response.writeHead(404).end()+ }+ })+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))+ const dir = await mkdtemp(join(tmpdir(), 'fx0-design-frame-'))try {- const url = `data:text/html;base64,${Buffer.from(published('image', imageDesign)).toString('base64')}`- const frameCsp = DESIGN_FRAME_CSP.replace(/; frame-ancestors[^;]*/, '')- const file = join(dir, 'page.html')- await writeFile(file, `<!doctype html><html><head><meta http-equiv="Content-Security-Policy" content="${frameCsp}"><title>frame CSP</title></head><body><pre id="out">{}</pre><script>-addEventListener('message', ({ data }) => { if (!data.token) document.getElementById('out').textContent = JSON.stringify(data) })-const frame = document.createElement('iframe')-frame.setAttribute('sandbox', 'allow-scripts')-frame.setAttribute('credentialless', '')-frame.src = ${JSON.stringify(url)}-document.body.append(frame)-</script></body></html>`)const { stdout } = await promisify(execFile)(CHROME, ['--headless=new', '--disable-gpu', '--no-first-run', '--no-default-browser-check',- `--user-data-dir=${join(dir, 'profile')}`, '--virtual-time-budget=5000', '--dump-dom', pathToFileURL(file).href,+ `--user-data-dir=${join(dir, 'profile')}`, '--dump-dom', `http://127.0.0.1:${server.address().port}/`,], { timeout: 60_000, maxBuffer: 16 * 1024 * 1024 })- const results = JSON.parse(stdout.match(/<pre id="out">([^<]*)<\/pre>/)[1])- assert.deepEqual(results, { embedded: 'loaded', remote: 'blocked' })+ const out = JSON.parse(stdout.match(/<pre id="out">([^<]*)<\/pre>/)[1])+ assert.deepEqual(out.small, { embedded: 'loaded', remote: 'blocked' })+ assert.deepEqual(out.large, { embedded: 'loaded', remote: 'blocked' })+ assert.deepEqual([...new Set(out.painted)].sort(), [0, 1], 'both designs report painted through the wrapper')} finally {+ server.closeAllConnections()+ server.close()await rm(dir, { recursive: true, force: true })}})diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjsindex e973313..31234d7 100644--- a/tests/route-security.test.mjs+++ b/tests/route-security.test.mjs@@ -470,7 +470,12 @@ test('the artifact preview permits iframe origins but preserves opaque credentiaassert.match(html, /navigator\.userActivation\?\.isActive!==false/)assert.doesNotMatch(html, /allow-popups/)assert.match(html, /setAttribute\('credentialless',''\)/)- assert.match(html, /data:text\/html;base64/)+ // A blob: frame, not a data: URL, which browsers refuse past 2 MiB. The design string+ // cannot close the wrapper's script: its "<" are escaped, so only one </script> remains.+ assert.match(html, /URL\.createObjectURL\(new Blob\(\[/)+ assert.doesNotMatch(html, /data:text\/html/)+ assert.equal(html.split('</script>').length - 1, 1)+ assert.match(frame.headers.get('content-security-policy'), /frame-src blob:;/)assert.doesNotMatch(html, /allow-same-origin|user-token|secret-refresh/)assert.match(frame.headers.get('content-security-policy'), /frame-ancestors 'self'/)})