diff --git a/src/session-env-clamp.ts b/src/session-env-clamp.ts index a64ed77c..2174b821 100644 --- a/src/session-env-clamp.ts +++ b/src/session-env-clamp.ts @@ -32,7 +32,7 @@ */ import { canUsernameRunPrivilegedCommands } from './user-store.js'; -import { enabledClis } from './config/cli-registry/registry.js'; +import { listClis } from './config/cli-registry/registry.js'; /** * Env-var keys a non-granted owner must not be able to set, because each one @@ -68,7 +68,8 @@ import { enabledClis } from './config/cli-registry/registry.js'; * already allowlisted for pi and not addressed here — see resolveOmpHome()). */ export function ownerClampedEnvKeys(): string[] { - return enabledClis().flatMap((entry) => entry.capabilities.privilegedEnvKeys); + // Every CLI, enabled or not: a session can outlive the switch that disabled its CLI. + return listClis().flatMap((entry) => entry.capabilities.privilegedEnvKeys); } /** diff --git a/test/session-env-clamp.test.ts b/test/session-env-clamp.test.ts new file mode 100644 index 00000000..705dcbf0 --- /dev/null +++ b/test/session-env-clamp.test.ts @@ -0,0 +1,35 @@ +/** + * @fileoverview The env clamp's key list, read from the registry. A key stays clamped while + * its CLI is disabled: a session can outlive the switch (the reboot-restore route and the + * custom-model recovery both clamp for one that already exists), and neither the clear + * branch of the custom-model route nor `restartCli()` checks `enabled`. + * + * Port: N/A (the on-disk `clis.json` uses the per-file temp HOME from test/setup.ts). + */ +import { mkdirSync, rmSync, writeFileSync } from 'node:fs'; +import { dirname } from 'node:path'; +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import { reloadCliRegistry, registryFilePath } from '../src/config/cli-registry/registry.js'; +import { clampEnvOverridesForOwner } from '../src/session-env-clamp.js'; + +describe('clampEnvOverridesForOwner: a disabled CLI', () => { + const ORIGINAL = process.env.CODEMAN_MULTIUSER; + + beforeEach(() => { + process.env.CODEMAN_MULTIUSER = '1'; + mkdirSync(dirname(registryFilePath()), { recursive: true }); + writeFileSync(registryFilePath(), JSON.stringify({ clis: { deepseek: { enabled: false } } }), { mode: 0o600 }); + reloadCliRegistry(); + }); + + afterEach(() => { + if (ORIGINAL === undefined) delete process.env.CODEMAN_MULTIUSER; + else process.env.CODEMAN_MULTIUSER = ORIGINAL; + rmSync(registryFilePath(), { force: true }); + reloadCliRegistry(); + }); + + it('still strips its privileged keys for a non-granted owner', async () => { + expect(await clampEnvOverridesForOwner('nobody', { DSH_HOME: '/own' })).toEqual({}); + }); +});