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(core): per-node token files — 0600 in a 0700 dir, tmp+rename, fail-open
Claude Codef82606cc644b
diff --git a/src/core/agents/node-token-files.test.ts b/src/core/agents/node-token-files.test.tsnew file mode 100644index 000000000..32ecb98d0--- /dev/null+++ b/src/core/agents/node-token-files.test.ts@@ -0,0 +1,72 @@+import { describe, it, expect, beforeEach, afterEach } from 'vitest'+import fs from 'node:fs'+import os from 'node:os'+import path from 'node:path'+import { initPlatform, resetPlatformForTests } from '../platform'+import { fakePlatform } from '../platform-fake'+import {+ nodeTokenDir,+ writeNodeTokenFile,+ sweepNodeTokenFile,+ materialisedNodes,+ resetNodeTokenFilesForTests+} from './node-token-files'++let dir = ''+beforeEach(() => {+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'nodeterm-toks-'))+ resetPlatformForTests()+ resetNodeTokenFilesForTests()+ initPlatform(fakePlatform({ userDataDir: dir }))+})+afterEach(() => fs.rmSync(dir, { recursive: true, force: true }))++describe('node token files', () => {+ it('writes 0600 files inside a 0700 dir and records materialisation', () => {+ expect(writeNodeTokenFile('node-1', 'kid.mac')).toBe(true)+ expect(fs.statSync(nodeTokenDir()).mode & 0o777).toBe(0o700)+ const f = path.join(nodeTokenDir(), 'node-1')+ expect(fs.statSync(f).mode & 0o777).toBe(0o600)+ expect(fs.readFileSync(f, 'utf8')).toBe('kid.mac\n')+ expect([...materialisedNodes()]).toEqual(['node-1'])+ })++ it('refuses unsafe node ids and creates NO file (path traversal guard)', () => {+ for (const bad of ['../evil', '..', '.', 'a b', 'a/b']) {+ expect(writeNodeTokenFile(bad, 'kid.mac'), bad).toBe(false)+ }+ const entries = fs.existsSync(nodeTokenDir()) ? fs.readdirSync(nodeTokenDir()) : []+ expect(entries).toEqual([])+ // Prove nothing escaped the token dir into userDataDir.+ expect(fs.existsSync(path.join(dir, 'evil'))).toBe(false)+ expect([...materialisedNodes()]).toEqual([])+ })++ it('refuses an empty token', () => {+ expect(writeNodeTokenFile('node-1', '')).toBe(false)+ })++ it('is idempotent under a racing rewrite (derived value ⇒ identical bytes)', () => {+ for (let i = 0; i < 5; i++) expect(writeNodeTokenFile('node-1', 'kid.mac')).toBe(true)+ expect(fs.readdirSync(nodeTokenDir())).toEqual(['node-1'])+ })++ it('sweeps a deleted node and forgets it', () => {+ writeNodeTokenFile('node-1', 'kid.mac')+ sweepNodeTokenFile('node-1')+ expect(fs.readdirSync(nodeTokenDir())).toEqual([])+ expect([...materialisedNodes()]).toEqual([])+ })++ it('leaves no leftover tmp files behind', () => {+ writeNodeTokenFile('node-1', 'kid.mac')+ expect(fs.readdirSync(nodeTokenDir())).toEqual(['node-1'])+ })++ it('fails OPEN on an unwritable dir — returns false, never throws', () => {+ fs.mkdirSync(nodeTokenDir(), { recursive: true })+ fs.chmodSync(nodeTokenDir(), 0o500)+ expect(() => writeNodeTokenFile('node-2', 'kid.mac')).not.toThrow()+ fs.chmodSync(nodeTokenDir(), 0o700)+ })+})diff --git a/src/core/agents/node-token-files.ts b/src/core/agents/node-token-files.tsnew file mode 100644index 000000000..1d311aba8--- /dev/null+++ b/src/core/agents/node-token-files.ts@@ -0,0 +1,74 @@+import { mkdirSync, chmodSync, writeFileSync, renameSync, rmSync } from 'fs'+import path from 'path'+import { platform } from '../platform'+import { isSafeNodeId } from './node-auth-token'++/**+ * Distribution channel for the per-node capability: a FILE keyed by node id, read by the client at+ * hook time. Env injection (#167's channel) fails four ways here — it lands in world-readable argv+ * (the leak this series closes), it is clobbered by the endpoint-file source, it reaches a session+ * only at tmux CREATION so every running session stays unauthenticated until the node is destroyed,+ * and the phone injects env itself and cannot mint. A file fixes all four.+ *+ * It is exactly as strong as env against a same-uid sibling (nothing makes a token secret from a+ * determined sibling within one uid) and strictly stronger against everyone else.+ */+const materialised = new Set<string>()++export function nodeTokenDir(): string {+ return path.join(platform().userDataDir, 'node-tokens')+}++/**+ * tmp + rename, 0700 dir / 0600 file. Returns false (never throws) on any failure — a missing token+ * is an ordinary state (pre-upgrade session, failed remote write), never an attack, so the whole+ * series must fail OPEN here rather than crash a hook path.+ *+ * The nodeId is attacker-controlled (it comes from a shared `project.json`) AND becomes a path+ * segment, so `isSafeNodeId` — which refuses `.`/`..`/`/`/over-length — gates BEFORE any path join.+ */+export function writeNodeTokenFile(nodeId: string, token: string): boolean {+ if (!isSafeNodeId(nodeId) || !token) return false+ try {+ const dir = nodeTokenDir()+ mkdirSync(dir, { recursive: true, mode: 0o700 })+ chmodSync(dir, 0o700) // an existing dir keeps its old mode otherwise+ const file = path.join(dir, nodeId)+ const tmp = path.join(dir, `.${nodeId}.${process.pid}.${Date.now()}.tmp`)+ try {+ writeFileSync(tmp, `${token}\n`, { encoding: 'utf8', mode: 0o600, flag: 'wx' })+ renameSync(tmp, file)+ chmodSync(file, 0o600)+ } finally {+ try {+ rmSync(tmp, { force: true })+ } catch {+ /* already renamed into place */+ }+ }+ materialised.add(nodeId)+ return true+ } catch (e) {+ console.warn('[node-identity] could not materialise token for', nodeId, e)+ return false+ }+}++export function sweepNodeTokenFile(nodeId: string): void {+ materialised.delete(nodeId)+ if (!isSafeNodeId(nodeId)) return+ try {+ rmSync(path.join(nodeTokenDir(), nodeId), { force: true })+ } catch {+ /* fail open */+ }+}++/** Which nodes this run believes have a readable token. Read by the migration latch later. */+export function materialisedNodes(): ReadonlySet<string> {+ return materialised+}++export function resetNodeTokenFilesForTests(): void {+ materialised.clear()+}