Use minimal env for Codex subprocess

This commit is contained in:
lezi-fun committed 2026-05-17 16:07:38 +08:00
1 parent e84da1caf3
commit 8bedfcee17
1 file changed
+21 -18
+21 -18
View File
@@ -189,34 +189,37 @@ async function runCodexExec(args: string[], extraEnv: Record<string, string>): P
}); });
} }
const SENSITIVE_ENV_KEYS = [
"GITHUB_TOKEN",
"GITHUB_APP_ID",
"GITHUB_APP_PRIVATE_KEY",
"GITHUB_APP_INSTALLATION_ID"
] as const;
function buildCodexChildEnv(extraEnv: Record<string, string>): NodeJS.ProcessEnv { function buildCodexChildEnv(extraEnv: Record<string, string>): NodeJS.ProcessEnv {
const env: NodeJS.ProcessEnv = { const env: NodeJS.ProcessEnv = {};
...process.env,
...extraEnv
};
for (const key of SENSITIVE_ENV_KEYS) { copyEnv(env, "PATH");
delete env[key]; copyEnv(env, "HOME");
} copyEnv(env, "USER");
copyEnv(env, "SHELL");
copyEnv(env, "TMPDIR");
copyEnv(env, "TMP");
copyEnv(env, "TEMP");
copyEnv(env, "LANG");
copyEnv(env, "LC_ALL");
copyEnv(env, "TERM");
copyEnv(env, "NO_COLOR");
copyEnv(env, "FORCE_COLOR");
copyEnv(env, "CI");
for (const [key, value] of Object.entries(extraEnv)) { for (const [key, value] of Object.entries(extraEnv)) {
if (SENSITIVE_ENV_KEYS.includes(key as (typeof SENSITIVE_ENV_KEYS)[number])) {
continue;
}
env[key] = value; env[key] = value;
} }
return env; return env;
} }
function copyEnv(target: NodeJS.ProcessEnv, key: string): void {
const value = process.env[key];
if (value !== undefined) {
target[key] = value;
}
}
function streamProcessOutput(stream: "stdout" | "stderr", text: string): void { function streamProcessOutput(stream: "stdout" | "stderr", text: string): void {
const normalized = text.replace(/\r\n/g, "\n"); const normalized = text.replace(/\r\n/g, "\n");
const lines = normalized.split("\n"); const lines = normalized.split("\n");