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(launch): check and read the prompt file through one descriptor

review3302e580587c
diff --git a/src/core/launch-intent-execute.test.ts b/src/core/launch-intent-execute.test.ts
index 9649ba8b5..96590ebfd 100644
--- a/src/core/launch-intent-execute.test.ts
+++ b/src/core/launch-intent-execute.test.ts
@@ -3,10 +3,15 @@
// agent as ONE argv element on every Windows-native dialect. All of it runs on any OS: the planner
// only builds strings, and the argv is recovered here by the same parsers PowerShell and the
// Microsoft CRT apply.
+import fs from 'fs'
+import os from 'os'
+import path from 'path'
+import { execFileSync } from 'child_process'
import { describe, expect, it, vi } from 'vitest'
import {
executeLaunchIntent,
promptFromFileText,
+ readPromptFileBounded,
resolveWindowsAgentExecutable,
windowsLaunchDialectForShell,
windowsPowerShellPath,
@@ -301,3 +306,39 @@ describe('executeLaunchIntent round trip', () => {
expect(promptFromFileText('\uFEFFa\r\n\tb c\n')).toBe('a b c')
})
})
+
+describe('readPromptFileBounded (one descriptor for the check and the read)', () => {
+ const tmp = (): string => fs.mkdtempSync(path.join(os.tmpdir(), 'nt-prompt-'))
+
+ it('reads a regular file within the cap', async () => {
+ const dir = tmp()
+ const f = path.join(dir, 'p.txt')
+ fs.writeFileSync(f, 'hello "world" $x')
+ await expect(readPromptFileBounded(f, 1024)).resolves.toBe('hello "world" $x')
+ fs.rmSync(dir, { recursive: true, force: true })
+ })
+
+ it('refuses a file over the cap', async () => {
+ const dir = tmp()
+ const f = path.join(dir, 'p.txt')
+ fs.writeFileSync(f, 'x'.repeat(65))
+ await expect(readPromptFileBounded(f, 64)).rejects.toThrow()
+ await expect(readPromptFileBounded(f, 65)).resolves.toBe('x'.repeat(65))
+ fs.rmSync(dir, { recursive: true, force: true })
+ })
+
+ it('refuses a directory and a missing file', async () => {
+ const dir = tmp()
+ await expect(readPromptFileBounded(dir, 64)).rejects.toThrow()
+ await expect(readPromptFileBounded(path.join(dir, 'nope'), 64)).rejects.toThrow()
+ fs.rmSync(dir, { recursive: true, force: true })
+ })
+
+ it.skipIf(process.platform === 'win32')('refuses a FIFO without hanging on the open (POSIX only: mkfifo)', async () => {
+ const dir = tmp()
+ const f = path.join(dir, 'fifo')
+ execFileSync('mkfifo', [f])
+ await expect(readPromptFileBounded(f, 64)).rejects.toThrow()
+ fs.rmSync(dir, { recursive: true, force: true })
+ })
+})
diff --git a/src/core/launch-intent-execute.ts b/src/core/launch-intent-execute.ts
index 4efa14c63..1084a55fb 100644
--- a/src/core/launch-intent-execute.ts
+++ b/src/core/launch-intent-execute.ts
@@ -260,9 +260,27 @@ export async function executeLaunchIntent(
}
}
-/** Default prompt-file reader: a regular file, size-checked before it is read. */
+/** Default prompt-file reader: a regular file, size-checked before it is read.
+ * The check and the read go through ONE file descriptor (open → fstat → read via the handle), not
+ * a stat-then-read on the path, which a swap in between could defeat (CodeQL js/file-system-race).
+ * The path is the user's own choice, so a symlink is followed as before; O_NONBLOCK keeps a FIFO
+ * from hanging the open, and the fstat then refuses it. At most one byte past the cap is read from
+ * the same handle, so a file that grew after the fstat is still refused rather than slurped. */
export async function readPromptFileBounded(file: string, maxBytes: number): Promise<string> {
- const stat = await fs.promises.stat(file)
- if (!stat.isFile() || stat.size > maxBytes) throw new Error('prompt file is not readable')
- return fs.promises.readFile(file, 'utf8')
+ const fh = await fs.promises.open(file, fs.constants.O_RDONLY | (fs.constants.O_NONBLOCK ?? 0))
+ try {
+ const stat = await fh.stat()
+ if (!stat.isFile() || stat.size > maxBytes) throw new Error('prompt file is not readable')
+ const buf = Buffer.alloc(maxBytes + 1)
+ let total = 0
+ while (total < buf.length) {
+ const { bytesRead } = await fh.read(buf, total, buf.length - total, total)
+ if (bytesRead === 0) break
+ total += bytesRead
+ }
+ if (total > maxBytes) throw new Error('prompt file is not readable')
+ return buf.subarray(0, total).toString('utf8')
+ } finally {
+ await fh.close().catch(() => {})
+ }
}