From 2cf56dc2d7b5e3e39303e12d611bff10a496c943 Mon Sep 17 00:00:00 2001 From: Florian BRUNIAUX Date: Thu, 15 Oct 2026 13:22:57 +0200 Subject: [PATCH 1/3] Inline the widget bridge for sandboxed WebKit frames Affected WebKit versions refuse the separate bridge script in an opaque widget frame. Embed the trusted local bridge in served widget HTML while retaining the iframe sandbox and CSP, and test its handshake. Co-Authored-By: Claude --- CHANGELOG.md | 2 ++ server/src/widgets/routes.ts | 13 ++++++++----- server/test/app.test.ts | 23 +++++++++++++++++++++-- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f68e57f8..f7003597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Anything that changes what the dashboard, the admin or a widget can do gets a li ## Unreleased +- Start sandboxed widgets on WebKit versions that refuse a bridge script request from an opaque frame. + ### Changed - **Clock in the compact bar.** Show an accent-coloured clock icon beside the time, matching the weather icon. - **Homey is a declared connection now.** The Homey connection type built into Fremkit is gone: diff --git a/server/src/widgets/routes.ts b/server/src/widgets/routes.ts index 82d3efd5..e9bc42b9 100644 --- a/server/src/widgets/routes.ts +++ b/server/src/widgets/routes.ts @@ -11,7 +11,6 @@ import { SDK_VERSION } from '../bridge/sdk.js' import { tr } from '../i18n.js' const BRIDGE_PATH = fileURLToPath(new URL('../bridge/fremkit.js', import.meta.url)) -const BRIDGE_TAG = '' /** * What a widget may do, said in a header rather than left to the iframe. @@ -43,13 +42,17 @@ export const WIDGET_CSP = [ "base-uri 'none'", ].join('; ') -export function injectBridge(html: string): string { +export function injectBridge(html: string, bridge: string): string { + // Inline the trusted bridge: affected WebKit versions refuse a self-origin script inside + // an opaque sandbox. Keep the sandbox and CSP intact, and escape HTML script end tags. + const tag = '' + const i = html.search(/]*>/i) if (i >= 0) { const end = html.indexOf('>', i) + 1 - return html.slice(0, end) + BRIDGE_TAG + html.slice(end) + return html.slice(0, end) + tag + html.slice(end) } - return BRIDGE_TAG + html + return tag + html } export async function widgetRoutes(app: FastifyInstance, opts: { catalog: WidgetCatalog; store: ConfigStore }): Promise { @@ -119,7 +122,7 @@ export async function widgetRoutes(app: FastifyInstance, opts: { catalog: Widget return reply .type('text/html; charset=utf-8') .header('cache-control', 'no-store') - .send(injectBridge(html)) + .send(injectBridge(html, bridge)) } // A widget folder has no business serving its own dotfiles, whatever an author drops in it. // `folder` is the widget's own, so `root` is it: @fastify/static still refuses a path that diff --git a/server/test/app.test.ts b/server/test/app.test.ts index 4c407d91..16500c9f 100644 --- a/server/test/app.test.ts +++ b/server/test/app.test.ts @@ -158,9 +158,28 @@ describe('widget routes', () => { expect(res.statusCode).toBe(200) expect(res.headers['content-type']).toContain('text/html') expect(res.headers['content-security-policy']).toBe(WIDGET_CSP) - expect(res.body).toContain('') + expect(res.body).toContain('') + expect(html.body).toContain('' +export function injectBridge(html: string): string { + const tag = '' const i = html.search(/]*>/i) if (i >= 0) { @@ -55,8 +61,9 @@ export function injectBridge(html: string, bridge: string): string { return tag + html } -export async function widgetRoutes(app: FastifyInstance, opts: { catalog: WidgetCatalog; store: ConfigStore }): Promise { +export async function widgetRoutes(app: FastifyInstance, opts: { catalog: WidgetCatalog; store: ConfigStore; port?: number; dev?: boolean }): Promise { const bridge = await readFile(BRIDGE_PATH, 'utf8') + const csp = widgetCsp(opts.port, opts.dev) app.get('/fremkit.js', async (_req, reply) => reply.type('application/javascript; charset=utf-8').send(bridge)) @@ -104,7 +111,7 @@ export async function widgetRoutes(app: FastifyInstance, opts: { catalog: Widget const rel = req.params['*'] || 'index.html' // On every answer, not just the entry point: a widget's second HTML file is the same // untrusted code, and on a case-insensitive filesystem so is `Index.html`. - reply.header('content-security-policy', WIDGET_CSP) + reply.header('content-security-policy', csp) // The folder rather than the root: a widget may be a built-in or one the user installed, and // which of the two owns an id is the catalogue's answer, decided once at scan time. const folder = opts.catalog.folderOf(id) @@ -122,7 +129,7 @@ export async function widgetRoutes(app: FastifyInstance, opts: { catalog: Widget return reply .type('text/html; charset=utf-8') .header('cache-control', 'no-store') - .send(injectBridge(html, bridge)) + .send(injectBridge(html)) } // A widget folder has no business serving its own dotfiles, whatever an author drops in it. // `folder` is the widget's own, so `root` is it: @fastify/static still refuses a path that diff --git a/server/test/app.test.ts b/server/test/app.test.ts index 16500c9f..1a27dbd5 100644 --- a/server/test/app.test.ts +++ b/server/test/app.test.ts @@ -3,7 +3,7 @@ import { mkdtemp, mkdir, writeFile, readFile, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { buildApp } from '../src/app.js' -import { WIDGET_CSP } from '../src/widgets/routes.js' +import { WIDGET_CSP, widgetCsp } from '../src/widgets/routes.js' import { BYTE_ROUTES, isByteRoute } from '../src/http/headers.js' import { degradedMessage } from '../src/config/routes.js' import { defaultLocale } from '../src/config/schema.js' @@ -158,27 +158,40 @@ describe('widget routes', () => { expect(res.statusCode).toBe(200) expect(res.headers['content-type']).toContain('text/html') expect(res.headers['content-security-policy']).toBe(WIDGET_CSP) - expect(res.body).toContain('') expect(res.body).toContain('hi') }) - it('boots the widget bridge without a same-origin script request', async () => { + it('allows the bridge and a widget-owned relative script in an opaque frame', async () => { + await writeFile(join(dir, 'widgets', 'clock', 'index.html'), + 'hi') + await writeFile(join(dir, 'widgets', 'clock', 'app.js'), 'window.widgetLoaded = true') const res = await app.inject({ url: '/widgets/clock/index.html' }) - const script = res.body.match(/') + expect(res.body).toContain('') + expect(res.headers['content-security-policy']).toContain("script-src 'self' http://127.0.0.1:4242") + expect((await app.inject({ url: '/fremkit.js' })).statusCode).toBe(200) + expect((await app.inject({ url: '/widgets/clock/app.js' })).body).toBe('window.widgetLoaded = true') + }) + it('scopes explicit subresource origins to the server port and development proxy', () => { + const csp = widgetCsp(4301, true) + for (const directive of ['script-src', 'style-src', 'img-src', 'font-src', 'media-src']) { + const line = csp.split('; ').find((part) => part.startsWith(directive)) + expect(line).toContain('http://127.0.0.1:4301') + expect(line).toContain('http://localhost:5173') + expect(line).not.toContain('http://127.0.0.1:4242') } - const document = { - querySelector: () => null, - addEventListener: () => {}, - createElement: () => ({ setAttribute: () => {} }), - head: { appendChild: () => {} }, + expect(widgetCsp(4301)).not.toContain(':5173') + }) + it('serves a second checkout with its own widget origin', async () => { + const other = await buildApp({ dataDir: join(dir, 'data'), widgetsDir: join(dir, 'widgets'), port: 4301 }) + try { + const res = await other.inject({ url: '/widgets/clock/index.html', headers: { host: '127.0.0.1:4301' } }) + expect(res.statusCode).toBe(200) + expect(res.headers['content-security-policy']).toBe(widgetCsp(4301)) + expect(res.headers['content-security-policy']).not.toContain(':4242') + } finally { + await other.close() } - new Function('window', 'document', script!)(window, document) - expect(messages).toContainEqual({ type: 'fremkit:hello' }) - expect(window.Fremkit).toBeDefined() }) it('sandboxes a widget however it is reached', async () => { // Opening a widget top-level, or as a second HTML file, or as Index.html on a @@ -236,7 +249,7 @@ describe('widget routes', () => { const html = await app.inject({ url: '/widgets/synology/index.html' }) expect(html.statusCode).toBe(200) expect(html.headers['content-security-policy']).toBe(WIDGET_CSP) - expect(html.body).toContain('') expect((await app.inject({ url: '/widgets/synology/app.js' })).body).toBe('console.log(1)') const dotfile = await app.inject({ url: '/widgets/synology/.env' }) From 13b0269d19935ae50d406a34f828f1e74abedca3 Mon Sep 17 00:00:00 2001 From: Florian BRUNIAUX Date: Thu, 15 Oct 2026 17:21:20 +0200 Subject: [PATCH 3/3] Drop unsupported IPv6 widget CSP source WebKit rejects bracketed IPv6 authorities in this directive, and Fremkit serves widgets on IPv4 loopback. Co-Authored-By: Claude --- server/src/widgets/routes.ts | 2 +- server/test/app.test.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/widgets/routes.ts b/server/src/widgets/routes.ts index 6d8c6aef..06144c8d 100644 --- a/server/src/widgets/routes.ts +++ b/server/src/widgets/routes.ts @@ -30,7 +30,7 @@ const BRIDGE_PATH = fileURLToPath(new URL('../bridge/fremkit.js', import.meta.ur * `Fremkit.fetch`, which asks the host, which asks the proxy, which checks the manifest. */ export function widgetCsp(port = 4242, dev = false): string { - const ownOrigins = [`http://127.0.0.1:${port}`, `http://localhost:${port}`, `http://[::1]:${port}`] + const ownOrigins = [`http://127.0.0.1:${port}`, `http://localhost:${port}`] if (dev) ownOrigins.push('http://127.0.0.1:5173', 'http://localhost:5173') const own = ownOrigins.join(' ') return [ diff --git a/server/test/app.test.ts b/server/test/app.test.ts index 1a27dbd5..bf6199cd 100644 --- a/server/test/app.test.ts +++ b/server/test/app.test.ts @@ -178,6 +178,7 @@ describe('widget routes', () => { const line = csp.split('; ').find((part) => part.startsWith(directive)) expect(line).toContain('http://127.0.0.1:4301') expect(line).toContain('http://localhost:5173') + expect(line).not.toContain('[::1]') expect(line).not.toContain('http://127.0.0.1:4242') } expect(widgetCsp(4301)).not.toContain(':5173')