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(hooks): read the pending request through one fd (CodeQL js/file-system-race)

review92a313098bc4
diff --git a/src/core/agents/pending-approvals.test.ts b/src/core/agents/pending-approvals.test.ts
index c822955e7..a225bb0ac 100644
--- a/src/core/agents/pending-approvals.test.ts
+++ b/src/core/agents/pending-approvals.test.ts
@@ -99,6 +99,21 @@ describe('readPendingRequestLocal + localHeldPermissionIo', () => {
fs.writeFileSync(path.join(pendingDir(home), 'big.json'), 'x'.repeat(PENDING_REQUEST_MAX_BYTES + 1))
expect(await readPendingRequestLocal('big', home)).toBeNull()
})
+ it('reads a file exactly at the cap, and refuses a directory at the name', async () => {
+ fs.mkdirSync(pendingDir(home), { recursive: true })
+ const atCap = 'y'.repeat(PENDING_REQUEST_MAX_BYTES)
+ fs.writeFileSync(path.join(pendingDir(home), 'cap.json'), atCap)
+ expect(await readPendingRequestLocal('cap', home)).toBe(atCap)
+ fs.mkdirSync(path.join(pendingDir(home), 'dir.json'))
+ expect(await readPendingRequestLocal('dir', home)).toBeNull()
+ })
+ it.skipIf(process.platform === 'win32')('refuses a symlink planted at the name (O_NOFOLLOW; POSIX only)', async () => {
+ fs.mkdirSync(pendingDir(home), { recursive: true })
+ const target = path.join(home, 'secret.json')
+ fs.writeFileSync(target, JSON.stringify({ hook_event_name: 'PermissionRequest', tool_name: 'Bash', tool_input: {} }))
+ fs.symlinkSync(target, path.join(pendingDir(home), 'link.json'))
+ expect(await readPendingRequestLocal('link', home)).toBeNull()
+ })
it('end to end: a plan answer is built from the file on disk and written beside it', async () => {
fs.mkdirSync(pendingDir(home), { recursive: true })
fs.writeFileSync(
diff --git a/src/core/agents/pending-approvals.ts b/src/core/agents/pending-approvals.ts
index 4da604a17..5d39d564e 100644
--- a/src/core/agents/pending-approvals.ts
+++ b/src/core/agents/pending-approvals.ts
@@ -70,12 +70,29 @@ export async function readPendingRequestLocal(
): Promise<string | null> {
if (!isValidPendingId(pendingId)) return null
const file = path.join(pendingDir(homeDir), `${pendingId}.json`)
+ // ONE file descriptor for the check and the read (no stat-then-read on a path, which a swap in
+ // between could defeat). O_NOFOLLOW refuses a symlink planted at the name; where the platform has
+ // no such flag (Windows) it is 0 and the isFile() check on the opened handle still applies.
+ let fh: fs.promises.FileHandle | undefined
try {
- const st = await fs.promises.stat(file)
+ fh = await fs.promises.open(file, fs.constants.O_RDONLY | (fs.constants.O_NOFOLLOW ?? 0))
+ const st = await fh.stat()
if (!st.isFile() || st.size > PENDING_REQUEST_MAX_BYTES) return null
- return await fs.promises.readFile(file, 'utf8')
+ // Read at most one byte past the cap from the SAME handle: a file that grew after the fstat is
+ // still refused rather than slurped.
+ const buf = Buffer.alloc(PENDING_REQUEST_MAX_BYTES + 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 > PENDING_REQUEST_MAX_BYTES) return null
+ return buf.subarray(0, total).toString('utf8')
} catch {
return null
+ } finally {
+ await fh?.close().catch(() => {})
}
}