Skip to content

fix: validate $ARGUMENTS path before shell interpolation in optimize command#197

Open
xiaolai wants to merge 1 commit intoccplugins:mainfrom
xiaolai:fix/nlpm-optimize-shell-injection
Open

fix: validate $ARGUMENTS path before shell interpolation in optimize command#197
xiaolai wants to merge 1 commit intoccplugins:mainfrom
xiaolai:fix/nlpm-optimize-shell-injection

Conversation

@xiaolai
Copy link
Copy Markdown

@xiaolai xiaolai commented Apr 21, 2026

Automated audit: This PR was generated by NLPM, a natural language programming linter, running via claude-code-action. Please evaluate the diff on its merits.

Security Bug (Medium)

plugins/optimize/commands/optimize.md lines 6–7 use the ! (pre-context shell execution) mechanism to gather file metadata:

- File size: !`du -h $ARGUMENTS 2>/dev/null || echo "File not specified"`
- Line count: !`wc -l $ARGUMENTS 2>/dev/null || echo "File not specified"`

$ARGUMENTS is interpolated unquoted and unvalidated directly into shell commands. A user passing a crafted value such as "; rm -rf ~" or $(malicious-command) would have it executed during context-building — before Claude is even invoked.

Note: the allowed-tools: Bash(du:*), Bash(wc:*) restriction applies to the Bash tool used during Claude's turn, not to the ! pre-context execution which happens at prompt-build time. The injection surface is the ! backtick mechanism.

Fix

Add a [[ "$ARGUMENTS" =~ ^[a-zA-Z0-9_./-]+$ ]] guard before each command and quote the variable:

- File size: !`[[ "$ARGUMENTS" =~ ^[a-zA-Z0-9_./-]+$ ]] && du -h "$ARGUMENTS" 2>/dev/null || echo "File not specified"`
- Line count: !`[[ "$ARGUMENTS" =~ ^[a-zA-Z0-9_./-]+$ ]] && wc -l "$ARGUMENTS" 2>/dev/null || echo "File not specified"`

This allows safe file paths (letters, digits, _, ., /, -) and rejects anything with shell metacharacters. The behavior for legitimate file path arguments is unchanged.

…command

The pre-context shell commands `du -h $ARGUMENTS` and `wc -l $ARGUMENTS`
interpolated raw user input directly into shell, enabling injection via
the !` execution mechanism (e.g. a value like `"; rm -rf ~"`).

Add a `[[ "$ARGUMENTS" =~ ^[a-zA-Z0-9_./-]+$ ]]` guard before each
command and quote the variable, so only safe file path characters are
accepted.

Co-Authored-By: Claude Code <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