fix: prevent command injection in auto-stage.js#9
Open
cincyjosh wants to merge 1 commit intokaranb192:mainfrom
Open
fix: prevent command injection in auto-stage.js#9cincyjosh wants to merge 1 commit intokaranb192:mainfrom
cincyjosh wants to merge 1 commit intokaranb192:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
stageFile()inauto-stage.jsbuilt 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.execSyncwithspawnSync('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.touchcommand and asserts the marker file is not created afterstageFileruns.Security context
This was identified during a code audit. The attack requires either:
Both are realistic in adversarial or misconfigured environments. The fix has zero functional impact on normal usage.
Test plan
npm test)execSyncthe new test fails (marker file is created); withspawnSyncit passes🤖 Generated with Claude Code