gitagent icon indicating copy to clipboard operation
gitagent copied to clipboard

Hook path-safety check is Windows-incompatible, causing pre_tool_use hooks to silently no-op

Open Nivesh353 opened this issue 4 months ago • 0 comments

Summary

pre_tool_use hooks (e.g. a safety-check script meant to block destructive commands like rm -rf) silently fail to run on Windows, because the base-directory containment check in hooks.ts is hardcoded to POSIX path separators.

Root cause

src/hooks.ts (around line 69):

if (!resolvedScript.startsWith(allowedBase + "/") && resolvedScript !== allowedBase) {
    reject(new Error(`Hook "${hook.script}" escapes its base directory`));
}

allowedBase + "/" hardcodes a forward slash. On Windows, path.resolve() returns backslash-separated paths (e.g. C:\Users...\hooks\safety-check.sh), so this check always fails for a legitimately-nested hook script — it's a false positive, not a real path-traversal case.

Compounding this: runHooks() catches any hook execution error and defaults to { action: "allow" } (fail-open), so the false "escapes its base directory" error is silently swallowed and the tool call proceeds as if no hook existed at all.

Impact

On Windows, pre_tool_use hooks never actually execute their logic. Any safety/validation hook (e.g. blocking rm -rf) is effectively disabled, with only a console warning (Hook error: ... escapes its base directory) as a trace — the destructive command still runs.

Repro

import path from "path"; const { join, resolve } = path.win32; const allowedBase = resolve("C:\Users\test\agent"); const resolvedScript = resolve(join("C:\Users\test\agent", "hooks", "safety-check.sh")); console.log(resolvedScript.startsWith(allowedBase + "/")); // false — bug reproduced without needing Windows

Fix

Replace the manual string check with path.relative(), which correctly handles OS-specific separators:

const rel = relative(allowedBase, resolvedScript); const escapesBase = rel !== "" && (rel.startsWith("..") || isAbsolute(rel)); Verified this resolves correctly on both path.win32 and path.posix, while still correctly catching genuine traversal attempts (e.g. ../../evil.sh).

Separately worth addressing: runHooks()'s fail-open behavior on any hook error — a safety-oriented hook failing to execute for any reason (not just this bug) currently defaults to allowing the action rather than blocking it.

Nivesh353 avatar Jul 07 '26 14:07 Nivesh353