T09 · Insecure Skill Coding Practices
- Location
scripts/loop.py:67- Finding
Arbitrary Command Execution Through Untrusted Loop Definitions
- Content
View full analysis
Vulnerability Details
File Location:
scripts/loop.py, lines 67–79
Vulnerability Type: OS command injection through shell execution
Risk Level: HighVulnerable Code
python def execute(step): if not step.get("run"): # No run field: treat as an external/manual delegation return True, "(delegated / no run command)" try: r = subprocess.run(step["run"], shell=True, capture_output=True, encoding="utf-8", errors="replace", timeout=60) out = (r.stdout or "") + (r.stderr or "") return (r.returncode == 0), out.strip() except Exception as e: return False, f"exec-error: {e}"Technical Analysis
The
runvalue is loaded from a caller-supplied JSON loop definition and passed directly tosubprocess.runwithshell=True. No command allowlist, argument separation, escaping, approval gate, or execution sandbox is applied.Consequently, the shell interprets metacharacters and compound expressions in the JSON value, including command separators, pipelines, substitutions, and redirections. The adjacent
acceptcheck only examines command output after execution and therefore does not restrict what the command can do. The 60-second timeout limits duration but does not prevent immediate harmful actions or terminate descendant processes reliably in every case.Attack Path
-
An attacker supplies or modifies a loop definition consumed by
python scripts/loop.py run <loop.json>. -
The attacker places an arbitrary shell payload in a step's
runproperty, for example:json { "goal": "process task", "steps": [ { "id": "task", "desc": "apparently legitimate operation", "run": "echo ACCEPTED; attacker_command", "accept": "ACCEPTED" } ] } -
load_loop()deserializes the JSON, andrun_loop()for ...[truncated 1173 chars]
-
- Remediation
View remediation
Remediation Suggestions
-
Remove
shell=Trueand represent commands as argument arrays:python import shlex import subprocess command = step["run"] argv = command if isinstance(command, list) else shlex.split(command) result = subprocess.run( argv, shell=False, capture_output=True, encoding="utf-8", errors="replace", timeout=60, check=False, )Prefer requiring a JSON array rather than parsing a command string, because an explicit array avoids ambiguous quoting.
-
Treat loop definitions as executable policy, not ordinary data. Only accept definitions from trusted, integrity-protected sources, and require explicit user confirmation before executing each command from externally generated content.
-
Validate the executable and arguments against a narrow allowlist appropriate to the intended workflow. Reject shell syntax, unexpected executables, dangerous flags, absolute paths where unnecessary, and unapproved environment manipulation.
-
Execute approved commands in a restricted environment using a dedicated low-privilege account or sandbox. Limit filesystem mounts, network access, environment variables, process creation, and access to credentials.
-
Use a controlled working directory and a minimal environment rather than inheriting all process state. Add output-size limits and terminate the full process group on timeout.
-
Keep
acceptstrictly as an output validation mechanism; do not treat it as a security boundary. Add provenance logging for the definition, executable, arguments, working directory, and authorization decision. -
If arbitrary shell workflows are an intentional feature, clearly document that loop files are executable and must never be run from untrusted sources.
-
