The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(security): record folder trust for a local engine's empty workspace (OH-14)
0xken15ee22cfd9730a
diff --git a/cli/src/backendSocket.spec.ts b/cli/src/backendSocket.spec.tsindex 0379f2010..7b24ed320 100644--- a/cli/src/backendSocket.spec.ts+++ b/cli/src/backendSocket.spec.ts@@ -1,7 +1,8 @@import * as gitPullRequest from './lib/gitPullRequest.js'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { readFileSync } from 'fs'-import { homedir } from 'os'+import { readFileSync, mkdtempSync, writeFileSync, rmSync } from 'fs'+import { homedir, tmpdir } from 'os'+import { join } from 'path'import { fileURLToPath } from 'url'import { BackendSocket, compactRuntimePickerModels, deviceAgentListItem, deviceAgentRow, grokHistoryPage } from './backendSocket.js'import { AuthSessionError, type AuthSessionManager } from './lib/authSession.js'@@ -1211,6 +1212,54 @@ describe('BackendSocket outbound queue', () => {}})+ it.each([+ { name: 'an empty cwd (the desktop just made it)', contents: [] as string[], trusts: true },+ { name: 'a cwd that already has something', contents: ['Makefile'], trusts: false },+ ])('records Claude trust for $name when the daemon sees a plain cwd', async ({ contents, trusts }) => {+ // A plain cwd on the LOCAL machine is the desktop opening a folder it just made empty; evidence is the+ // readdir, never the client's word, so a folder with content (a clone, the person's own repo) stays+ // the engine's question.+ const dir = mkdtempSync(join(tmpdir(), 'harness-trust-'))+ for (const item of contents) writeFileSync(join(dir, item), 'x')+ const socket = new BackendSocket('token')+ const frames: Array<Record<string, unknown>> = []+ socket.registerLocalClient('local:cwd-trust', { sendFrame: (frame) => { frames.push(frame); return true }, sendBinary: () => true })+ vi.mocked(claudeTrust.preTrustClaudeProject).mockClear()+ socket.onCreateAgent = vi.fn(async () => ({ ok: false as const, error: 'TMUX_UNAVAILABLE' }))+ try {+ socket.handleLocalFrame('local:cwd-trust', { type: 'agent_create', payload: { requestId: 'r', creationId: randomUUID(), engine: 'claude', cwd: dir } })+ await vi.waitFor(() => expect(socket.onCreateAgent).toHaveBeenCalled())+ if (trusts) expect(claudeTrust.preTrustClaudeProject).toHaveBeenCalledExactlyOnceWith(dir)+ else expect(claudeTrust.preTrustClaudeProject).not.toHaveBeenCalled()+ } finally {+ await socket.unregisterLocalClient('local:cwd-trust')+ await socket.stop()+ rmSync(dir, { recursive: true, force: true })+ }+ })++ it('does NOT record Claude trust for an empty cwd from a relayed/E2EE frame', async () => {+ // agent_create is not backend-only, so a relay frame is E2EE-unwrapped and reaches the same pre-trust+ // path. The LOCAL gate is what keeps it from recording trust for an arbitrary empty path on this host.+ const dir = mkdtempSync(join(tmpdir(), 'harness-trust-'))+ const socket = new BackendSocket('token')+ const internals = socket as any+ vi.spyOn(internals.e2ee, 'unwrapDown').mockReturnValue({+ type: 'agent_create', payload: { requestId: 'r', creationId: randomUUID(), engine: 'claude', cwd: dir },+ })+ vi.mocked(claudeTrust.preTrustClaudeProject).mockClear()+ socket.onCreateAgent = vi.fn(async () => ({ ok: false as const, error: 'TMUX_UNAVAILABLE' }))+ try {+ await internals.dispatchDown({ type: 'agent_create', payload: { __e2e: { v: 1, k: 'p', n: 1, ct: 'fixture' } } }, 'web-1', 'relay')+ await vi.waitFor(() => expect(socket.onCreateAgent).toHaveBeenCalled())+ // onCreateAgent ran, so the pre-trust path executed — but a relay frame is not local: no trust recorded.+ expect(claudeTrust.preTrustClaudeProject).not.toHaveBeenCalled()+ } finally {+ await socket.stop()+ rmSync(dir, { recursive: true, force: true })+ }+ })+it('checks an unknown creation without spawning and rejects malformed creation ids before launch', async () => {const socket = new BackendSocket('token')const frames: Array<Record<string, unknown>> = []diff --git a/cli/src/backendSocket.ts b/cli/src/backendSocket.tsindex 12d932652..a8f938e48 100644--- a/cli/src/backendSocket.ts+++ b/cli/src/backendSocket.ts@@ -21,7 +21,7 @@ import type { AutonomousDeviceService, AutonomousDeviceFrame } from './lib/autonimport { WebSocket } from 'ws'import { watchSocketLiveness, type LivenessWatch } from './lib/wsLiveness.js'-import { stat, readFile } from 'fs/promises'+import { stat, readFile, readdir } from 'fs/promises'import { isAbsolute, join } from 'path'import { hostname, homedir } from 'os'import { env } from './config/env.js'@@ -2375,6 +2375,22 @@ export class BackendSocket {engineTrust.record(preparedFolder)}} catch (error) { console.warn(`[agent] pre-trust ${preparedFolder} · ${error instanceof Error ? error.message : error}`) }+ } else if (!dsh && local) {+ // A plain cwd on the LOCAL machine is the desktop opening a NEW folder: the desktop made it+ // empty and hands the path over, it has nothing in it to review, so it is trusted the way a+ // `new` project is. The LOCAL gate is the boundary — agent_create is not backend-only, so a+ // relayed/E2EE frame would otherwise name any empty path on this host and get it trusted.+ // A clone / the person's own repo is never empty (lib/claudeTrust.ts), and evidence beats a+ // client's word, so this readdir is the gate. DSH trust is decided in cli.ts where the+ // template count is known; leave that to it.+ try {+ const empty = await readdir(input.cwd).then((names) => names.length === 0, () => false)+ if (empty) {+ const engineTrust = input.engine === 'claude' ? { record: preTrustClaudeProject }+ : input.engine === 'codex' ? { record: preTrustCodexProject } : null+ if (engineTrust) engineTrust.record(input.cwd)+ }+ } catch (error) { console.warn(`[agent] pre-trust ${input.cwd} · ${error instanceof Error ? error.message : error}`) }}const result = await create(preparedFolder ? { ...input, cwd: preparedFolder } : input)if (result.ok) return { state: 'created', agentId: result.session.agentId }