fix: add PATH to restricted hook env, protect HOME/CLAUDE_PROJECT_DIR from allowlist override, reset plugin hooks state in tests

- P1: When allowedEnvVars is provided, PATH was missing from the base
  restricted env, causing non-builtin commands to fail at exec time
- P2: Allowlisted HOME/CLAUDE_PROJECT_DIR could overwrite normalized
  values from getHomeDirectory()/cwd with ambient process.env values
- P2: Test suite mutated shared pluginHooksState singleton without
  resetting it in afterEach, causing cross-test state leaks
This commit is contained in:
JacobZyy
2026-05-20 22:49:05 +08:00
parent 0a20844bd4
commit 33f121b113
3 changed files with 18 additions and 4 deletions
@@ -56,12 +56,20 @@ export async function executeHookCommand(
const isWin32 = process.platform === "win32";
// Keys that are always set from normalized sources and must not be
// overwritten by ambient process.env values during the allowlist merge.
const PROTECTED_ENV_KEYS = new Set(["HOME", "CLAUDE_PROJECT_DIR"]);
let env: Record<string, string | undefined>;
if (options?.allowedEnvVars) {
const allowedSet = new Set(options.allowedEnvVars);
env = { HOME: home, CLAUDE_PROJECT_DIR: cwd };
env = {
HOME: home,
CLAUDE_PROJECT_DIR: cwd,
PATH: process.env.PATH,
};
for (const key of Object.keys(process.env)) {
if (allowedSet.has(key)) {
if (allowedSet.has(key) && !PROTECTED_ENV_KEYS.has(key)) {
env[key] = process.env[key];
}
}