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
security(remote): R7 client-mutation allowlist + R5 wss-only pairing offers
enes831944b9c399
diff --git a/src/main/remote/canvas-sync.ts b/src/main/remote/canvas-sync.tsindex b0dbf7d83..6c19d0cb8 100644--- a/src/main/remote/canvas-sync.ts+++ b/src/main/remote/canvas-sync.ts@@ -2,7 +2,7 @@// wire-syncable form and apply mutations. No Electron, no sockets — shared by the// host broadcast and the client mirror in later tasks.-import type { CanvasMutation, CanvasNodeState } from '@shared/types'+import type { CanvasMutation, CanvasNodeState, CanvasState } from '@shared/types'export type { CanvasMutation, CanvasState } from '@shared/types'@@ -28,6 +28,50 @@ export function applyMutation(return next}+/**+ * R7 — sanitize a CLIENT-supplied mutation before the host applies it.+ *+ * A client may only (a) remove nodes and (b) update the layout/cosmetic fields of nodes the+ * host already has. Everything else stays host-authoritative, because upserted node state has+ * real authority on the host: a terminal node's `shell`/`cwd` feed the PTY spawn when the host+ * renderer mounts it (an approved client could otherwise spawn a shell in a cwd of its choice —+ * reopening the remote `pty.create` that R1 deliberately blocked), `cwd` also WIDENS the remote+ * fs jail (`rootsFromCanvas`), `ssh`/`sshRemoteTmux` retarget spawns at other hosts, and+ * `filePath`/`url` make the host open arbitrary files/pages. Brand-new node ids are rejected+ * outright — the client UI only ever moves/deletes existing nodes.+ *+ * Returns the safe mutation to apply, or null to drop it entirely.+ */+export function sanitizeClientMutation(+ m: CanvasMutation,+ current: CanvasState | null+): CanvasMutation | null {+ if (m.op === 'remove') return typeof m.id === 'string' && m.id ? m : null+ if (m.op !== 'upsert' || !m.node || typeof m.node !== 'object') return null+ const incoming = m.node+ const existing = current?.nodes.find((n) => n.id === incoming.id)+ if (!existing) return null // clients may not CREATE nodes+ // Light shape checks on the layout fields so a malformed payload can't wedge React Flow.+ const pos = incoming.position+ const size = incoming.size+ if (!pos || !Number.isFinite(pos.x) || !Number.isFinite(pos.y)) return null+ if (!size || !Number.isFinite(size.width) || !Number.isFinite(size.height)) return null+ const node: CanvasNodeState = {+ ...existing, // kind, shell, cwd, ssh, sshRemoteTmux, sshFs, agentId, filePath, url, worktree, …+ position: { x: pos.x, y: pos.y },+ size: { width: size.width, height: size.height },+ title: typeof incoming.title === 'string' ? incoming.title : existing.title,+ color: typeof incoming.color === 'string' ? incoming.color : existing.color,+ group: incoming.group ?? null,+ tags: incoming.tags,+ collapsed: incoming.collapsed,+ // Full-state semantics: an absent parentId means "ungrouped", so take it verbatim.+ parentId: incoming.parentId,+ text: typeof incoming.text === 'string' ? incoming.text : existing.text+ }+ return { op: 'upsert', node }+}+/** Stable JSON stringify (keys sorted) so deep-equality is order-independent. */function stableStringify(value: unknown): string {return JSON.stringify(value, (_key, val) => {diff --git a/src/main/remote/host-service.ts b/src/main/remote/host-service.tsindex 8cbece164..40c3302c2 100644--- a/src/main/remote/host-service.ts+++ b/src/main/remote/host-service.ts@@ -31,6 +31,7 @@ import { getStoredEntitlement, isPremium } from '../license'import { genKeyPair, publicKeyToB64, type KeyPair } from './e2ee'import { OP, type Frame } from './framing'import { encodeOffer } from './pairing'+import { sanitizeClientMutation } from './canvas-sync'import { connectRelay, type RelaySocket, type RpcRequest } from './relay-socket'// Default relay endpoint; `NODETERM_RELAY_URL` overrides it (mirrors license.ts's API_BASE /@@ -398,8 +399,12 @@ export function createHostCanvasSync(if (!mutation || typeof mutation !== 'object' || typeof (mutation as { op?: unknown }).op !== 'string') {return null}- onMutation(mutation)- return mutation+ // R7: the wire mutation is CLIENT input — reduce it to layout/cosmetic changes on nodes+ // the host already has before it reaches the renderer (see sanitizeClientMutation).+ const safe = sanitizeClientMutation(mutation, current)+ if (!safe) return null+ onMutation(safe)+ return safe}}}diff --git a/src/main/remote/pairing.ts b/src/main/remote/pairing.tsindex 1a9e171fc..cfa561254 100644--- a/src/main/remote/pairing.ts+++ b/src/main/remote/pairing.ts@@ -65,6 +65,25 @@ function extractCode(input: string): string | null {return input}+// R5: the client connects to `relayEndpoint` verbatim, so an attacker-crafted offer must not+// be able to point it at a plaintext (or non-WebSocket) endpoint. TLS is required; plaintext+// `ws://` is allowed ONLY to loopback, for the local relay in dev and the e2e tests — a+// loopback endpoint never crosses a network.+function isAllowedRelayEndpoint(endpoint: string): boolean {+ let url: URL+ try {+ url = new URL(endpoint)+ } catch {+ return false+ }+ if (url.protocol === 'wss:') return true+ if (url.protocol === 'ws:') {+ const h = url.hostname+ return h === '127.0.0.1' || h === 'localhost' || h === '::1' || h === '[::1]'+ }+ return false+}+function isPairingOffer(value: unknown): value is PairingOffer {if (typeof value !== 'object' || value === null) {return false@@ -72,7 +91,7 @@ function isPairingOffer(value: unknown): value is PairingOffer {const o = value as Record<string, unknown>return (typeof o.relayEndpoint === 'string' &&- o.relayEndpoint.length > 0 &&+ isAllowedRelayEndpoint(o.relayEndpoint) &&typeof o.pairingToken === 'string' &&o.pairingToken.length > 0 &&typeof o.hostPublicKeyB64 === 'string' &&diff --git a/test/remote/host-canvas-sync.test.ts b/test/remote/host-canvas-sync.test.tsindex 7402a80dc..c1fbfdbf1 100644--- a/test/remote/host-canvas-sync.test.ts+++ b/test/remote/host-canvas-sync.test.ts@@ -60,26 +60,91 @@ describe('createHostCanvasSync', () => {])})- it('routes a canvas:mutate RPC to onMutation and returns the parsed mutation', () => {+ it('routes a canvas:mutate RPC to onMutation for a node the host already has', () => {const { socket } = fakeNotifySocket()const received: CanvasMutation[] = []const sync = createHostCanvasSync(socket, (m) => received.push(m))+ sync.setState(state([node('x')]))- const mutation: CanvasMutation = { op: 'upsert', node: node('x') }- const result = sync.handleRpc({ id: '', method: CANVAS_MUTATE_METHOD, params: mutation })+ const moved = { ...node('x'), position: { x: 100, y: 50 } }+ const result = sync.handleRpc({+ id: '',+ method: CANVAS_MUTATE_METHOD,+ params: { op: 'upsert', node: moved } satisfies CanvasMutation+ })- expect(result).toEqual(mutation)- expect(received).toEqual([mutation])+ expect(result).not.toBeNull()+ expect(received).toHaveLength(1)+ const applied = received[0] as Extract<CanvasMutation, { op: 'upsert' }>+ expect(applied.node.position).toEqual({ x: 100, y: 50 })+ })++ it('R7: rejects upserts for node ids the host does not have (no client-created nodes)', () => {+ const { socket } = fakeNotifySocket()+ const received: CanvasMutation[] = []+ const sync = createHostCanvasSync(socket, (m) => received.push(m))+ sync.setState(state([node('a')]))++ const injected = { op: 'upsert', node: node('evil') } satisfies CanvasMutation+ expect(sync.handleRpc({ id: '', method: CANVAS_MUTATE_METHOD, params: injected })).toBeNull()+ // And with NO state known yet, everything is rejected (deny-by-default).+ const empty = createHostCanvasSync(socket, (m) => received.push(m))+ expect(+ empty.handleRpc({ id: '', method: CANVAS_MUTATE_METHOD, params: { op: 'upsert', node: node('a') } })+ ).toBeNull()+ expect(received).toHaveLength(0)+ })++ it('R7: strips host-authoritative fields (shell/cwd/ssh/filePath/url/kind) from client upserts', () => {+ const { socket } = fakeNotifySocket()+ const received: CanvasMutation[] = []+ const sync = createHostCanvasSync(socket, (m) => received.push(m))+ const hostNode: CanvasNodeState = { ...node('t'), shell: '/bin/zsh', cwd: '/safe/project' }+ sync.setState(state([hostNode]))++ const malicious = {+ ...node('t'),+ kind: 'terminal' as const,+ shell: '/bin/sh',+ cwd: '/', // would widen the remote fs jail to the whole disk+ ssh: { host: 'attacker.example' } as never,+ filePath: '/Users/victim/.ssh/id_rsa',+ url: 'https://attacker.example',+ position: { x: 5, y: 5 }+ }+ const result = sync.handleRpc({+ id: '',+ method: CANVAS_MUTATE_METHOD,+ params: { op: 'upsert', node: malicious } satisfies CanvasMutation+ })++ expect(result).not.toBeNull()+ const applied = received[0] as Extract<CanvasMutation, { op: 'upsert' }>+ expect(applied.node.shell).toBe('/bin/zsh')+ expect(applied.node.cwd).toBe('/safe/project')+ expect(applied.node.ssh).toBeUndefined()+ expect(applied.node.filePath).toBeUndefined()+ expect(applied.node.url).toBeUndefined()+ expect(applied.node.position).toEqual({ x: 5, y: 5 }) // layout still applies})it('ignores non-canvas RPC methods and malformed mutations (returns null, no callback)', () => {const { socket } = fakeNotifySocket()const received: CanvasMutation[] = []const sync = createHostCanvasSync(socket, (m) => received.push(m))+ sync.setState(state([node('a')]))expect(sync.handleRpc({ id: 'r1', method: 'pty.create', params: {} })).toBeNull()expect(sync.handleRpc({ id: '', method: CANVAS_MUTATE_METHOD, params: null })).toBeNull()expect(sync.handleRpc({ id: '', method: CANVAS_MUTATE_METHOD, params: { foo: 1 } })).toBeNull()+ // Malformed layout payloads on a known node are rejected too.+ expect(+ sync.handleRpc({+ id: '',+ method: CANVAS_MUTATE_METHOD,+ params: { op: 'upsert', node: { ...node('a'), position: { x: 'NaN' } } }+ })+ ).toBeNull()expect(received).toHaveLength(0)})})diff --git a/test/remote/pairing.test.ts b/test/remote/pairing.test.tsindex fb6a69984..c6518f775 100644--- a/test/remote/pairing.test.ts+++ b/test/remote/pairing.test.ts@@ -34,4 +34,24 @@ describe('pairing', () => {const code = Buffer.from(json, 'utf-8').toString('base64url')expect(decodeOffer(`nodeterm://pair?code=${code}`)).toBeNull()})++ // R5: the client connects to relayEndpoint verbatim — a crafted offer must not be able to+ // point it at a plaintext or non-WebSocket endpoint. ws:// is allowed only to loopback.+ it('R5: rejects non-wss relay endpoints (plaintext ws to a real host, http, garbage)', () => {+ const withEndpoint = (relayEndpoint: string) =>+ encodeOffer({ ...sample, relayEndpoint })+ expect(decodeOffer(withEndpoint('ws://attacker.example'))).toBeNull()+ expect(decodeOffer(withEndpoint('ws://192.168.1.10:8080'))).toBeNull()+ expect(decodeOffer(withEndpoint('http://relay.nodeterm.dev'))).toBeNull()+ expect(decodeOffer(withEndpoint('https://relay.nodeterm.dev'))).toBeNull()+ expect(decodeOffer(withEndpoint('not a url'))).toBeNull()+ })++ it('R5: accepts wss:// anywhere and plain ws:// on loopback only (dev/tests)', () => {+ const withEndpoint = (relayEndpoint: string) =>+ decodeOffer(encodeOffer({ ...sample, relayEndpoint }))+ expect(withEndpoint('wss://relay.nodeterm.dev')).not.toBeNull()+ expect(withEndpoint('ws://127.0.0.1:8137')).not.toBeNull()+ expect(withEndpoint('ws://localhost:8137')).not.toBeNull()+ })})