diff --git a/README.md b/README.md index 888535ad..4c6f98a0 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ export function apply(ctx: Context) { - Routes sit behind a Host-header trust fence (same as `/api`); `fs.write` is atomic; git is only invoked via CLI and never sets identity - ⚠️ **Since v0.23.0 the fs routes do no workspace-containment check**: they can read/write any path the host user can reach (OS permissions only) — callers must not treat these routes as fenced -- HTML previews render in an **opaque-origin sandboxed iframe**; the sandbox can be disabled per feature in settings (off by default, with warnings) +- HTML previews render in an **opaque-origin sandboxed iframe** and serve relative CSS, JavaScript, JSON, fonts and WebAssembly with their content types; the sandbox can be disabled per feature in settings (off by default, with warnings) - No git push/pull/fetch; the file tree only watches **expanded** directories; Markdown preview renders the saved file - Read-only formats (spreadsheets / PDF / images / Office) are rendered by DSH's own document preview and follow the host version; the browser view only exists in the desktop profile - Unsaved drafts are confirmed before closing a tab / deleting a file / refreshing; saving after external modification is rejected, never silently overwritten diff --git a/src/index.ts b/src/index.ts index 0e53ea1a..c7c027af 100644 --- a/src/index.ts +++ b/src/index.ts @@ -113,7 +113,7 @@ export interface SidebarFsLevel { /** Services required before mounting: the webserver routes, the session store, the web runtime's trusted hosts, and the tool registry. */ export const inject = ['webServer', 'sessions', 'webRuntime', 'tools'] -/** Content types for the media route, by extension. */ +/** Content types for media files and HTML preview resources, by extension. */ const MEDIA_TYPES: Record = { '.png': 'image/png', '.jpg': 'image/jpeg', @@ -127,9 +127,18 @@ const MEDIA_TYPES: Record = { '.pdf': 'application/pdf', '.html': 'text/html', '.htm': 'text/html', + '.css': 'text/css', + '.js': 'text/javascript', + '.mjs': 'text/javascript', + '.json': 'application/json', + '.woff': 'font/woff', + '.woff2': 'font/woff2', + '.ttf': 'font/ttf', + '.otf': 'font/otf', + '.wasm': 'application/wasm', } -/** Content type served by /sidebar/file (binary-safe fallback for unknowns). */ +/** Content type served by /sidebar/file and /sidebar/html; unknowns use a binary-safe fallback. */ export function mediaTypeForPath(path: string): string { return MEDIA_TYPES[extname(path).toLowerCase()] ?? 'application/octet-stream' } diff --git a/tests/file-stream-route.spec.ts b/tests/file-stream-route.spec.ts index 8afd971e..b69011a1 100644 --- a/tests/file-stream-route.spec.ts +++ b/tests/file-stream-route.spec.ts @@ -216,6 +216,31 @@ describe('/sidebar/file streams without a size cap', () => { }) describe('/sidebar/html streams without a size cap', () => { + it.each([ + ['style.css', 'text/css', 'body { color: red }'], + ['app.js', 'text/javascript', 'document.body.dataset.ready = "yes"'], + ['module.mjs', 'text/javascript', 'export const ready = true'], + ['STYLE.CSS', 'text/css', 'body { color: blue }'], + ['data.json', 'application/json', '{"ready":true}'], + ['font.woff', 'font/woff', 'woff bytes'], + ['font.woff2', 'font/woff2', 'woff2 bytes'], + ['font.ttf', 'font/ttf', 'ttf bytes'], + ['font.otf', 'font/otf', 'otf bytes'], + ['module.wasm', 'application/wasm', 'wasm bytes'], + ])('serves relative %s resources with a browser-compatible content type', async (name, type, content) => { + const ws = workspace('html-assets') + writeFileSync(join(ws, 'index.html'), ``) + writeFileSync(join(ws, name), content) + const { origin } = await serve(ws) + const { encodeHtmlUrl } = await import('../src/html-route.ts') + const page = new URL(encodeHtmlUrl('s', join(ws, 'index.html')), origin) + const response = await fetch(new URL(name, page)) + expect(response.status).toBe(200) + expect(response.headers.get('content-type')).toBe(type) + expect(response.headers.get('x-content-type-options')).toBe('nosniff') + expect(await response.text()).toBe(content) + }) + it('serves a page past the retired mediaLimit with the sandbox CSP', async () => { const ws = workspace('html') const target = join(ws, 'page.html')