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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(project-icons): ProjectIcon model + hostile-input sanitizer + project.json persistence
enes0debe9d4c31b
diff --git a/src/core/workspace-files.test.ts b/src/core/workspace-files.test.tsindex 2b3d8fe40..12ed44451 100644--- a/src/core/workspace-files.test.ts+++ b/src/core/workspace-files.test.ts@@ -550,3 +550,35 @@ describe('framingViewport', () => {expect(framingViewport(many)).toEqual({ x: 75, y: 71, zoom: 1 })})})++describe('project icon: emitted only when valid, sanitized on the hostile load path', () => {+ it('a valid icon round-trips projectToFile -> fileToProject', () => {+ const p = project({ icon: { type: 'lucide', name: 'rocket' } })+ const f = projectToFile(p, 1, 'now')+ expect(f.icon).toEqual({ type: 'lucide', name: 'rocket' })+ const back = fileToProject(f, { id: p.id })+ expect(back.icon).toEqual({ type: 'lucide', name: 'rocket' })+ })++ it('an invalid in-memory icon is never written to the file (no bytes)', () => {+ const p = project({ icon: { type: 'lucide', name: 'not-a-real-icon' } as any })+ const f = projectToFile(p, 1, 'now')+ expect(f.icon).toBeUndefined()+ })++ it('an absent icon adds no bytes to the file', () => {+ const f = projectToFile(project(), 1, 'now')+ expect(f.icon).toBeUndefined()+ })++ it('an oversized icon on disk is dropped on load (fileToProject degrades to no icon)', () => {+ const hugeSrc = `data:image/png;base64,${'A'.repeat(400_000)}`+ const f = { ...projectToFile(project(), 1, 'now'), icon: { type: 'image', src: hugeSrc, source: 'upload' } } as any+ expect(fileToProject(f, { id: 'p1' }).icon).toBeUndefined()+ })++ it('a hand-edited hostile icon shape on disk is dropped on load', () => {+ const f = { ...projectToFile(project(), 1, 'now'), icon: { type: 'lucide', name: 'not-real' } } as any+ expect(fileToProject(f, { id: 'p1' }).icon).toBeUndefined()+ })+})diff --git a/src/core/workspace-files.ts b/src/core/workspace-files.tsindex aaa13aed1..52ebd7417 100644--- a/src/core/workspace-files.ts+++ b/src/core/workspace-files.ts@@ -10,6 +10,7 @@ import {import type { BridgeLink, CanvasNodeState, Project, ProjectKanban, Viewport, Workspace } from '../shared/types'import { projectCapabilityFields, readProjectCapabilities } from '../shared/project-capabilities'import { loadedAgentBrowserPartition } from '../shared/browser-partition'+import { sanitizeProjectIcon, type ProjectIcon } from '../shared/project-icon'/*** Drop a browser node's persisted `partition` unless it is exactly the jar THIS project (its@@ -60,6 +61,9 @@ export interface ProjectFileV1 {id?: stringname: stringcolor: string+ /** Sanitized on the way in (`fileToProject`) and only ever emitted when valid (`projectToFile`) —+ * see `sanitizeProjectIcon`. An off/invalid icon adds no bytes to the committed file. */+ icon?: ProjectIcon/*** NOT a camera any more — a SUGGESTED one, derived from where the canvas's own nodes sit* (`framingViewport`).@@ -219,6 +223,7 @@ export function projectToFile(// (`shell`, `ssh.extraArgs`) never leave this machine in it — they ride the machine-local index// entry instead (`localNodeExec` / `IndexEntryV3.localExec`). See @shared/node-exec.const nodes = stripSharedNodeExec(p.cwd ? toPortableNodes(p.nodes, p.cwd) : p.nodes)+ const icon = sanitizeProjectIcon(p.icon)return {version: 1,rev,@@ -228,6 +233,7 @@ export function projectToFile(color: p.color,viewport: framingViewport(nodes),nodes,+ ...(icon ? { icon } : {}),...(p.bridges ? { bridges: p.bridges } : {}),...(p.ropes ? { ropes: p.ropes } : {}),...(p.defaultPermissionMode ? { defaultPermissionMode: p.defaultPermissionMode } : {}),@@ -282,10 +288,12 @@ export function fileToProject(}): Project {const defaultAccountId = base.defaultAccountId ?? f.defaultAccountId+ const icon = sanitizeProjectIcon(f.icon)return {id: base.id,name: f.name,color: f.color,+ ...(icon ? { icon } : {}),viewport: base.viewport ?? f.viewport ?? framingViewport(f.nodes),// applyLocalNodeExec DROPS whatever the file carried in the exec fields (it is not ours) and// re-attaches only what this machine typed. See @shared/node-exec. `sanitizeBrowserPartitions`diff --git a/src/shared/project-icon.test.ts b/src/shared/project-icon.test.tsnew file mode 100644index 000000000..798b0fff7--- /dev/null+++ b/src/shared/project-icon.test.ts@@ -0,0 +1,106 @@+import { describe, it, expect } from 'vitest'+import {+ sanitizeProjectIcon, dataUrlByteLength, LUCIDE_ICON_IDS, PROJECT_ICON_MAX_BYTES+} from './project-icon'++const png1x1 =+ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII='++describe('sanitizeProjectIcon: kept', () => {+ it('keeps a valid emoji icon', () => {+ expect(sanitizeProjectIcon({ type: 'emoji', emoji: '🚀' })).toEqual({ type: 'emoji', emoji: '🚀' })+ })+ it('keeps a 16-code-unit emoji (boundary)', () => {+ const emoji = 'x'.repeat(16)+ expect(sanitizeProjectIcon({ type: 'emoji', emoji })).toEqual({ type: 'emoji', emoji })+ })+ it('keeps a known lucide id', () => {+ expect(sanitizeProjectIcon({ type: 'lucide', name: 'rocket' })).toEqual({ type: 'lucide', name: 'rocket' })+ })+ it('keeps a valid data: image icon under the byte cap', () => {+ expect(sanitizeProjectIcon({ type: 'image', src: png1x1, source: 'upload' }))+ .toEqual({ type: 'image', src: png1x1, source: 'upload' })+ })+ it('accepts every allowed image MIME + source combo', () => {+ for (const mime of ['image/png', 'image/jpeg', 'image/gif', 'image/webp']) {+ for (const source of ['github', 'upload', 'favicon'] as const) {+ const src = `data:${mime};base64,QQ==`+ expect(sanitizeProjectIcon({ type: 'image', src, source })).toEqual({ type: 'image', src, source })+ }+ }+ })+})++describe('sanitizeProjectIcon: rejected (never throws, degrades to undefined)', () => {+ it('rejects non-object input', () => {+ expect(sanitizeProjectIcon(null)).toBeUndefined()+ expect(sanitizeProjectIcon(undefined)).toBeUndefined()+ expect(sanitizeProjectIcon('rocket')).toBeUndefined()+ expect(sanitizeProjectIcon(42)).toBeUndefined()+ expect(sanitizeProjectIcon([])).toBeUndefined()+ })+ it('rejects an unknown type', () => {+ expect(sanitizeProjectIcon({ type: 'svg', markup: '<svg/>' })).toBeUndefined()+ expect(sanitizeProjectIcon({})).toBeUndefined()+ })+ it('rejects a 17-code-unit emoji', () => {+ expect(sanitizeProjectIcon({ type: 'emoji', emoji: 'x'.repeat(17) })).toBeUndefined()+ })+ it('rejects an empty emoji', () => {+ expect(sanitizeProjectIcon({ type: 'emoji', emoji: '' })).toBeUndefined()+ })+ it('rejects a non-string emoji', () => {+ expect(sanitizeProjectIcon({ type: 'emoji', emoji: 7 })).toBeUndefined()+ })+ it('rejects an unknown lucide id', () => {+ expect(sanitizeProjectIcon({ type: 'lucide', name: 'not-a-real-icon' })).toBeUndefined()+ })+ it('rejects a non-string lucide name', () => {+ expect(sanitizeProjectIcon({ type: 'lucide', name: 123 })).toBeUndefined()+ })+ it('rejects an http(s) src (not a data: URL)', () => {+ expect(sanitizeProjectIcon({+ type: 'image', src: 'https://evil.test/track.png', source: 'upload'+ })).toBeUndefined()+ })+ it('rejects an oversized data: URL', () => {+ const bigB64 = 'A'.repeat(Math.ceil((PROJECT_ICON_MAX_BYTES + 1024) / 3) * 4)+ const src = `data:image/png;base64,${bigB64}`+ expect(dataUrlByteLength(src)).toBeGreaterThan(PROJECT_ICON_MAX_BYTES)+ expect(sanitizeProjectIcon({ type: 'image', src, source: 'upload' })).toBeUndefined()+ })+ it('rejects a wrong/disallowed MIME type', () => {+ expect(sanitizeProjectIcon({+ type: 'image', src: 'data:image/svg+xml;base64,QQ==', source: 'upload'+ })).toBeUndefined()+ expect(sanitizeProjectIcon({+ type: 'image', src: 'data:text/html;base64,QQ==', source: 'upload'+ })).toBeUndefined()+ })+ it('rejects a missing/unknown source', () => {+ expect(sanitizeProjectIcon({ type: 'image', src: png1x1 })).toBeUndefined()+ expect(sanitizeProjectIcon({ type: 'image', src: png1x1, source: 'random' })).toBeUndefined()+ })+})++describe('LUCIDE_ICON_IDS', () => {+ it('is a non-empty curated list with no duplicates', () => {+ expect(LUCIDE_ICON_IDS.length).toBeGreaterThan(0)+ expect(new Set(LUCIDE_ICON_IDS).size).toBe(LUCIDE_ICON_IDS.length)+ })+})++describe('dataUrlByteLength', () => {+ it('is 0 for a non-data URL', () => {+ expect(dataUrlByteLength('https://x.test/a.png')).toBe(0)+ expect(dataUrlByteLength('not a url')).toBe(0)+ })+ it('never throws on garbage input', () => {+ expect(dataUrlByteLength('data:image/png;base64,%%%not-base64%%%')).toBeGreaterThanOrEqual(0)+ expect(dataUrlByteLength('data:text/plain,%zz')).toBe(0)+ })+ it('decodes a known base64 payload exactly', () => {+ // btoa('hello') === 'aGVsbG8=' — 5 bytes.+ expect(dataUrlByteLength('data:text/plain;base64,aGVsbG8=')).toBe(5)+ })+})diff --git a/src/shared/project-icon.ts b/src/shared/project-icon.tsnew file mode 100644index 000000000..d2c22c1c2--- /dev/null+++ b/src/shared/project-icon.ts@@ -0,0 +1,100 @@+/**+ * A project's icon: an emoji, a curated lucide glyph, or a small raster image (GitHub avatar,+ * user upload, or a fetched favicon). Lives on `Project.icon` and rides `.nodeterm/project.json`+ * (git-shared) like `name`/`color`, so `sanitizeProjectIcon` treats every value as HOSTILE INPUT —+ * same stance as `readProjectCapabilities` / `validKanban` in core/workspace-files.ts: a stored+ * value survives only when it passes a strict check, everything else degrades to "no icon" rather+ * than throwing or rendering something unvetted (an oversized/foreign data: URL, an arbitrary+ * icon-font name that doesn't exist, an http(s) URL that would leak a referer on load).+ *+ * Browser-safe: this module is bundled into the renderer, so no node builtins (no `Buffer`).+ */+export type ProjectIcon =+ | { type: 'emoji'; emoji: string }+ | { type: 'lucide'; name: string }+ | { type: 'image'; src: string; source: 'github' | 'upload' | 'favicon' }++/**+ * Curated allowlist of lucide-react export ids (kebab-case) a project icon's `name` may be. Closed+ * set, not "any string lucide ships": an open set would let a hostile project.json name an icon+ * that doesn't exist in whatever lucide version this build carries, or churn every time lucide+ * renames one. Also drives the icon-picker grid (project-icons Task 4) — this list IS the grid.+ */+export const LUCIDE_ICON_IDS = [+ 'folder', 'folder-git', 'code', 'terminal', 'rocket', 'box', 'package', 'database',+ 'server', 'cloud', 'globe', 'star', 'heart', 'flag', 'bookmark', 'zap', 'flame', 'cpu',+ 'layers', 'layout-grid', 'hash', 'braces', 'bug', 'wrench', 'hammer', 'beaker', 'palette',+ 'paintbrush', 'camera', 'image', 'music', 'video', 'book', 'file-text', 'shield', 'key',+ 'lock', 'users', 'bot', 'sparkles'+] as const++/** MIME types an `image` icon's `data:` URL may declare. Closed set — no `image/svg+xml` (script+ * execution risk on render), no video/other types. */+const IMAGE_ICON_MIME_TYPES = ['image/png', 'image/jpeg', 'image/gif', 'image/webp'] as const++/** Decoded byte size cap for an `image` icon — keeps a hostile/careless project.json from+ * embedding an arbitrarily large blob that then ships to every clone and re-renders on every load. */+export const PROJECT_ICON_MAX_BYTES = 262_144 // 256 KB++/**+ * Decoded byte length of a `data:` URL's payload; 0 for anything else (never throws). Only the+ * `;base64` form is sized precisely (arithmetic on the base64 string's length — `floor(len*3/4)`+ * minus padding — NOT `Buffer`, which does not exist in the renderer); a non-base64 (percent-+ * encoded) data URL is decoded and UTF-8-counted by hand for the same reason.+ */+export function dataUrlByteLength(dataUrl: string): number {+ if (typeof dataUrl !== 'string' || !dataUrl.startsWith('data:')) return 0+ const comma = dataUrl.indexOf(',')+ if (comma === -1) return 0+ const header = dataUrl.slice(5, comma)+ const data = dataUrl.slice(comma + 1)+ if (!/;base64$/i.test(header)) {+ let decoded: string+ try {+ decoded = decodeURIComponent(data)+ } catch {+ return 0+ }+ let bytes = 0+ for (const ch of decoded) {+ const code = ch.codePointAt(0) ?? 0+ bytes += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4+ }+ return bytes+ }+ const clean = data.replace(/[^A-Za-z0-9+/=]/g, '')+ if (clean.length === 0) return 0+ const padding = clean.endsWith('==') ? 2 : clean.endsWith('=') ? 1 : 0+ return Math.floor((clean.length * 3) / 4) - padding+}++/**+ * Strict, throw-free normaliser for a value read as a project's `icon` — the boundary every+ * `project.json` (git-shared, hand-editable, auto-adopted) must cross. Unknown/malformed shapes,+ * an oversized or wrong-MIME image, an http(s)/non-`data:` `src`, or an unlisted lucide name all+ * degrade to `undefined` (no icon) rather than throwing or being trusted as-is.+ */+export function sanitizeProjectIcon(v: unknown): ProjectIcon | undefined {+ if (!v || typeof v !== 'object') return undefined+ const o = v as Record<string, unknown>+ if (o.type === 'emoji') {+ return typeof o.emoji === 'string' && o.emoji.length > 0 && o.emoji.length <= 16+ ? { type: 'emoji', emoji: o.emoji }+ : undefined+ }+ if (o.type === 'lucide') {+ return typeof o.name === 'string' &&+ (LUCIDE_ICON_IDS as readonly string[]).includes(o.name)+ ? { type: 'lucide', name: o.name }+ : undefined+ }+ if (o.type === 'image') {+ if (typeof o.src !== 'string' || !o.src.startsWith('data:')) return undefined+ const mime = /^data:([^;,]*)/.exec(o.src)?.[1] ?? ''+ if (!(IMAGE_ICON_MIME_TYPES as readonly string[]).includes(mime)) return undefined+ if (dataUrlByteLength(o.src) > PROJECT_ICON_MAX_BYTES) return undefined+ if (o.source !== 'github' && o.source !== 'upload' && o.source !== 'favicon') return undefined+ return { type: 'image', src: o.src, source: o.source }+ }+ return undefined+}diff --git a/src/shared/types.ts b/src/shared/types.tsindex 9e6057cad..a2ee72c74 100644--- a/src/shared/types.ts+++ b/src/shared/types.ts@@ -12,6 +12,7 @@ import type { ClientId, DinoSnapshot, PeerDiff, PeerIdentity, PeerState } from 'import type { WhisperModelInfo } from './speech'import type { ProjectKanbanGitHub } from './github-issues'import type { CodexAccount } from './codex-account'+import type { ProjectIcon } from './project-icon'import type {ModelDiscoveryResult,ModelGatewayCredentialStatus,@@ -537,6 +538,10 @@ export interface Project {id: stringname: stringcolor: string+ /** Optional icon shown beside `name` (tab, start screen). Git-shared like `name`/`color` — see+ * `sanitizeProjectIcon` (@shared/project-icon) for the hostile-input rules a stored value must+ * pass on load. */+ icon?: ProjectIcon/** Default working directory for new terminals created in this project. */cwd?: string/** When set, this is an SSH project: its terminals run on `server` in `remoteCwd` (remote tmux). */