ECC/scripts/hooks
Michael 600072ebd8
fix(hooks): resolve MCP health-check spawn ENOENT on Windows (#1456)
* fix(hooks): resolve MCP health-check spawn ENOENT on Windows

On Windows, commands like 'npx' are batch files (npx.cmd) that require
shell expansion to resolve via PATH. Without shell: true, Node.js
spawn() fails with ENOENT.

However, absolute paths (e.g. C:\Program Files\nodejs\node.exe) must
NOT use shell mode because cmd.exe misparses paths containing spaces.

Fix: enable shell mode only for non-absolute commands on Windows, using
path.isAbsolute() to distinguish. This matches how attemptReconnect()
already handles the shell option.

Fixes #1455

* fix(hooks): harden Windows shell spawn — validate command for metacharacters

Addresses bot review feedback on PR #1456:

- Add UNSAFE_SHELL_CHARS regex to guard against shell injection when
  needsShell=true: cmd.exe operators (&, |, <, >, ^, %, !, (), ;,
  whitespace) are rejected before shell mode is enabled
- Add typeof command === 'string' check so path.isAbsolute() cannot
  throw on malformed non-string command values
- Rename test to 'via PATH resolution' (not Windows-only; runs all platforms)
- Fix misleading test comment: 'node' resolves via PATH like npx.cmd but
  does not itself use .cmd; comment now accurately reflects the intent

* fix(hooks): kill full process tree on Windows when shell mode is used

When needsShell=true, the spawned child is cmd.exe. Calling child.kill()
only terminates the shell, leaving the real server process orphaned.

Use taskkill /PID <pid> /T /F on Windows+shell to kill the entire
process tree rooted at cmd.exe. Fall back to SIGTERM+SIGKILL on all
other platforms or when shell mode is not active.

* fix(hooks): fall back to child.kill() when taskkill fails

Windows taskkill can fail if it's not on PATH, the process already
exited, or permissions are denied. Previously the failure was silently
ignored and no kill signal reached the child.

Now: capture the spawnSync result and fall back to child.kill('SIGKILL')
on any taskkill error or non-zero status. This still may leak a
detached server process but at least guarantees the cmd.exe shell is
signaled.
2026-05-11 01:13:37 -04:00
..
auto-tmux-dev.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
bash-hook-dispatcher.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
block-no-verify.js fix: parse block-no-verify flags by shell words 2026-04-29 21:59:12 -04:00
check-console-log.js Fix stdin overflow bug in hook scripts - truncate chunks to stay within MAX_STDIN limit 2026-02-18 07:40:12 +00:00
check-hook-enabled.js feat: deliver v1.8.0 harness reliability and parity updates 2026-03-04 14:48:06 -08:00
config-protection.js fix: fold session manager blockers into one candidate 2026-03-24 23:08:27 -04:00
cost-tracker.js feat: sync ecc2 cost tracker metrics 2026-04-09 06:22:20 -07:00
design-quality-check.js feat: add web frontend rules and design quality hook 2026-04-02 17:33:17 -07:00
desktop-notify.js feat(hooks): add WSL desktop notification support via PowerShell + BurntToast (#1019) 2026-03-30 03:14:49 -04:00
doc-file-warning.js fix(lint): prefix unused options parameter with underscore 2026-03-29 10:14:53 +08:00
evaluate-session.js Fix stdin overflow bug in hook scripts - truncate chunks to stay within MAX_STDIN limit 2026-02-18 07:40:12 +00:00
gateguard-fact-force.js fix: show correct gateguard hook recovery id 2026-04-30 11:26:15 -04:00
governance-capture.js fix: fold session manager blockers into one candidate 2026-03-24 23:08:27 -04:00
insaits-security-monitor.py test: cover hook bootstrap and InsAIts monitor 2026-04-29 17:45:22 -04:00
insaits-security-wrapper.js fix: fail open on insaits monitor errors 2026-04-29 18:03:33 -04:00
mcp-health-check.js fix(hooks): resolve MCP health-check spawn ENOENT on Windows (#1456) 2026-05-11 01:13:37 -04:00
observe-runner.js fix: route continuous learning observe hooks through node 2026-04-29 21:28:59 -04:00
plugin-hook-bootstrap.js fix: bootstrap plugin-installed hook commands safely 2026-04-14 20:24:21 -07:00
post-bash-build-complete.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-bash-command-log.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-bash-dispatcher.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-bash-pr-created.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
post-edit-accumulator.js perf(hooks): batch format+typecheck at Stop instead of per Edit (#746) 2026-03-31 14:12:12 -07:00
post-edit-console-warn.js Merge branch 'affaan-m:main' into main 2026-02-18 18:33:04 +05:30
post-edit-format.js fix(hooks): add Windows .cmd support with shell injection guard 2026-03-11 10:45:28 +09:00
post-edit-typecheck.js Fix stdin overflow bug in hook scripts - truncate chunks to stay within MAX_STDIN limit 2026-02-18 07:40:12 +00:00
pre-bash-commit-quality.js fix: run pre-bash linters through windows wrappers 2026-04-29 18:59:10 -04:00
pre-bash-dev-server-block.js fix: resolve PR 371 portability regressions 2026-03-09 22:49:43 -07:00
pre-bash-dispatcher.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
pre-bash-git-push-reminder.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
pre-bash-tmux-reminder.js fix: consolidate bash hooks without fork storms 2026-04-14 21:23:57 -07:00
pre-compact.js fix: nullish coalescing in evaluate-session config, narrow pre-compact glob, add 11 tests 2026-02-13 02:42:01 -08:00
pre-write-doc-warn.js feat: deliver v1.8.0 harness reliability and parity updates 2026-03-04 14:48:06 -08:00
quality-gate.js refactor: deduplicate config lists and unify resolveFormatterBin branches 2026-03-11 10:45:28 +09:00
run-with-flags-shell.sh fix(hooks): pass phase argument from hook ID to observe.sh (#1042) 2026-03-31 14:05:16 -07:00
run-with-flags.js fix: route continuous learning observe hooks through node 2026-04-29 21:28:59 -04:00
session-activity-tracker.js Merge pull request #1384 from KeWang0622/fix/lint-md028-eqeqeq 2026-04-12 23:03:19 -07:00
session-end-marker.js fix: clean up observer sessions on lifecycle end 2026-04-02 18:02:29 -07:00
session-end.js review: broaden CLAUDE_TRANSCRIPT_PATH fallback to cover missing/empty JSON fields 2026-04-19 14:35:21 +09:00
session-start-bootstrap.js fix: shorten plugin slug to ecc 2026-04-05 14:31:30 -07:00
session-start.js fix: cap session-start context injection 2026-04-30 08:41:52 -04:00
stop-format-typecheck.js perf(hooks): batch format+typecheck at Stop instead of per Edit (#746) 2026-03-31 14:12:12 -07:00
suggest-compact.js fix(hooks): scrub secrets and harden hook security (#348) 2026-03-07 14:47:31 -08:00