A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Keep a design's remote images in the browser cache across revisions
Esteban Suarezc86fbd98a14e
diff --git a/app/api/artifact-frame/route.ts b/app/api/artifact-frame/route.tsindex e9244d3..cb7d053 100644--- a/app/api/artifact-frame/route.ts+++ b/app/api/artifact-frame/route.ts@@ -1,6 +1,6 @@-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 { ARTIFACT_IMAGE_PATH, artifactImageSrc } from '../../../lib/artifact-image'+import { DESIGN_ARTIFACT_EMBEDDED_CSP, designFrameCsp, designFrameHtml, prepareDesignArtifactHtml, remoteImagesEnabled } from '../../../lib/design-artifact'+import { markRemoteImages } from '../../../lib/remote-image'import { readDesignArtifactAt } from '../../../lib/journal'import { readGeneration } from '../../../lib/history'import { withSessionRoute } from '../../../lib/route-auth'@@ -25,15 +25,14 @@ export const GET = withSessionRoute(async (request, { id: sessionId, user }) =>return new Response(null, { status: 304, headers: { 'cache-control': 'private, no-cache', etag } })}const readyToken = crypto.randomUUID()- // Remote images are fetched here and embedded as data: URLs, so the design has no- // network while it runs (lib/remote-image.ts). A copy missing an image is not kept.- const embedStarted = performance.now()- const embedded = artifact != null && remoteImagesEnabled()- ? await embedRemoteImages(artifact, cachedRemoteImage)- : { html: artifact, complete: true, images: 0 }- const embedMs = Math.round(performance.now() - embedStarted)- const keep = etag != null && embedded.complete- const prepared = embedded.html == null ? null : prepareDesignArtifactHtml(embedded.html, DESIGN_ARTIFACT_EMBEDDED_CSP, readyToken)+ // Remote images become markers here; the wrapper loads each from /api/artifact-image+ // and embeds it before the design runs, so the design has no network and the+ // browser keeps the images across revisions (lib/remote-image.ts).+ const marked = artifact != null && remoteImagesEnabled()+ ? markRemoteImages(artifact, crypto.randomUUID().replaceAll('-', ''))+ : { html: artifact, images: [] }+ const images = marked.images.map(({ marker, url }) => ({ marker, src: artifactImageSrc(sessionId, url) }))+ const prepared = marked.html == null ? null : prepareDesignArtifactHtml(marked.html, DESIGN_ARTIFACT_EMBEDDED_CSP, readyToken)// Every stored design passed validation, so this only guards against one that// somehow lacks a doctype: it is never shown without its CSP.if (artifact != null && prepared == null) log(sessionId, 'artifact:unrenderable', { revision: past ? revision : live!.revision })@@ -42,17 +41,13 @@ 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 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 })- const body = new ReadableStream({ start(controller) { controller.enqueue(bytes); controller.close() } })- return new Response(body, {+ const html = designFrameHtml(preparedArtifact, readyToken, images)+ return new Response(html, {status: artifact == null ? 404 : 200,headers: {- 'cache-control': keep ? 'private, no-cache' : 'private, no-store',- ...(keep ? { etag } : {}),- 'content-security-policy': DESIGN_FRAME_CSP,+ 'cache-control': etag ? 'private, no-cache' : 'private, no-store',+ ...(etag ? { etag } : {}),+ 'content-security-policy': designFrameCsp(images.length ? `${new URL(request.url).origin}${ARTIFACT_IMAGE_PATH}` : undefined),'content-type': 'text/html; charset=utf-8','permissions-policy':'accelerometer=(), camera=(), clipboard-read=(), clipboard-write=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), midi=(), payment=(), usb=()',diff --git a/app/api/artifact-image/route.ts b/app/api/artifact-image/route.tsnew file mode 100644index 0000000..4296586--- /dev/null+++ b/app/api/artifact-image/route.ts@@ -0,0 +1,31 @@+import { verifyArtifactImage } from '../../../lib/artifact-image'+import { cachedRemoteImage } from '../../../lib/remote-image-cache'+import { withSessionRoute } from '../../../lib/route-auth'+import { log } from '../../../lib/session'++export const maxDuration = 30++// One image of a design, for the wrapper artifact-frame serves (see lib/artifact-image.ts).+// The design itself never reaches this route: its CSP allows no connections and its+// frame is credentialless. The bytes come from the shared cache artifact-frame warms.+export const GET = withSessionRoute(async (request, { id: sessionId }) => {+ const params = new URL(request.url).searchParams+ const url = verifyArtifactImage(sessionId, params.get('u'), params.get('sig'))+ if (!url) return new Response(null, { status: 404, headers: { 'cache-control': 'private, no-store' } })+ let image: string+ try {+ image = await cachedRemoteImage(url)+ } catch (error) {+ log(sessionId, 'artifact-image:failed', { host: new URL(url).host, reason: (error as Error).message.slice(0, 80) })+ return new Response(null, { status: 404, headers: { 'cache-control': 'private, no-store' } })+ }+ const comma = image.indexOf(',')+ return new Response(Buffer.from(image.slice(comma + 1), 'base64'), {+ headers: {+ 'content-type': image.slice('data:'.length, image.indexOf(';')),+ 'cache-control': 'private, max-age=86400',+ 'content-security-policy': "default-src 'none'; sandbox",+ 'x-content-type-options': 'nosniff',+ },+ })+})diff --git a/lib/artifact-image.ts b/lib/artifact-image.tsnew file mode 100644index 0000000..b97853d--- /dev/null+++ b/lib/artifact-image.ts@@ -0,0 +1,32 @@+import { createHmac, timingSafeEqual } from 'node:crypto'+// @ts-expect-error Node strip-types tests require the source extension.+import { remoteImageUrl } from './design-artifact.ts'++// The wrapper loads a design's images from /api/artifact-image. The route serves only the+// chat's owner, and only a URL artifact-frame signed for that chat, so it cannot be used+// to fetch arbitrary addresses. The URL is stable per chat and image, so the browser+// keeps the image across revisions.++export const ARTIFACT_IMAGE_PATH = '/api/artifact-image'++function signature(sessionId: string, url: string): Buffer {+ const secret = process.env.SESSION_SECRET+ if (!secret || secret.length < 32) throw new Error('SESSION_SECRET must contain at least 32 characters')+ const key = createHmac('sha256', secret).update('fx0 artifact-image key v1').digest()+ return createHmac('sha256', key).update(`${sessionId}\n${url}`).digest()+}++/** The same-origin path the wrapper fetches `url` from, for this chat. */+export function artifactImageSrc(sessionId: string, url: string): string {+ const params = new URLSearchParams({ session: sessionId, u: url, sig: signature(sessionId, url).toString('base64url') })+ return `${ARTIFACT_IMAGE_PATH}?${params}`+}++/** The remote image a request may load for this chat, or null. */+export function verifyArtifactImage(sessionId: string, value: string | null, sig: string | null): string | null {+ const url = value == null ? null : remoteImageUrl(value)+ if (!url || url !== value || !sig || !/^[A-Za-z0-9_-]{43}$/.test(sig)) return null+ const given = Buffer.from(sig, 'base64url')+ const expected = signature(sessionId, url)+ return given.length === expected.length && timingSafeEqual(given, expected) ? url : null+}diff --git a/lib/design-artifact.ts b/lib/design-artifact.tsindex 1b72b44..8fff0bb 100644--- a/lib/design-artifact.ts+++ b/lib/design-artifact.ts@@ -37,29 +37,42 @@ export const DESIGN_ARTIFACT_EMBEDDED_CSP = [// 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 blob:',- "script-src 'unsafe-inline'",- "style-src 'unsafe-inline'",- 'img-src data:',- 'font-src data:',- 'media-src data:',- "object-src 'none'",- "base-uri 'none'",- "form-action 'none'",- "frame-ancestors 'self'",-].join('; ')+// `imageRoute` is the absolute /api/artifact-image URL the wrapper itself loads a+// design's images from; the design's own policy still allows it no connection.+export function designFrameCsp(imageRoute?: string): string {+ return [+ "default-src 'none'",+ 'frame-src blob:',+ ...(imageRoute ? [`connect-src ${imageRoute}`] : []),+ "script-src 'unsafe-inline'",+ "style-src 'unsafe-inline'",+ 'img-src data:',+ 'font-src data:',+ 'media-src data:',+ "object-src 'none'",+ "base-uri 'none'",+ "form-action 'none'",+ "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 {+export const DESIGN_FRAME_CSP = designFrameCsp()+// Counted per place an image is written, so a repeated image cannot expand the design past it.+export const MAX_EMBEDDED_IMAGE_BYTES = 4 * 1024 * 1024++// The page artifact-frame serves under designFrameCsp(): it frames the prepared design+// and relays its heartbeat, paint and link messages to the app. Each image marker in the+// design becomes its image, loaded from `src` (cached by the browser across revisions),+// within MAX_EMBEDDED_IMAGE_BYTES counted per occurrence; a missing image is blank. Only+// then does the design start, so it never needs the network.+export function designFrameHtml(preparedArtifact: string, readyToken: string, images: { marker: string; src: 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)+ const serializedImages = JSON.stringify(images).replace(/</g, '\\u003c')// 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.@@ -74,7 +87,9 @@ export function designFrameHtml(preparedArtifact: string, readyToken: string): s<body><div id="frame-root"></div><script>-const artifact=URL.createObjectURL(new Blob([${serializedArtifact}],{type:'text/html;charset=utf-8'}));+const design=${serializedArtifact};+const images=${serializedImages};+let artifact=null;const token=${serializedReadyToken};const frame=document.createElement('iframe');frame.title='Interactive design artifact';@@ -84,7 +99,7 @@ frame.setAttribute('credentialless','');let readySignals=0;let consumedSignals=0;let lastHeartbeat=Date.now();-const restore=()=>{lastHeartbeat=Date.now();frame.src=artifact;};+const restore=()=>{if(!artifact)return;lastHeartbeat=Date.now();frame.src=artifact;};addEventListener('message',event=>{if(event.source!==frame.contentWindow||event.data?.token!==token)return;lastHeartbeat=Date.now();@@ -106,8 +121,19 @@ frame.addEventListener('load',()=>setTimeout(()=>{setInterval(()=>{if(document.visibilityState==='visible'&&Date.now()-lastHeartbeat>3000)restore();},500);-frame.src=artifact;-document.querySelector('#frame-root').append(frame);+const read=blob=>new Promise(done=>{const reader=new FileReader();reader.onload=()=>done(String(reader.result));reader.onerror=()=>done(null);reader.readAsDataURL(blob)});+const load=async src=>{try{const response=await fetch(src,{credentials:'same-origin'});if(!response.ok)return null;const blob=await response.blob();return /^image\\/(png|jpeg|gif|webp|avif)$/.test(blob.type)?await read(blob):null}catch{return null}};+Promise.all(images.map(image=>load(image.src))).then(loaded=>{+ let html=design,total=0;+ images.forEach((image,index)=>{+ const data=loaded[index],size=data?(data.length-data.indexOf(',')-1)*3/4-(data.endsWith('==')?2:data.endsWith('=')?1:0):0;+ html=html.split(image.marker).map((part,at)=>at===0?part:(data&&total+size<=${MAX_EMBEDDED_IMAGE_BYTES}?(total+=size,data):${JSON.stringify(BLANK_IMAGE)})+part).join('');+ });+ artifact=URL.createObjectURL(new Blob([html],{type:'text/html;charset=utf-8'}));+ lastHeartbeat=Date.now();+ frame.src=artifact;+ document.querySelector('#frame-root').append(frame);+});</script></body></html>`@@ -430,8 +456,10 @@ export function rewriteRemoteImages(html: string, embed: (url: string) => string// be a base64 data: image, whose characters need no escaping in either.const EMBEDDED_IMAGE = /^data:image\/(?:png|jpeg|gif|webp|avif);base64,[A-Za-z0-9+/]+={0,2}$/export const BLANK_IMAGE = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'+// A marker the wrapper replaces with its image (see markRemoteImages in remote-image.ts).+const IMAGE_MARKER = /^fx0-image-[0-9a-f]{32}-\d{1,2}$/function embeddedImage(value: string): string {- return EMBEDDED_IMAGE.test(value) ? value : BLANK_IMAGE+ return EMBEDDED_IMAGE.test(value) || IMAGE_MARKER.test(value) ? value : BLANK_IMAGE}function largestCandidate(value: string): string | null {diff --git a/lib/remote-image.ts b/lib/remote-image.tsindex c83fa37..937ab38 100644--- a/lib/remote-image.ts+++ b/lib/remote-image.ts@@ -3,17 +3,16 @@ import { BLANK_IMAGE, rewriteRemoteImages } from './design-artifact.ts'// @ts-expect-error Node strip-types tests require the source extension.import { sniffImageType } from './image-sniff.ts'// @ts-expect-error Node strip-types tests require the source extension.-import { fetchPublicImage, MAX_IMAGE_FETCH_BYTES } from './public-fetch.ts'+import { fetchPublicImage } from './public-fetch.ts'// A design never touches the network while it runs: a script could choose which-// requests to make and so leak what the viewer types. fx0 fetches the remote images-// the stored markup names, before the design runs, and embeds them as data: URLs.-// What is fetched depends only on the stored design, never on the viewer.+// requests to make and so leak what the viewer types. The images the stored markup+// names are embedded as data: URLs before the design runs: artifact-frame marks them,+// and its wrapper (fx0's own code, not the design) loads them from /api/artifact-image,+// where the browser keeps them across revisions. What is loaded depends only on the+// stored design, never on the viewer.export const MAX_REMOTE_IMAGES = 12-// Counted per place an image is written. With the 512 KiB design limit this bounds a-// prepared preview to about 6 MiB, and the artifact-frame response to about 8 MiB.-export const MAX_REMOTE_IMAGES_TOTAL_BYTES = 4 * 1024 * 1024/** One remote image as a data: URL. Throws when it is unreachable, too large or not an image. */export async function loadRemoteImage(url: string): Promise<string> {@@ -24,35 +23,18 @@ export async function loadRemoteImage(url: string): Promise<string> {}/**- * The design with its remote images embedded, within the count and size budgets; any- * other one becomes a blank image. The size budget counts every place an image is- * written, since each one carries the whole image, so a repeated image cannot expand- * the preview past it. `complete` is false when an image failed to load, so the- * caller does not let a browser keep that copy.+ * The design with each remote image replaced by a marker the wrapper swaps for the+ * image, in document order, at most MAX_REMOTE_IMAGES; any other becomes a blank image.+ * The nonce is fresh per response, so the design's own text cannot name a marker.*/-export async function embedRemoteImages(html: string, load: (url: string) => Promise<string>): Promise<{ html: string; complete: boolean; images: number }> {+export function markRemoteImages(html: string, nonce: string): { html: string; images: { marker: string; url: string }[] } {const urls: string[] = []rewriteRemoteImages(html, (url: string) => {if (!urls.includes(url)) urls.push(url)return BLANK_IMAGE})- if (!urls.length) return { html, complete: true, images: 0 }- const wanted = urls.slice(0, MAX_REMOTE_IMAGES)- const loaded = await Promise.all(wanted.map((url) => load(url).catch(() => null)))- const embedded = new Map<string, string>()- for (const [index, image] of loaded.entries()) {- if (image && imageBytes(image) <= MAX_IMAGE_FETCH_BYTES) embedded.set(wanted[index], image)- }- let total = 0- const rewritten = rewriteRemoteImages(html, (url: string) => {- const image = embedded.get(url)- if (!image || total + imageBytes(image) > MAX_REMOTE_IMAGES_TOTAL_BYTES) return BLANK_IMAGE- total += imageBytes(image)- return image- })- return { html: rewritten, complete: loaded.every(Boolean), images: wanted.length }-}--function imageBytes(image: string): number {- return Math.floor((image.length - image.indexOf(',') - 1) * 3 / 4)+ const images = urls.slice(0, MAX_REMOTE_IMAGES).map((url, index) => ({ marker: `fx0-image-${nonce}-${index}`, url }))+ if (!images.length) return { html, images }+ const markers = new Map(images.map(({ marker, url }) => [url, marker]))+ return { html: rewriteRemoteImages(html, (url: string) => markers.get(url) ?? BLANK_IMAGE), images }}diff --git a/tests/design-artifact.browser.mjs b/tests/design-artifact.browser.mjsindex 6a34254..e603e93 100644--- a/tests/design-artifact.browser.mjs+++ b/tests/design-artifact.browser.mjs@@ -8,8 +8,8 @@ import { tmpdir } from 'node:os'import { join } from 'node:path'import { pathToFileURL } from 'node:url'import { promisify } from 'node:util'-import { embedRemoteImages } from '../lib/remote-image.ts'-import { DESIGN_ARTIFACT_EMBEDDED_CSP, DESIGN_FRAME_CSP, designFrameHtml, prepareDesignArtifactHtml, validateDesignArtifactHtml } from '../lib/design-artifact.ts'+import { markRemoteImages } from '../lib/remote-image.ts'+import { DESIGN_ARTIFACT_EMBEDDED_CSP, DESIGN_FRAME_CSP, designFrameCsp, designFrameHtml, MAX_EMBEDDED_IMAGE_BYTES, 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'@@ -95,67 +95,80 @@ test('in Chrome, every published or stored design keeps its CSP in <head> and ca}})-// 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.+// The page artifact-frame serves, with its policy as a real response header, framing+// published designs the way production does: images marked by the server, loaded by the+// wrapper from the image route, embedded before the design runs. Designs report to the+// top page (the wrapper relays only its own messages).const PIXEL = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=='const reporting = (name, body) => `<!doctype html><html><head><title>design</title></head><body>${body}<script>const results = {}-const report = (key, value) => { results[key] ??= value; top.postMessage({ design: ${JSON.stringify(name)}, 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')+ if (event.blockedURI.startsWith('http:') && event.violatedDirective.startsWith('img-src')) report('remote', 'blocked')+ if (event.violatedDirective.startsWith('connect-src')) report('fetch', 'blocked')})-const embedded = document.getElementById('embedded')-embedded.complete && embedded.naturalWidth ? report('embedded', 'loaded') : embedded.addEventListener('load', () => report('embedded', 'loaded'))const remote = new Image()remote.onload = () => report('remote', 'loaded')remote.src = 'http://127.0.0.1:9/pixel.png'+fetch(location.ancestorOrigins?.[0] ?? 'http://127.0.0.1:9', { mode: 'no-cors' }).then(() => report('fetch', 'reached'), () => {})+addEventListener('load', () => {+ report('embedded', [...document.images].filter((image) => image.src.startsWith('data:image/png')).length)+ const first = document.querySelector('img')+ report('decoded', first.complete && first.naturalWidth > 0)+})</script></body></html>`-test('in Chrome, the real wrapper shows embedded images, refuses remote ones, and frames designs past 2 MiB', async () => {+test('in Chrome, the real wrapper embeds marked images, keeps them across revisions, and frames designs past 2 MiB', async () => {assert.ok(existsSync(CHROME), `Chrome not found at ${CHROME}; set CHROME_PATH`)- // 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'),+ const pixel = Buffer.from(PIXEL, 'base64')+ const big = Buffer.alloc(Math.floor(1.1 * 1024 * 1024), 0x41)+ const repeated = Buffer.alloc(1024 * 1024, 0x41)+ const bodies = { pixel, big1: big, big2: big, big3: big, repeated }+ const hits = {}+ const design = (name, body, images) => {+ const stored = validateDesignArtifactHtml(reporting(name, body + images.map((image) => `<img alt="" src="https://example.com/${image}.png">`).join('')))+ assert.equal(stored.ok, true, name)+ const marked = markRemoteImages(stored.html, '0123456789abcdef0123456789abcdef')+ return {+ html: prepareDesignArtifactHtml(marked.html, DESIGN_ARTIFACT_EMBEDDED_CSP, 'browser-test'),+ images: marked.images.map(({ marker, url }) => ({ marker, src: `/api/artifact-image?u=${new URL(url).pathname.slice(1, -4)}` })),+ }+ }+ const designs = {+ small: design('small', '', ['pixel']),+ again: design('again', '', ['pixel']),+ large: design('large', '', ['pixel', 'big1', 'big2', 'big3']),+ repeat: design('repeat', '<img alt="" src="https://example.com/repeated.png">'.repeat(5), ['repeated']),}- 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 (url.pathname === '/api/artifact-image') {+ const name = url.searchParams.get('u')+ hits[name] = (hits[name] ?? 0) + 1+ if (!bodies[name]) return response.writeHead(404).end()+ return response.writeHead(200, { 'content-type': 'image/png', 'cache-control': 'private, max-age=86400' }).end(bodies[name])+ }+ if (url.pathname === '/frame' && designs[url.searchParams.get('design')]) {+ const { html, images } = designs[url.searchParams.get('design')]+ response.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'content-security-policy': designFrameCsp(`http://127.0.0.1:${server.address().port}/api/artifact-image`) })+ return response.end(designFrameHtml(html, 'browser-test', images))+ }+ if (url.pathname === '/hold') return setTimeout(() => response.writeHead(404).end(), 4000)+ if (url.pathname === '/') {+ return response.writeHead(200, { 'content-type': 'text/html' }).end(`<!doctype html><html><head><title>app</title></head><body><pre id="out">{}</pre><script>+const out = {}+const open = (name) => { const frame = document.createElement('iframe'); frame.src = '/frame?design=' + name; document.body.append(frame) }+addEventListener('message', ({ data }) => {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))+ // A later revision of the same design opens only once the first one has painted.+ if (data?.type === 'fx0-artifact-painted' && !out.reopened && out.small) { out.reopened = true; open('again') }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)-}+for (const name of ['small', 'large', 'repeat']) open(name)</script><img alt="" src="/hold"></body></html>`)- } else {- response.writeHead(404).end()}+ 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-'))@@ -165,9 +178,11 @@ for (const name of ['small', 'large']) {`--user-data-dir=${join(dir, 'profile')}`, '--dump-dom', `http://127.0.0.1:${server.address().port}/`,], { timeout: 60_000, maxBuffer: 16 * 1024 * 1024 })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')+ assert.deepEqual(out.small, { remote: 'blocked', fetch: 'blocked', embedded: 1, decoded: true })+ assert.deepEqual(out.again, { remote: 'blocked', fetch: 'blocked', embedded: 1, decoded: true })+ assert.equal(hits.pixel, 1, 'the next revision embeds the image from the browser cache')+ assert.equal(out.large?.embedded, 4, 'a design past what a data: URL can carry still renders')+ assert.equal(out.repeat?.embedded, Math.floor(MAX_EMBEDDED_IMAGE_BYTES / repeated.length), 'the budget counts every place an image is written')} finally {server.closeAllConnections()server.close()diff --git a/tests/remote-image.test.mjs b/tests/remote-image.test.mjsindex a2b7b7a..45f4c94 100644--- a/tests/remote-image.test.mjs+++ b/tests/remote-image.test.mjs@@ -14,7 +14,8 @@ registerHooks({},})import { BLANK_IMAGE, DESIGN_ARTIFACT_EMBEDDED_CSP, MAX_DESIGN_ARTIFACT_BYTES, remoteImageUrl, rewriteRemoteImages, validateDesignArtifactHtml } from '../lib/design-artifact.ts'-import { embedRemoteImages, loadRemoteImage, MAX_REMOTE_IMAGES, MAX_REMOTE_IMAGES_TOTAL_BYTES } from '../lib/remote-image.ts'+import { loadRemoteImage, markRemoteImages, MAX_REMOTE_IMAGES } from '../lib/remote-image.ts'+import { artifactImageSrc, verifyArtifactImage } from '../lib/artifact-image.ts'import { sniffImageType } from '../lib/image-sniff.ts'import { MAX_IMAGE_FETCH_BYTES } from '../lib/public-fetch.ts'@@ -88,34 +89,44 @@ test('anything but a base64 data: image becomes a blank image, so nothing can br}})-test('embedding fetches each image once, in document order, within the count and size budgets', async () => {+const NONCE = '0123456789abcdef0123456789abcdef'++test('marking names each image once, in document order, up to the count budget', () => {const urls = Array.from({ length: MAX_REMOTE_IMAGES + 2 }, (_, index) => `https://example.com/${index}.png`)- const html = doc(urls.map((url) => `<img src="${url}">`).join('') + `<img src="${urls[0]}">`)- const fetched = []- const result = await embedRemoteImages(html, async (url) => { fetched.push(url); return PNG_URL })- assert.deepEqual(fetched, urls.slice(0, MAX_REMOTE_IMAGES))- assert.equal(result.complete, true)- assert.equal(result.html.split(PNG_URL).length - 1, MAX_REMOTE_IMAGES + 1, 'a repeated image is embedded everywhere it appears')+ const html = doc(urls.map((url) => `<img src="${url}">`).join('') + `<div style="background:url(${urls[0]})"></div>`)+ const result = markRemoteImages(html, NONCE)+ assert.deepEqual(result.images.map(({ url }) => url), urls.slice(0, MAX_REMOTE_IMAGES))+ assert.deepEqual(result.images[0], { marker: `fx0-image-${NONCE}-0`, url: urls[0] })+ assert.equal(result.html.split(`fx0-image-${NONCE}-0`).length - 1, 2, 'every place an image appears carries its marker')assert.equal(result.html.split(BLANK_IMAGE).length - 1, 2, 'images past the count budget are blank')assert.ok(!result.html.includes('https://'))+ const plain = doc('<p>no images</p>')+ assert.deepEqual(markRemoteImages(plain, NONCE), { html: plain, images: [] })+})- const big = `data:image/png;base64,${'A'.repeat(Math.ceil(MAX_IMAGE_FETCH_BYTES * 4 / 3) - 8)}`- const sized = await embedRemoteImages(doc('<img src="https://example.com/1.png"><img src="https://example.com/2.png"><img src="https://example.com/3.png"><img src="https://example.com/4.png">'), async () => big)- assert.equal(sized.html.split(big).length - 1, 3, 'the total budget stops the fourth large image')+test('a design cannot forge a marker: only the ones fx0 writes, with this response\'s nonce, survive', () => {+ const out = rewriteRemoteImages(doc('<img src="https://example.com/a.png">'), () => 'fx0-image-zz-0')+ assert.ok(out.includes(`<img src="${BLANK_IMAGE}">`))+ const typed = doc(`<p>fx0-image-${NONCE}-0</p><img src="https://example.com/a.png">`)+ const marked = markRemoteImages(typed, 'ffffffffffffffffffffffffffffffff')+ assert.ok(marked.html.includes(`<p>fx0-image-${NONCE}-0</p>`), 'text that looks like another nonce is not this response\'s marker')})-test('the size budget counts every place an image is written, so repeating one cannot expand the preview', async () => {- const big = `data:image/png;base64,${'A'.repeat(Math.ceil(1024 * 1024 * 4 / 3))}`- const source = doc('<img src="https://example.com/big.png">'.repeat(20) + '<div style="background:url(https://example.com/big.png)"></div>'.repeat(20))- const result = await embedRemoteImages(source, async () => big)- assert.equal(result.html.split(big).length - 1, 4, 'four 1 MiB copies fill the 4 MiB budget')- assert.ok(result.html.length < source.length + MAX_REMOTE_IMAGES_TOTAL_BYTES * 4 / 3 + 40 * BLANK_IMAGE.length)-- const many = doc('<img src="https://example.com/tiny.png">'.repeat(20_000))- const started = performance.now()- const tiny = await embedRemoteImages(many, async () => PNG_URL)- assert.equal(tiny.html.split(PNG_URL).length - 1, 20_000)- assert.ok(performance.now() - started < 2_000, 'rewriting grows with the output, not with the number of edits')+test('an image URL is signed for one chat: another chat, URL or signature is refused', () => {+ process.env.SESSION_SECRET ??= 'deterministic-artifact-image-test-secret-32+'+ const chat = '11111111-1111-4111-8111-111111111111'+ const url = 'https://images.example.com/a.png?w=100&h=50'+ const src = new URL(artifactImageSrc(chat, url), 'https://fx0.example')+ assert.equal(src.pathname, '/api/artifact-image')+ assert.equal(src.searchParams.get('session'), chat)+ assert.equal(artifactImageSrc(chat, url), artifactImageSrc(chat, url), 'stable, so the browser keeps it across revisions')+ const sig = src.searchParams.get('sig')+ assert.equal(verifyArtifactImage(chat, url, sig), url)+ assert.equal(verifyArtifactImage('22222222-2222-4222-8222-222222222222', url, sig), null)+ assert.equal(verifyArtifactImage(chat, 'https://images.example.com/b.png', sig), null)+ assert.equal(verifyArtifactImage(chat, url, `${sig.slice(0, -1)}${sig.endsWith('A') ? 'B' : 'A'}`), null)+ assert.equal(verifyArtifactImage(chat, url, null), null)+ assert.equal(verifyArtifactImage(chat, 'http://images.example.com/a.png', sig), null)})test('preparing a design stays linear in its size, however its CSS is shaped', () => {@@ -133,15 +144,6 @@ test('preparing a design stays linear in its size, however its CSS is shaped', (}})-test('a failed image is blank and the copy is marked incomplete; a design without remote images is untouched', async () => {- const html = doc('<img src="https://example.com/ok.png"><img src="https://example.com/down.png">')- const result = await embedRemoteImages(html, async (url) => { if (url.endsWith('down.png')) throw new Error('down'); return PNG_URL })- assert.equal(result.complete, false)- assert.ok(result.html.includes(`<img src="${PNG_URL}"><img src="${BLANK_IMAGE}">`))- const plain = doc('<p>no images</p>')- assert.deepEqual(await embedRemoteImages(plain, async () => { throw new Error('never called') }), { html: plain, complete: true, images: 0 })-})-test('embedded designs need no network: the CSP stays data-only', () => {assert.match(DESIGN_ARTIFACT_EMBEDDED_CSP, /img-src data:;/)assert.doesNotMatch(DESIGN_ARTIFACT_EMBEDDED_CSP, /https?:/)diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjsindex 31234d7..313bcd1 100644--- a/tests/route-security.test.mjs+++ b/tests/route-security.test.mjs@@ -237,7 +237,7 @@ function request(path, method = 'GET', id = own, body, origin = process.env.APP_return req}const routes = {}-for (const name of ['answer', 'artifact', 'artifact-frame', 'cancel', 'debug', 'events',+for (const name of ['answer', 'artifact', 'artifact-frame', 'artifact-image', 'cancel', 'debug', 'events','credits', 'model', 'prompt', 'session', 'sessions', 'teams', 'warm']) {routes[name] = await import(`../app/api/${name}/route.ts`)}@@ -480,6 +480,23 @@ test('the artifact preview permits iframe origins but preserves opaque credentiaassert.match(frame.headers.get('content-security-policy'), /frame-ancestors 'self'/)})+test('the wrapper\'s image route serves only URLs artifact-frame signed for this chat, and never caches a refusal', async () => {+ if ((process.env.SESSION_SECRET ?? '').length < 32) process.env.SESSION_SECRET = 'deterministic-route-security-secret-32+'+ const { artifactImageSrc } = await import('../lib/artifact-image.ts')+ const url = 'https://images.example.com/a.png'+ // Another user's chat is refused by ownership, even with a URL validly signed for it.+ reset()+ const theirs = await routes['artifact-image'].GET(new Request(`https://fx0.example${artifactImageSrc(foreign, url)}`, { headers: { origin: process.env.APP_URL } }))+ assert.equal(theirs.status, 404)+ for (const query of ['', `&u=${encodeURIComponent(url)}`, `&u=${encodeURIComponent(url)}&sig=${'A'.repeat(43)}`,+ `&${new URL(artifactImageSrc(foreign, url), 'https://fx0.example').searchParams.toString().replace(/^session=[^&]*&/, '')}`]) {+ reset()+ const response = await routes['artifact-image'].GET(new Request(`https://fx0.example/api/artifact-image?session=${own}${query}`, { headers: { origin: process.env.APP_URL } }))+ assert.equal(response.status, 404, query || 'no URL')+ assert.equal(response.headers.get('cache-control'), 'private, no-store')+ }+})+test('the artifact preview paints no white of its own before the design does', async () => {reset()const html = await (await routes['artifact-frame'].GET(request('artifact-frame', 'GET', own, undefined, 'null'))).text()