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(claude): inspect settings through the open file descriptor

enes8cc404140dd8
diff --git a/src/core/agents/hooks/settings-file.test.ts b/src/core/agents/hooks/settings-file.test.ts
index 2cc2e7696..feff02a73 100644
--- a/src/core/agents/hooks/settings-file.test.ts
+++ b/src/core/agents/hooks/settings-file.test.ts
@@ -39,10 +39,10 @@ for (const remote of [false, true]) {
const result = JSON.parse(readFileSync(file, 'utf8'))
expect(result).toMatchObject({ outputStyle: original.outputStyle, model: original.model, statusLine: original.statusLine })
expect(result.hooks.Stop[0]).toEqual({ matcher: '*', hooks: [foreign] })
- expect(statSync(file).mode & 0o777).toBe(0o640)
const once = readFileSync(file, 'utf8')
expect(await update()).toBe(false)
expect(readFileSync(file, 'utf8')).toBe(once)
+ expect(statSync(file).mode & 0o777).toBe(0o640)
})
it.each(['', '{broken', 'null', '[]', '42', '{"hooks":null}', '{"hooks":{"Stop":{}}}', '{"hooks":{"Stop":[null]}}', '{"hooks":{"Stop":[{"hooks":[null]}]}}'])('preserves malformed settings: %s', async (raw) => {
@@ -66,6 +66,13 @@ for (const remote of [false, true]) {
expect(JSON.parse(readFileSync(target, 'utf8')).model).toBe('keep')
})
+ it.skipIf(process.platform === 'win32')('refuses a FIFO without waiting for a writer', async () => {
+ const fifo = spawnSync('mkfifo', [file])
+ expect(fifo.status).toBe(0)
+ expect(await update()).toBe(false)
+ expect(lstatSync(file).isFIFO()).toBe(true)
+ })
+
it('leaves dangling symlinks untouched', async () => {
symlinkSync(path.join(dir, 'absent'), file)
expect(await update()).toBe(false)
diff --git a/src/core/agents/hooks/settings-file.ts b/src/core/agents/hooks/settings-file.ts
index 8e164a7ed..691f2c4f5 100644
--- a/src/core/agents/hooks/settings-file.ts
+++ b/src/core/agents/hooks/settings-file.ts
@@ -1,5 +1,5 @@
// User-owned JSON is never repaired by an installer. Only ENOENT means a new config.
-import { lstatSync, readFileSync, mkdirSync, rmdirSync, statSync, writeFileSync, rmSync, chmodSync, realpathSync } from 'fs'
+import { lstatSync, readFileSync, mkdirSync, rmdirSync, statSync, writeFileSync, rmSync, chmodSync, realpathSync, openSync, closeSync, fstatSync, constants } from 'fs'
import path from 'path'
import { renameAtomicSync, tempNameFor } from '../../fs-atomic'
@@ -10,13 +10,20 @@ export function parseSettings(raw: string): Record<string, unknown> {
}
function snapshot(file: string): string | null {
+ let fd: number | undefined
try {
- // Do not replace links (including dangling links), devices, or directories.
- if (!lstatSync(file).isFile()) throw new Error('Settings must be a regular file')
- return readFileSync(file, 'utf8')
+ // Inspect and read the SAME open file, never lstat(path) then reopen a possibly swapped path.
+ // POSIX O_NOFOLLOW rejects a symlink introduced after target resolution. On platforms without
+ // that flag, the target-resolution check before publication remains the backstop. NONBLOCK
+ // lets fstat reject a FIFO without waiting for a writer.
+ fd = openSync(file, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0) | (constants.O_NONBLOCK ?? 0))
+ if (!fstatSync(fd).isFile()) throw new Error('Settings must be a regular file')
+ return readFileSync(fd, 'utf8')
} catch (e) {
if ((e as NodeJS.ErrnoException).code === 'ENOENT') return null
throw e
+ } finally {
+ if (fd !== undefined) closeSync(fd)
}
}