Skip to content

Commit 21708f5

Browse files
TakhoffmanGlucksberg
andauthored
fix(exec): resolve PATH key case-insensitively for Windows pathPrepend (#25399) (#31879)
Co-authored-by: Glucksberg <[email protected]>
1 parent 1ea42eb commit 21708f5

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

src/agents/bash-tools.exec-runtime.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { Type } from "@sinclair/typebox";
44
import type { ExecAsk, ExecHost, ExecSecurity } from "../infra/exec-approvals.js";
55
import { requestHeartbeatNow } from "../infra/heartbeat-wake.js";
66
import { isDangerousHostEnvVarName } from "../infra/host-env-security.js";
7-
import { mergePathPrepend } from "../infra/path-prepend.js";
7+
import { findPathKey, mergePathPrepend } from "../infra/path-prepend.js";
88
import { enqueueSystemEvent } from "../infra/system-events.js";
99
import type { ProcessSession } from "./bash-process-registry.js";
1010
import type { ExecToolDetails } from "./bash-tools.exec-types.js";
1111
import type { BashSandboxConfig } from "./bash-tools.shared.js";
12-
export { applyPathPrepend, normalizePathPrepend } from "../infra/path-prepend.js";
12+
export { applyPathPrepend, findPathKey, normalizePathPrepend } from "../infra/path-prepend.js";
1313
import { logWarn } from "../logger.js";
1414
import type { ManagedRun } from "../process/supervisor/index.js";
1515
import { getProcessSupervisor } from "../process/supervisor/index.js";
@@ -210,9 +210,10 @@ export function applyShellPath(env: Record<string, string>, shellPath?: string |
210210
if (entries.length === 0) {
211211
return;
212212
}
213-
const merged = mergePathPrepend(env.PATH, entries);
213+
const pathKey = findPathKey(env);
214+
const merged = mergePathPrepend(env[pathKey], entries);
214215
if (merged) {
215-
env.PATH = merged;
216+
env[pathKey] = merged;
216217
}
217218
}
218219

src/agents/bash-tools.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "node:path";
22
import { afterEach, beforeEach, describe, expect, it } from "vitest";
3+
import { applyPathPrepend, findPathKey } from "../infra/path-prepend.js";
34
import { peekSystemEvents, resetSystemEventsForTest } from "../infra/system-events.js";
45
import { captureEnv } from "../test-utils/env.js";
56
import { getFinishedSession, resetProcessRegistryForTests } from "./bash-process-registry.js";
@@ -547,3 +548,57 @@ describe("exec PATH handling", () => {
547548
}
548549
});
549550
});
551+
552+
describe("findPathKey", () => {
553+
it("returns PATH when key is uppercase", () => {
554+
expect(findPathKey({ PATH: "/usr/bin" })).toBe("PATH");
555+
});
556+
557+
it("returns Path when key is mixed-case (Windows style)", () => {
558+
expect(findPathKey({ Path: "C:\\Windows\\System32" })).toBe("Path");
559+
});
560+
561+
it("returns PATH as default when no PATH-like key exists", () => {
562+
expect(findPathKey({ HOME: "/home/user" })).toBe("PATH");
563+
});
564+
565+
it("prefers uppercase PATH when both PATH and Path exist", () => {
566+
expect(findPathKey({ PATH: "/usr/bin", Path: "C:\\Windows" })).toBe("PATH");
567+
});
568+
});
569+
570+
describe("applyPathPrepend with case-insensitive PATH key", () => {
571+
it("prepends to Path key on Windows-style env (no uppercase PATH)", () => {
572+
const env: Record<string, string> = { Path: "C:\\Windows\\System32" };
573+
applyPathPrepend(env, ["C:\\custom\\bin"]);
574+
// Should write back to the same `Path` key, not create a new `PATH`
575+
expect(env.Path).toContain("C:\\custom\\bin");
576+
expect(env.Path).toContain("C:\\Windows\\System32");
577+
expect("PATH" in env).toBe(false);
578+
});
579+
580+
it("preserves all existing entries when prepending via Path key", () => {
581+
// Use platform-appropriate paths and delimiters
582+
const delim = path.delimiter;
583+
const existing = isWin
584+
? ["C:\\Windows\\System32", "C:\\Windows", "C:\\Program Files\\nodejs"]
585+
: ["/usr/bin", "/usr/local/bin", "/opt/node/bin"];
586+
const prepend = isWin ? ["C:\\custom\\bin"] : ["/custom/bin"];
587+
const existingPath = existing.join(delim);
588+
const env: Record<string, string> = { Path: existingPath };
589+
applyPathPrepend(env, prepend);
590+
const parts = env.Path.split(delim);
591+
expect(parts[0]).toBe(prepend[0]);
592+
for (const entry of existing) {
593+
expect(parts).toContain(entry);
594+
}
595+
});
596+
597+
it("respects requireExisting option with Path key", () => {
598+
const env: Record<string, string> = { HOME: "/home/user" };
599+
applyPathPrepend(env, ["C:\\custom\\bin"], { requireExisting: true });
600+
// No Path/PATH key exists, so nothing should be written
601+
expect("PATH" in env).toBe(false);
602+
expect("Path" in env).toBe(false);
603+
});
604+
});

src/infra/path-prepend.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
import path from "node:path";
22

3+
/**
4+
* Find the actual key used for PATH in the env object.
5+
* On Windows, `process.env` stores it as `Path` (not `PATH`),
6+
* and after copying to a plain object the original casing is preserved.
7+
*/
8+
export function findPathKey(env: Record<string, string>): string {
9+
if ("PATH" in env) {
10+
return "PATH";
11+
}
12+
for (const key of Object.keys(env)) {
13+
if (key.toUpperCase() === "PATH") {
14+
return key;
15+
}
16+
}
17+
return "PATH";
18+
}
19+
320
export function normalizePathPrepend(entries?: string[]) {
421
if (!Array.isArray(entries)) {
522
return [];
@@ -48,11 +65,15 @@ export function applyPathPrepend(
4865
if (!Array.isArray(prepend) || prepend.length === 0) {
4966
return;
5067
}
51-
if (options?.requireExisting && !env.PATH) {
68+
// On Windows the PATH key may be stored as `Path` (case-insensitive env vars).
69+
// After coercing to a plain object the original casing is preserved, so we must
70+
// look up the actual key to read the existing value and write the merged result back.
71+
const pathKey = findPathKey(env);
72+
if (options?.requireExisting && !env[pathKey]) {
5273
return;
5374
}
54-
const merged = mergePathPrepend(env.PATH, prepend);
75+
const merged = mergePathPrepend(env[pathKey], prepend);
5576
if (merged) {
56-
env.PATH = merged;
77+
env[pathKey] = merged;
5778
}
5879
}

0 commit comments

Comments
 (0)