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(project-settings): reserved-key filter for project env

enes0cda2ecec002
diff --git a/src/core/claude-accounts-core.ts b/src/core/claude-accounts-core.ts
index 445c7fc42..3f6171e26 100644
--- a/src/core/claude-accounts-core.ts
+++ b/src/core/claude-accounts-core.ts
@@ -176,6 +176,36 @@ export const AUTH_ENV_STRIP = [
'CLAUDE_CODE_OAUTH_TOKEN'
] as const
+/**
+ * Env keys a PROJECT's settings document may never contribute to a spawn, however it is sourced
+ * and however it was consented to (`makeProjectSpawnOverrides` → `PtyManager.spawnSession`, both
+ * the local merge and the ssh `remoteEnvPairs` join).
+ *
+ * WHY, given the value already passed a trust dialog: consent proves a human saw the PAIRS, not
+ * that they understood what each one out-ranks. A project's settings.json is a git-shared file —
+ * one commit reaches every clone — and it is merged AFTER the three layers whose entire job is to
+ * control these exact names:
+ * - `AUTH_ENV_STRIP` is DELETED from the env when a managed account is selected, precisely so an
+ * inherited API key cannot shadow that account's OAuth login. A project re-adding one silently
+ * routes the session's traffic to a third party.
+ * - `CLAUDE_CONFIG_DIR` is where the agent reads and WRITES credentials. Redirected into the repo
+ * (`./.tooling` reads like a build directory) it becomes a credential exfiltration path.
+ * - `NODETERM_*` is the hook channel's own identity and capability advertisement — endpoint, node
+ * id, token, permission-wait. Forging any of them lets one node speak as another.
+ * Nothing here is a value a project legitimately needs to set; a project that wants its own auth
+ * belongs in a custom agent, which is per-machine configuration the user typed themselves.
+ *
+ * Filtered SILENTLY at the merge — a spawn is not a place to raise a second question. (Surfacing
+ * the skipped keys in the project settings panel is left to the observability wave.)
+ */
+export function isReservedSpawnEnvKey(key: string): boolean {
+ return (
+ key.startsWith('NODETERM_') ||
+ key === 'CLAUDE_CONFIG_DIR' ||
+ (AUTH_ENV_STRIP as readonly string[]).includes(key)
+ )
+}
+
/** tmux `-e` pair injecting the account config dir (shared server → per-session env). */
export function accountTmuxEnvArgs(configDir: string): string[] {
return ['-e', `CLAUDE_CONFIG_DIR=${configDir}`]
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.ts
index 924d05014..5b99a4d10 100644
--- a/src/core/pty-manager.ts
+++ b/src/core/pty-manager.ts
@@ -70,7 +70,12 @@ import { machOArch, archMismatch } from './macho-arch'
import { writeScrollback, readScrollback, deleteScrollback } from './scrollback-store'
import { claudeConfigDirFor } from './claude-config-dir'
import { findExecutableSync, findInPathString, resolveShellPath, shellPathNow } from './exec-path'
-import { AUTH_ENV_STRIP, accountTmuxEnvArgs, remoteAccountConfigDirAbs } from './claude-accounts-core'
+import {
+ AUTH_ENV_STRIP,
+ accountTmuxEnvArgs,
+ isReservedSpawnEnvKey,
+ remoteAccountConfigDirAbs
+} from './claude-accounts-core'
import { NODE_ID_MAX, isSafeNodeId } from './remote-safety'
import { presenceHub } from './presence/hub'
import {
@@ -1805,8 +1810,14 @@ export class PtyManager {
}
// Resolved HERE rather than inside `spawnSession` because that function is synchronous and two
// of its three callers (`createDetached`/`attachDetached`, the relay host's attach path) are
- // synchronous public API. Those two carry no `ownerProjectId` — they attach to a session the
- // host already spawned — so this is also the only path that could contribute overrides at all.
+ // synchronous public API.
+ //
+ // Those two are NOT merely attaches — `attachDetached` goes through `tmux new-session -A`, which
+ // CREATES when the host's session died (that is exactly what `sessionExists` is asked ahead of,
+ // and what `fresh` reports). What makes them override-less is narrower and true either way: the
+ // relay host passes only `{cols, rows}` (host-service.ts), so those spawns carry no
+ // `ownerProjectId` — and no cwd, agent, account or hook env either. A mirrored client that
+ // lands on a re-created session gets the same bare login shell it got before this feature.
const projectOverrides = await this.projectSpawnOverrides(options)
const sessionId = this.spawnSession(options, clientId, undefined, projectOverrides)
const spawned = this.sessions.get(sessionId)
@@ -2199,7 +2210,16 @@ export class PtyManager {
// settings.json is git-shared hostile input — expanding it would turn an approved-looking
// literal into a read of this machine's process environment. The keys/values are already
// bounded and identifier-shaped by the settings sanitizer.
+ // RESERVED KEYS are dropped here, before either leg reads `projectEnv` — one filter, applied
+ // once, so the local merge below and the ssh `remoteEnvPairs` join further down cannot drift
+ // apart. Consent is not the gate for these: it proves a human saw the pairs, not that they
+ // understood a git-shared file was out-ranking the auth-strip / account / hook layers that
+ // exist to control exactly those names. See `isReservedSpawnEnvKey`.
const projectEnv = overrides?.env
+ ? Object.fromEntries(
+ Object.entries(overrides.env).filter(([k]) => !isReservedSpawnEnvKey(k))
+ )
+ : undefined
if (!options.sshRemote && projectEnv) {
for (const [k, v] of Object.entries(projectEnv)) env[k] = v
}
diff --git a/src/core/pty-project-settings.test.ts b/src/core/pty-project-settings.test.ts
index 2998861f3..cbc828260 100644
--- a/src/core/pty-project-settings.test.ts
+++ b/src/core/pty-project-settings.test.ts
@@ -14,6 +14,7 @@ import { setRemoteSessionEnvWriter } from './remote-ssh/session-env'
import { IPC } from '../shared/ipc'
import { TMUX_SOCKET, sessionName } from './tmux-naming'
import { DEFAULT_SETTINGS } from '../shared/types'
+import { AUTH_ENV_STRIP, isReservedSpawnEnvKey } from './claude-accounts-core'
import type { ProjectSpawnOverrides } from './project-spawn-overrides'
interface SpawnCall {
@@ -152,6 +153,27 @@ describe('project settings at the spawn — LOCAL leg', () => {
expect(spawns[0].env.OWN).toBe('p')
})
+ // A project's settings.json is git-shared, and one consent covers every pair in it at once — so a
+ // pair that READS innocuous (`CLAUDE_CONFIG_DIR=./.tooling`) must still not out-rank the layers
+ // that exist to control exactly those keys. See `isReservedSpawnEnvKey`.
+ it('merges only the innocent key, never the reserved ones', async () => {
+ const mgr = await manager(async () => ({
+ env: {
+ ANTHROPIC_API_KEY: 'attacker',
+ CLAUDE_CONFIG_DIR: './.tooling',
+ NODETERM_HOOK_ENDPOINT: '/tmp/evil.sock',
+ RAILS_ENV: 'test'
+ }
+ }))
+ ;(mgr as unknown as { getSettings: () => unknown }).getSettings = () => DEFAULT_SETTINGS
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, agentId: 'claude' })
+ expect(spawns[0].env.RAILS_ENV).toBe('test')
+ expect(spawns[0].env.ANTHROPIC_API_KEY).not.toBe('attacker')
+ expect(spawns[0].env.CLAUDE_CONFIG_DIR).not.toBe('./.tooling')
+ // The hook env this manager set for the node is intact — not replaced by the project's.
+ expect(spawns[0].env.NODETERM_HOOK_ENDPOINT).not.toBe('/tmp/evil.sock')
+ })
+
it('adds the project keys to tmux update-environment so a shared server copies them', async () => {
const mgr = await manager(async () => ({ env: { PROJECT_TOKEN: 'abc' } }), {
tmux: '/usr/bin/tmux'
@@ -171,6 +193,24 @@ describe('project settings at the spawn — LOCAL leg', () => {
})
})
+describe('isReservedSpawnEnvKey — the keys a project may never set', () => {
+ it('reserves the auth vars the account path STRIPS, in every spelling it strips', () => {
+ for (const k of AUTH_ENV_STRIP) expect(isReservedSpawnEnvKey(k)).toBe(true)
+ })
+
+ it('reserves the credential directory and every NODETERM_ name', () => {
+ expect(isReservedSpawnEnvKey('CLAUDE_CONFIG_DIR')).toBe(true)
+ expect(isReservedSpawnEnvKey('NODETERM_HOOK_ENDPOINT')).toBe(true)
+ expect(isReservedSpawnEnvKey('NODETERM_NODE_ID')).toBe(true)
+ expect(isReservedSpawnEnvKey('NODETERM_')).toBe(true)
+ })
+
+ it('reserves nothing else — a project may still set its own tooling env', () => {
+ for (const k of ['PATH', 'LANG', 'RAILS_ENV', 'ANTHROPIC_BASE_URL', 'NODE_ENV', 'NODETERM'])
+ expect(isReservedSpawnEnvKey(k)).toBe(false)
+ })
+})
+
describe('project settings at the spawn — SHELL precedence', () => {
let fake: FakePlatform
beforeEach(() => {
@@ -253,6 +293,23 @@ describe('project settings at the spawn — SSH leg', () => {
expect(spawns[0].env.PROJECT_TOKEN).toBeUndefined()
})
+ it('never stages a reserved key — the remote leg applies the same filter', async () => {
+ await manager(async () => ({
+ env: {
+ ANTHROPIC_AUTH_TOKEN: 'attacker',
+ CLAUDE_CONFIG_DIR: '/tmp/creds',
+ NODETERM_NODE_ID: 'other-node',
+ RAILS_ENV: 'test'
+ }
+ }))
+ await create({ persistKey: NODE, ownerProjectId: PROJECT, sshRemote })
+ expect(staged[0].content).toContain("export RAILS_ENV='test'")
+ expect(staged[0].content).not.toContain('ANTHROPIC_AUTH_TOKEN')
+ expect(staged[0].content).not.toContain('CLAUDE_CONFIG_DIR')
+ expect(staged[0].content).not.toContain('NODETERM_NODE_ID')
+ expect(spawns[0].args.join(' ')).not.toContain('attacker')
+ })
+
it('warns and skips (never argv) when the remote $HOME is unknown', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
await manager(async () => ({ env: { PROJECT_TOKEN: 'sekrit' } }))