T09 · Insecure Skill Coding Practices
- Location
scripts/dispatch.py:31- Finding
Shell Command Injection Through Untrusted Tool Arguments
- Content
View full analysis
Vulnerability Details
File Location:
scripts/dispatch.py, lines 31-32
Vulnerability Type: OS command injection caused by unsafe shell execution
Risk Level: HighVulnerable Code
python cmd = entry["cmd"].format(**args) r = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=30)Technical Analysis
The dispatcher inserts values from the tool call's
argumentsobject directly into a registered command template usingstr.format(). It then passes the resulting string tosubprocess.run()withshell=True.Because a system shell interprets the formatted string, shell control characters contained in an argument—such as semicolons, command substitutions, pipes, or redirections—are treated as executable syntax rather than as literal argument data.
The
validate()function only verifies that the requested tool is registered and that required argument names are present. It does not enforce argument types, validate values against a schema, reject shell metacharacters, or otherwise prevent command injection. Registration of the command template therefore does not make argument interpolation safe.This implementation also contradicts the documented safety claim that command tools disable unnecessary shell expansion and redirection.
Attack Path
-
A registry defines a command tool containing an argument placeholder, for example:
json { "echo": { "type": "command", "cmd": "echo {text}", "required": ["text"] } } -
An attacker or untrusted model-generated tool call supplies shell syntax in that argument:
json { "name": "echo", "arguments": { "text": "hello; id" } } -
validate()accepts the call becauseechois registered and thetextargument is present. -
String formatting produces:
sh echo hello; id -
Because t ...[truncated 1080 chars]
-
- Remediation
View remediation
Remediation Suggestions
-
Remove
shell=Trueand execute commands as argument arrays withshell=False:python command = entry["cmd"] argv = [part.format(**args) for part in command] result = subprocess.run( argv, shell=False, capture_output=True, text=True, timeout=30, check=False, )Registry commands should consequently be represented as arrays rather than shell command strings:
json { "echo": { "type": "command", "cmd": ["echo", "{text}"], "required": ["text"] } } -
Validate that
callis an object,argumentsis an object, and each argument conforms to an explicit schema. Enforce types, lengths, enumerations, numeric bounds, and application-specific allowlists before execution. -
Reject undeclared arguments by default. This prevents unexpected input from reaching command construction or Python function calls.
-
Avoid treating shell escaping as the primary defense. Utilities such as
shlex.quote()are fragile when templates contain complex shell syntax; direct process execution with separate arguments is safer. -
If shell features are genuinely required, isolate those tools behind dedicated, fixed wrapper programs. Do not interpolate untrusted values into shell source. Apply strict allowlists and require explicit administrative approval for dangerous operations.
-
Run the dispatcher under a dedicated least-privileged account, provide only the necessary filesystem and network access, and avoid exposing secrets through its environment.
-
Add regression tests containing command separators, substitutions, pipes, redirections, newlines, and quoted payloads to verify that argument values remain literal data and cannot create additional commands.
-
