mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:30:42 +00:00
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:
@@ -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__", {
|
||||
|
||||
@@ -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>,
|
||||
|
||||
Reference in New Issue
Block a user