fix(security): escape backslashes and cmd metacharacters in hook executor

Backslashes were not escaped before double-quotes in the PowerShell
command wrapper, so paths like C:\foo\" would terminate the string
early and allow injection. cmd /c wrapper had no escaping at all.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
davincidreams 2026-04-27 05:03:04 -04:00
parent 9062273ef5
commit 315e29a2d1

View file

@ -71,9 +71,13 @@ async function executeCommandHook(
// Wrap command for different shells
if (shell === "powershell") {
command = `powershell -Command "${command.replace(/"/g, '\\"')}"`
// Escape backslashes first, then double-quotes, to prevent shell injection
const escaped = command.replace(/\\/g, "\\\\").replace(/"/g, '\\"')
command = `powershell -Command "${escaped}"`
} else if (shell === "cmd") {
command = `cmd /c "${command}"`
// Escape % (variable expansion) and ^ (cmd escape char) to prevent injection
const escaped = command.replace(/\^/g, "^^").replace(/%/g, "%%")
command = `cmd /c "${escaped}"`
}
try {