Clear Codex app-server env keys case-insensitively on Windows (#73102)

* fix(codex): clear app-server env case variants

* fix(codex): avoid repeated env clear scans
This commit is contained in:
pashpashpash
2026-04-28 13:34:14 -07:00
committed by GitHub
parent 4509420dd4
commit 78d51dcebe
2 changed files with 46 additions and 2 deletions

View File

@@ -112,6 +112,28 @@ describe("resolveCodexAppServerSpawnEnv", () => {
});
});
it("clears denied env vars case-insensitively on Windows", () => {
expect({
...resolveCodexAppServerSpawnEnv(
{
env: {
OpenAI_Api_Key: "configured-openai-key",
Other: "configured",
},
clearEnv: ["OPENAI_API_KEY", " CODEX_API_KEY ", ""],
},
{
Codex_Api_Key: "parent-codex-key",
KEEP: "parent",
},
"win32",
),
}).toEqual({
KEEP: "parent",
Other: "configured",
});
});
it("uses a null-prototype env map and ignores prototype-polluting keys", () => {
const overrides = Object.create(null) as Record<string, string | undefined>;
Object.defineProperty(overrides, "__proto__", {

View File

@@ -46,16 +46,38 @@ export function resolveCodexAppServerSpawnInvocation(
export function resolveCodexAppServerSpawnEnv(
options: Pick<CodexAppServerStartOptions, "env" | "clearEnv">,
baseEnv: NodeJS.ProcessEnv = process.env,
platform: NodeJS.Platform = process.platform,
): NodeJS.ProcessEnv {
const env = Object.create(null) as NodeJS.ProcessEnv;
copySafeEnvironmentEntries(env, baseEnv);
copySafeEnvironmentEntries(env, options.env ?? {});
for (const key of options.clearEnv ?? []) {
delete env[key];
const keysToClear = normalizedEnvironmentKeys(options.clearEnv ?? []);
if (platform === "win32") {
const lowerCaseKeysToClear = new Set(keysToClear.map((key) => key.toLowerCase()));
for (const candidate of Object.keys(env)) {
if (lowerCaseKeysToClear.has(candidate.toLowerCase())) {
delete env[candidate];
}
}
} else {
for (const key of keysToClear) {
delete env[key];
}
}
return env;
}
function normalizedEnvironmentKeys(rawKeys: readonly string[]): string[] {
const keys: string[] = [];
for (const rawKey of rawKeys) {
const key = rawKey.trim();
if (key.length > 0) {
keys.push(key);
}
}
return keys;
}
function copySafeEnvironmentEntries(
target: NodeJS.ProcessEnv,
source: NodeJS.ProcessEnv | Record<string, string | undefined>,