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(browser): re-validate a persisted partition on the hostile load path

Claude Code2dbc38b0fe00
diff --git a/src/core/workspace-files.test.ts b/src/core/workspace-files.test.ts
index ff33f7237..2b3d8fe40 100644
--- a/src/core/workspace-files.test.ts
+++ b/src/core/workspace-files.test.ts
@@ -64,6 +64,41 @@ describe('projectToFile / fileToProject round-trip', () => {
})
})
+describe('browser partition is re-validated on the hostile load path', () => {
+ // project.json is git-shared, hand-editable and auto-adopted (constraint 10); `data.partition` is
+ // copied verbatim to <webview partition>. A stored partition survives fileToProject ONLY when it
+ // is exactly the jar THIS project (base.id) would mint — anything else drops to un-owned.
+ const browser = (partition?: string): CanvasNodeState =>
+ node({ id: 'browser-1', kind: 'browser', url: 'https://x.test/', ...(partition ? { partition } : {}) })
+
+ it("drops a FOREIGN project's jar — a cloned project.json cannot name persist:...-<victim>", () => {
+ const f = projectToFile(project({ nodes: [browser('persist:nt-agent-browser-victim')] }), 1, 'ts', 'file-id')
+ const back = fileToProject(f, { id: 'p-mine' })
+ // The webview would have received the victim's jar; it now gets none (default session).
+ expect(back.nodes[0].partition).toBeUndefined()
+ })
+
+ it('drops an UNSAFE storage-key string (not persist:...-<isSafeNodeId>)', () => {
+ for (const bad of ['persist:nt-agent-browser-../../x', 'evil', 'persist:nt-agent-browser-a b', '']) {
+ const f = projectToFile(project({ nodes: [browser(bad)] }), 1, 'ts', 'file-id')
+ expect(fileToProject(f, { id: 'p-mine' }).nodes[0].partition).toBeUndefined()
+ }
+ })
+
+ it('KEEPS the jar that THIS project would mint for itself (same-machine reload)', () => {
+ const own = 'persist:nt-agent-browser-p-mine'
+ const f = projectToFile(project({ nodes: [browser(own)] }), 1, 'ts', 'file-id')
+ expect(fileToProject(f, { id: 'p-mine' }).nodes[0].partition).toBe(own)
+ })
+
+ it('leaves a user-opened (partition-less) browser node and non-browser nodes untouched', () => {
+ const f = projectToFile(project({ nodes: [browser(), node({ id: 't1', partition: 'x' } as never)] }), 1, 'ts', 'file-id')
+ const back = fileToProject(f, { id: 'p-mine' })
+ expect(back.nodes[0].partition).toBeUndefined() // user-opened browser: stays absent
+ expect((back.nodes[1] as { partition?: string }).partition).toBe('x') // non-browser: not our concern
+ })
+})
+
describe('per-project capability fields in the shared file', () => {
it('a capability round-trips through projectToFile/fileToProject, and a non-literal-true does not', () => {
const p = project({ agentBrowserControl: true })
diff --git a/src/core/workspace-files.ts b/src/core/workspace-files.ts
index 394eba1e4..1a5abc605 100644
--- a/src/core/workspace-files.ts
+++ b/src/core/workspace-files.ts
@@ -9,6 +9,24 @@ import {
} from '../shared/node-exec'
import type { BridgeLink, CanvasNodeState, Project, ProjectKanban, Viewport, Workspace } from '../shared/types'
import { projectCapabilityFields, readProjectCapabilities } from '../shared/project-capabilities'
+import { loadedAgentBrowserPartition } from '../shared/browser-partition'
+
+/**
+ * Drop a browser node's persisted `partition` unless it is exactly the jar THIS project (its
+ * machine-local id) would mint. `project.json` is hostile input and `partition` is copied straight
+ * to `<webview partition>`, so a foreign/cloned/unsafe stored value is jar forgery; it falls back to
+ * the un-owned default session. Non-browser nodes and un-partitioned browser nodes are untouched.
+ * See `loadedAgentBrowserPartition`. Pure and side-effect free (only clones the nodes it changes).
+ */
+function sanitizeBrowserPartitions(nodes: CanvasNodeState[], projectId: string): CanvasNodeState[] {
+ return nodes.map((n) => {
+ if (n.kind !== 'browser' || n.partition === undefined) return n
+ const safe = loadedAgentBrowserPartition(n.partition, projectId)
+ if (safe === n.partition) return n
+ const { partition: _dropped, ...rest } = n
+ return safe === undefined ? rest : { ...rest, partition: safe }
+ })
+}
export const PROJECT_DIR = '.nodeterm'
export const PROJECT_FILE = 'project.json'
@@ -256,8 +274,15 @@ export function fileToProject(
color: f.color,
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.
- nodes: applyLocalNodeExec(base.cwd ? resolveNodes(f.nodes, base.cwd) : f.nodes, base.localExec),
+ // re-attaches only what this machine typed. See @shared/node-exec. `sanitizeBrowserPartitions`
+ // is the same "the file is hostile input" treatment for a browser node's session jar: a stored
+ // `partition` survives only when it is exactly the one THIS project (base.id, machine-local)
+ // would mint — a foreign/cloned/unsafe one drops to un-owned default session. See
+ // loadedAgentBrowserPartition; without it a cloned project.json forges another project's jar.
+ nodes: sanitizeBrowserPartitions(
+ applyLocalNodeExec(base.cwd ? resolveNodes(f.nodes, base.cwd) : f.nodes, base.localExec),
+ base.id
+ ),
...(f.bridges ? { bridges: f.bridges } : {}),
...(f.ropes ? { ropes: f.ropes } : {}),
...(defaultAccountId ? { defaultAccountId } : {}),
diff --git a/src/shared/browser-partition.test.ts b/src/shared/browser-partition.test.ts
index 30a78d091..e73d320b6 100644
--- a/src/shared/browser-partition.test.ts
+++ b/src/shared/browser-partition.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
-import { agentBrowserPartition } from './browser-partition'
+import { agentBrowserPartition, loadedAgentBrowserPartition } from './browser-partition'
describe('agentBrowserPartition', () => {
it('is per-project and prefixed persist:', () => {
@@ -14,3 +14,25 @@ describe('agentBrowserPartition', () => {
}
})
})
+
+describe('loadedAgentBrowserPartition — the hostile-load gate', () => {
+ it('keeps a partition that is EXACTLY the one this project would mint', () => {
+ expect(loadedAgentBrowserPartition('persist:nt-agent-browser-p1', 'p1')).toBe('persist:nt-agent-browser-p1')
+ })
+ it("drops a FOREIGN project's jar — project.json cannot name persist:...-<victim>", () => {
+ // The whole finding: a cloned file shipping another project's jar name must not reach the webview.
+ expect(loadedAgentBrowserPartition('persist:nt-agent-browser-victim', 'p1')).toBeUndefined()
+ })
+ it('drops an unsafe/oddly-shaped storage-key string', () => {
+ for (const bad of ['persist:nt-agent-browser-../x', 'evil', 'persist:nt-agent-browser-a b', 'persist:other-p1']) {
+ expect(loadedAgentBrowserPartition(bad, 'p1'), bad).toBeUndefined()
+ }
+ })
+ it('a partition-less (user-opened) node stays partition-less', () => {
+ expect(loadedAgentBrowserPartition(undefined, 'p1')).toBeUndefined()
+ })
+ it('an unsafe PROJECT id can never match, so nothing is kept', () => {
+ // agentBrowserPartition('..') is null, so no stored string can equal it.
+ expect(loadedAgentBrowserPartition('persist:nt-agent-browser-..', '..')).toBeUndefined()
+ })
+})
diff --git a/src/shared/browser-partition.ts b/src/shared/browser-partition.ts
index 9f540b89f..05137fa69 100644
--- a/src/shared/browser-partition.ts
+++ b/src/shared/browser-partition.ts
@@ -33,3 +33,30 @@ export function agentBrowserPartition(projectId: string): string | null {
if (!isSafeNodeId(projectId)) return null
return `persist:nt-agent-browser-${projectId}`
}
+
+/**
+ * The partition a PERSISTED browser node is allowed to keep when it is LOADED.
+ *
+ * `agentBrowserPartition` validates the id at CREATE, but `data.partition` is then persisted and
+ * `.nodeterm/project.json` is hostile input (git-shared, hand-editable, auto-adopted — constraint
+ * 10), and the field is copied verbatim to `<webview partition>`. Without a load-path gate a hostile
+ * clone could ship a browser node whose stored `partition` names ANOTHER project's jar
+ * (`persist:nt-agent-browser-<victim>`) or is a non-`isSafeNodeId` storage-key string, and it would
+ * reach Electron unchecked — forging jar identity the per-project isolation exists to prevent.
+ *
+ * The rule: a stored partition survives ONLY when it is EXACTLY the one THIS project would mint for
+ * itself (`agentBrowserPartition(projectId)`). Anything else — a foreign project's id, a different
+ * machine's id from a clone, an unsafe/oddly-shaped string — drops to `undefined`, i.e. partition-
+ * less / default session / un-owned (the probe's user-opened clause). This is the single load gate:
+ * the value is not re-validated after it, so it runs at the adopt boundary (`fileToProject`, which
+ * is where the machine-local `projectId` is authoritative and never the file's own id). Pinned by
+ * `browser-partition.test.ts` and the hostile-project.json case in `workspace-files.test.ts`; a
+ * missing gate lets `persist:nt-agent-browser-victim` reach the webview.
+ */
+export function loadedAgentBrowserPartition(
+ partition: string | undefined,
+ projectId: string
+): string | undefined {
+ if (!partition) return undefined
+ return partition === agentBrowserPartition(projectId) ? partition : undefined
+}