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(codex-accounts): gate and read auth.json through one descriptor
reviewac047bbdb0c5
diff --git a/src/core/read-regular-file.test.ts b/src/core/read-regular-file.test.tsnew file mode 100644index 000000000..fedf8291b--- /dev/null+++ b/src/core/read-regular-file.test.ts@@ -0,0 +1,53 @@+import { execFileSync } from 'child_process'+import { mkdtempSync, mkdirSync, rmSync, symlinkSync, writeFileSync } from 'fs'+import os from 'os'+import path from 'path'+import { afterEach, beforeEach, describe, expect, it } from 'vitest'+import { readRegularFileNoFollow } from './read-regular-file'++describe('readRegularFileNoFollow', () => {+ let dir: string+ beforeEach(() => {+ dir = mkdtempSync(path.join(os.tmpdir(), 'nt-regular-'))+ })+ afterEach(() => rmSync(dir, { recursive: true, force: true }))++ it('reads a plain regular file', async () => {+ const f = path.join(dir, 'auth.json')+ writeFileSync(f, '{"a":1}')+ await expect(readRegularFileNoFollow(f)).resolves.toBe('{"a":1}')+ })++ it('reads an empty file as the empty string', async () => {+ const f = path.join(dir, 'empty')+ writeFileSync(f, '')+ await expect(readRegularFileNoFollow(f)).resolves.toBe('')+ })++ it('is null for an absent file and a directory', async () => {+ await expect(readRegularFileNoFollow(path.join(dir, 'nope'))).resolves.toBeNull()+ mkdirSync(path.join(dir, 'd'))+ await expect(readRegularFileNoFollow(path.join(dir, 'd'))).resolves.toBeNull()+ })++ it('is null over the cap, and reads exactly at it', async () => {+ const f = path.join(dir, 'big')+ writeFileSync(f, 'x'.repeat(10))+ await expect(readRegularFileNoFollow(f, 9)).resolves.toBeNull()+ await expect(readRegularFileNoFollow(f, 10)).resolves.toBe('x'.repeat(10))+ })++ it.skipIf(process.platform === 'win32')('refuses a symlink to a real file (POSIX only: symlink needs privilege on Windows)', async () => {+ const real = path.join(dir, 'real')+ writeFileSync(real, 'secret')+ const link = path.join(dir, 'link')+ symlinkSync(real, link)+ await expect(readRegularFileNoFollow(link)).resolves.toBeNull()+ })++ it.skipIf(process.platform === 'win32')('refuses a FIFO without hanging (POSIX only: mkfifo)', async () => {+ const f = path.join(dir, 'fifo')+ execFileSync('mkfifo', [f])+ await expect(readRegularFileNoFollow(f)).resolves.toBeNull()+ })+})diff --git a/src/core/read-regular-file.ts b/src/core/read-regular-file.tsnew file mode 100644index 000000000..5cf851d94--- /dev/null+++ b/src/core/read-regular-file.ts@@ -0,0 +1,48 @@+/**+ * Read a file only if the name holds a plain regular file — never a symlink, directory or FIFO —+ * with the check and the read served by ONE file descriptor (open → fstat → read via the handle).+ * A stat/lstat on the path followed by a second open by name is a check-then-use race (CodeQL+ * js/file-system-race): the name can be swapped between the two.+ *+ * O_NOFOLLOW refuses a symlink at the name and O_NONBLOCK keeps a FIFO from hanging the open; the+ * fstat then judges what we actually hold. Where the platform has no O_NOFOLLOW (Windows) the open+ * follows a link, so the name is lstat'ed AFTER the open and must be a plain file that is the very+ * file we hold (same dev + ino, when the filesystem reports a file id — a zero ino proves nothing+ * and is accepted, like `readIfRegular` in integration-files.ts). It runs on every platform, so+ * POSIX tests cover it too. Same shape as `readAlertSound` in alert-sounds.ts.+ *+ * Resolves `null` for anything that is not a readable regular file within `maxBytes` (absent,+ * a link, a directory, a FIFO, too large). Never throws.+ */+import { promises as fs, constants as fsConstants } from 'fs'++export const READ_REGULAR_FILE_DEFAULT_MAX = 1024 * 1024++export async function readRegularFileNoFollow(+ file: string,+ maxBytes: number = READ_REGULAR_FILE_DEFAULT_MAX+): Promise<string | null> {+ let fh: fs.FileHandle | undefined+ try {+ fh = await fs.open(file, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0) | (fsConstants.O_NONBLOCK ?? 0))+ const st = await fh.stat({ bigint: true })+ if (!st.isFile() || st.size > BigInt(maxBytes)) return null+ const named = await fs.lstat(file, { bigint: true })+ if (!named.isFile() || (st.ino !== 0n && (named.dev !== st.dev || named.ino !== st.ino))) return null+ // At most one byte past the cap from the SAME handle: a file that grew after the fstat is+ // refused rather than slurped.+ 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) return null+ return buf.subarray(0, total).toString('utf8')+ } catch {+ return null+ } finally {+ await fh?.close().catch(() => {})+ }+}diff --git a/src/main/codex-accounts.ts b/src/main/codex-accounts.tsindex ba1f9ef51..49ef97848 100644--- a/src/main/codex-accounts.ts+++ b/src/main/codex-accounts.ts@@ -37,6 +37,7 @@ import {import { readCodexAccountAt, readCodexThreadAt } from '../core/codex-session-name'import { directExecutableInvocation } from '../core/exec-path'import { linkSharedEntry } from '../core/shared-entry-link'+import { readRegularFileNoFollow } from '../core/read-regular-file'import { ensureCodexRelayRoot } from './codex-relay-daemon'import { platform } from '../core/platform'import { findInLoginPath } from '../core/pty-manager'@@ -174,13 +175,11 @@ async function accountIdentity(accountId?: string): Promise<{ email: string | nuasync function authFileIdentity(accountId?: string): Promise<{ email: string | null } | null> {const home = accountId ? localCodexAccountHome(accountId) : systemCodexHome()const file = path.join(home, 'auth.json')- try {- const auth = await fs.lstat(file)- if (!auth.isFile() || auth.isSymbolicLink()) return null- return { email: readCodexAuthEmail(await fs.readFile(file, 'utf8')) }- } catch {- return null- }+ // The gate and the read share ONE descriptor (no lstat-then-read on the name — a swap in between+ // could hand the read a symlink the gate never saw): a link, FIFO, directory or absent file is null.+ const text = await readRegularFileNoFollow(file)+ if (text === null) return null+ return { email: readCodexAuthEmail(text) }}/**