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
fix(context-link): bound the opencode export and refuse an option-like session id
Fabricio Casalif5e218a195e8
diff --git a/src/core/context-link.opencode-export.test.ts b/src/core/context-link.opencode-export.test.tsnew file mode 100644index 000000000..a6aef375c--- /dev/null+++ b/src/core/context-link.opencode-export.test.ts@@ -0,0 +1,58 @@+import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import fs from 'fs'+import os from 'os'+import path from 'path'+import { isSafeOpencodeSessionId, opencodeExportAt } from './context-link'++/** A stand-in `opencode` that runs `script` under this Node: a cmd shim on Windows (the shape npm+ * installs there), an executable sh script elsewhere. */+function writeFakeOpencode(dir: string, script: string): string {+ const js = path.join(dir, 'opencode.js')+ fs.writeFileSync(js, script)+ if (process.platform === 'win32') {+ const cmd = path.join(dir, 'opencode.cmd')+ fs.writeFileSync(cmd, `@echo off\r\n"${process.execPath}" "${js}" %*\r\n`)+ return cmd+ }+ const sh = path.join(dir, 'opencode')+ fs.writeFileSync(sh, `#!/bin/sh\nexec '${process.execPath}' '${js}' "$@"\n`, { mode: 0o755 })+ return sh+}++describe('opencodeExportAt output bound', () => {+ let dir: string++ beforeEach(() => {+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'nt-opencode-export-'))+ })++ afterEach(() => {+ fs.rmSync(dir, { recursive: true, force: true })+ })++ it('returns an export larger than execFile\'s default 1 MiB buffer', async () => {+ const bytes = 3 * 1024 * 1024+ const bin = writeFakeOpencode(dir, `process.stdout.write('x'.repeat(${bytes}))\n`)+ const output = await opencodeExportAt(bin, 'ses_large')+ expect(output?.length).toBe(bytes)+ })+})++describe('isSafeOpencodeSessionId', () => {+ it('accepts the ids agents mint', () => {+ expect(isSafeOpencodeSessionId('ses_6f2a9c1d8e7b4a05')).toBe(true)+ expect(isSafeOpencodeSessionId('01a0b1f6-1990-7a62-9436-ed4e3ba84b0b')).toBe(true)+ })++ it('refuses an id opencode would read as an option', () => {+ expect(isSafeOpencodeSessionId('--help')).toBe(false)+ expect(isSafeOpencodeSessionId('-s')).toBe(false)+ })++ it('refuses empty, spaced, shell-bearing and oversized ids', () => {+ expect(isSafeOpencodeSessionId('')).toBe(false)+ expect(isSafeOpencodeSessionId('ses a')).toBe(false)+ expect(isSafeOpencodeSessionId("ses';rm -rf ~;'")).toBe(false)+ expect(isSafeOpencodeSessionId('a'.repeat(257))).toBe(false)+ })+})diff --git a/src/core/context-link.ts b/src/core/context-link.tsindex fbd9349d9..e0344efc8 100644--- a/src/core/context-link.ts+++ b/src/core/context-link.ts@@ -203,6 +203,21 @@ async function fetchTranscript(node: LinkDocEntry): Promise<string | null> {}}+/** An export names ONE provider session. The id reaches us from a hook payload, so it is re-checked+ * here, where it becomes an argv entry: `directExecutableInvocation` stops it being read as shell+ * syntax, and nothing but this stops a leading `-` being read by opencode as an option. */+const SAFE_OPENCODE_SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,255}$/++export function isSafeOpencodeSessionId(sessionId: string): boolean {+ return SAFE_OPENCODE_SESSION_ID.test(sessionId)+}++// A whole conversation comes back on stdout. execFile's default 1 MiB buffer turns a long session+// into an error, which reads here as "no transcript"; and with no timeout a wedged CLI holds the+// linked agent's read open for good.+const OPENCODE_EXPORT_MAX_BYTES = 32 * 1024 * 1024+const OPENCODE_EXPORT_TIMEOUT_MS = 60_000+export async function opencodeExportAt(bin: string, sessionId: string): Promise<string | null> {const invocation = directExecutableInvocation(bin, ['export', sessionId])if (!invocation) return null@@ -212,7 +227,12 @@ export async function opencodeExportAt(bin: string, sessionId: string): Promise<execFile(invocation.executable,invocation.args,- { ...invocation.options, encoding: 'utf-8' },+ {+ ...invocation.options,+ encoding: 'utf-8',+ maxBuffer: OPENCODE_EXPORT_MAX_BYTES,+ timeout: OPENCODE_EXPORT_TIMEOUT_MS+ },(err, stdout) => resolve(err ? null : stdout))})@@ -222,7 +242,7 @@ export async function opencodeExportAt(bin: string, sessionId: string): Promise<}async function fetchOpencodeExport(node: LinkDocEntry): Promise<string | null> {- if (!node.sessionId) return null+ if (!node.sessionId || !isSafeOpencodeSessionId(node.sessionId)) return nullif (deps.isRemoteNode?.(node.id)) {return deps.runRemoteCommand? await deps.runRemoteCommand(node.id, `opencode export ${shellQuote(node.sessionId)}`)