Skip to content

fix: prevent command injection in auto-stage.js#9

Open
cincyjosh wants to merge 1 commit intokaranb192:mainfrom
cincyjosh:fix/command-injection-auto-stage
Open

fix: prevent command injection in auto-stage.js#9
cincyjosh wants to merge 1 commit intokaranb192:mainfrom
cincyjosh:fix/command-injection-auto-stage

Conversation

@cincyjosh
Copy link
Copy Markdown

Summary

  • Vulnerability: stageFile() in auto-stage.js built a shell command using template string interpolation — execSync(\git add "${filePath}"`). A filename containing "followed by shell metacharacters (e.g.evil"; touch marker; echo "x.txt`) breaks out of the quotes and executes arbitrary commands when the PostToolUse hook fires.
  • Fix: Replace execSync with spawnSync('git', ['add', '--', filePath], ...), which passes the filename as a literal argument without involving a shell. The -- prevents git from interpreting filenames that begin with - as flags.
  • Regression test added: Creates a file whose name embeds a touch command and asserts the marker file is not created after stageFile runs.

Security context

This was identified during a code audit. The attack requires either:

  1. Claude being induced to write a file with a crafted name, or
  2. A repo that already contains a file with shell metacharacters in its name

Both are realistic in adversarial or misconfigured environments. The fix has zero functional impact on normal usage.

Test plan

  • All 263 existing tests still pass (npm test)
  • New regression test passes (confirms injection is blocked)
  • Verified fix works: with the old execSync the new test fails (marker file is created); with spawnSync it passes

🤖 Generated with Claude Code

stageFile() was building a shell command with an unescaped filename:
  execSync(`git add "${filePath}"`)
A filename containing `"` followed by shell metacharacters (e.g. `;`,
`&&`, `|`) would break out of the quotes and execute arbitrary commands
when Claude Code's PostToolUse hook ran.

Replace execSync with spawnSync using an args array, which bypasses the
shell entirely so the filename is always treated as a literal argument.
Add `--` so git cannot interpret filenames beginning with `-` as flags.

Adds a regression test that creates a file whose name embeds a `touch`
command and asserts the marker file is not created after stageFile runs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant