fix: serve HTML preview assets with browser-compatible MIME types

This commit is contained in:
Siyan Li committed 2026-10-16 16:20:16 +08:00
1 parent c765c3eb49
commit 0bc32a9945
3 files changed
+37 -3

No files matched your search

+1 -1
View File
@@ -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
+11 -2
View File
@@ -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<string, string> = {
'.png': 'image/png',
'.jpg': 'image/jpeg',
@@ -127,9 +127,18 @@ const MEDIA_TYPES: Record<string, string> = {
'.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'
}
+25
View File
@@ -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'), `<link rel="stylesheet" href="style.css"><script src="app.js"></script>`)
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')