Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(media): symlink jail + agent-web CSP + range clamp/416 + stream error guard

enesf1fcb4ecefdd
diff --git a/src/main/media-protocol.test.ts b/src/main/media-protocol.test.ts
index a3e70415a..d51b4dc36 100644
--- a/src/main/media-protocol.test.ts
+++ b/src/main/media-protocol.test.ts
@@ -23,4 +23,11 @@ describe('resolveMediaPath (path jail)', () => {
const url = mediaUrlFor('/a b/çlip.mp4')
expect(resolveMediaPath(new URL(url).pathname, allow2)).toBe('/a b/çlip.mp4')
})
+
+ it('round-trips a path containing ? through mediaUrlFor', () => {
+ const original = '/projects/app/q?x&y#z.mp4'
+ const allow3 = new Set([original])
+ const url = mediaUrlFor(original)
+ expect(resolveMediaPath(new URL(url).pathname, allow3)).toBe(original)
+ })
})
diff --git a/src/main/media-protocol.ts b/src/main/media-protocol.ts
index 84b8c300d..cec86f5a0 100644
--- a/src/main/media-protocol.ts
+++ b/src/main/media-protocol.ts
@@ -1,7 +1,7 @@
// nt-media:// — a privileged streaming protocol for local media (video) + large images.
// Files are served ONLY if they are on the per-session allowlist (path jail), so the
// renderer/agent can never read an arbitrary local file. Supports HTTP Range so <video> seeks.
-import { createReadStream, statSync, mkdirSync, writeFileSync } from 'fs'
+import { createReadStream, statSync, lstatSync, mkdirSync, writeFileSync } from 'fs'
import { normalize } from 'path'
import { app, protocol } from 'electron'
@@ -12,8 +12,14 @@ const allowed = new Set<string>()
/** Build the nt-media:// URL for an absolute path (path encoded as the URL pathname). */
export function mediaUrlFor(absPath: string): string {
- // Use the path as an opaque, percent-encoded pathname under a fixed host.
- return `${MEDIA_SCHEME}://media${encodeURI(absPath)}`
+ // Encode each path segment individually so reserved chars (?, #, &) round-trip through
+ // the URL pathname (encodeURI leaves them, which would break the pathname match). The
+ // decode side (resolveMediaPath → decodeURIComponent) stays symmetric.
+ const pathname = absPath
+ .split('/')
+ .map((seg) => encodeURIComponent(seg))
+ .join('/')
+ return `${MEDIA_SCHEME}://media${pathname}`
}
/**
@@ -32,20 +38,36 @@ export function resolveMediaPath(requestPath: string, allow: ReadonlySet<string>
return allow.has(norm) ? norm : null
}
-/** Register the path (so the protocol will serve it) and return its nt-media:// URL. */
+/**
+ * Register the path (so the protocol will serve it) and return its nt-media:// URL.
+ * Allowlist registration is renderer-trusted (same trust boundary as the existing
+ * `fs:read-binary` IPC), so this intentionally accepts any absolute path; the serve-time
+ * lexical jail + symlink check in `initMediaProtocol` are the boundary.
+ */
export function allowMediaPath(absPath: string): string {
const norm = normalize(absPath)
allowed.add(norm)
return mediaUrlFor(norm)
}
+/** The per-session directory holding agent-authored HTML (served under a restrictive CSP). */
+function agentWebDir(): string {
+ return `${app.getPath('userData')}/agent-web`
+}
+
+// Restrictive CSP for agent-authored HTML: render + inline scripts/styles/media, but NO
+// network requests and NO fetching other nt-media files (so it can't exfiltrate or read
+// sibling allowlisted files).
+const AGENT_HTML_CSP =
+ "default-src 'none'; img-src nt-media: data:; media-src nt-media:; style-src 'unsafe-inline'; script-src 'unsafe-inline'; font-src data:"
+
let htmlSeq = 0
/** Write raw HTML to a per-session file under userData, allowlist it, return its abs path. */
export function writeAgentHtml(html: string): string {
- const d = `${app.getPath('userData')}/agent-web`
+ const d = agentWebDir()
mkdirSync(d, { recursive: true })
const p = `${d}/${Date.now().toString(36)}-${++htmlSeq}.html`
- writeFileSync(p, html, 'utf8')
+ writeFileSync(p, html, { encoding: 'utf8', mode: 0o600 })
allowMediaPath(p)
return p
}
@@ -66,31 +88,53 @@ export function initMediaProtocol(): void {
const url = new URL(req.url)
const abs = resolveMediaPath(url.pathname, allowed)
if (!abs) return new Response('Not found', { status: 404 })
+ // Symlink jail: reject a final-component symlink so an allowlisted entry can't be
+ // turned into an arbitrary-file read. lstat does NOT follow the final link (it follows
+ // intermediate dir symlinks like macOS /tmp→/private/tmp, which is fine and avoids the
+ // realpath-equality pitfalls with those system dirs).
+ try {
+ if (lstatSync(abs).isSymbolicLink()) return new Response('Not found', { status: 404 })
+ } catch {
+ return new Response('Not found', { status: 404 })
+ }
let size: number
try {
size = statSync(abs).size
} catch {
return new Response('Not found', { status: 404 })
}
+ // Agent-authored HTML gets a restrictive CSP; video/image files get none (unchanged).
+ const isAgentHtml = abs.startsWith(agentWebDir() + '/')
const range = req.headers.get('range')
const m = range && /bytes=(\d*)-(\d*)/.exec(range)
if (m) {
const start = m[1] ? parseInt(m[1], 10) : 0
- const end = m[2] ? parseInt(m[2], 10) : size - 1
+ let end = m[2] ? parseInt(m[2], 10) : size - 1
+ // Unsatisfiable range → 416 (NaN/negative start or start past EOF).
+ if (!Number.isFinite(start) || start < 0 || start >= size) {
+ return new Response('Range Not Satisfiable', {
+ status: 416,
+ headers: { 'Content-Range': `bytes */${size}` }
+ })
+ }
+ end = Math.min(end, size - 1)
const stream = createReadStream(abs, { start, end })
- return new Response(stream as unknown as ReadableStream, {
- status: 206,
- headers: {
- 'Content-Range': `bytes ${start}-${end}/${size}`,
- 'Accept-Ranges': 'bytes',
- 'Content-Length': String(end - start + 1)
- }
- })
+ stream.on('error', () => stream.destroy())
+ const headers: Record<string, string> = {
+ 'Content-Range': `bytes ${start}-${end}/${size}`,
+ 'Accept-Ranges': 'bytes',
+ 'Content-Length': String(end - start + 1)
+ }
+ if (isAgentHtml) headers['Content-Security-Policy'] = AGENT_HTML_CSP
+ return new Response(stream as unknown as ReadableStream, { status: 206, headers })
}
const stream = createReadStream(abs)
- return new Response(stream as unknown as ReadableStream, {
- status: 200,
- headers: { 'Content-Length': String(size), 'Accept-Ranges': 'bytes' }
- })
+ stream.on('error', () => stream.destroy())
+ const headers: Record<string, string> = {
+ 'Content-Length': String(size),
+ 'Accept-Ranges': 'bytes'
+ }
+ if (isAgentHtml) headers['Content-Security-Policy'] = AGENT_HTML_CSP
+ return new Response(stream as unknown as ReadableStream, { status: 200, headers })
})
}