T09 · Insecure Skill Coding Practices
- Location
scripts/gate_switch.py:138- Finding
Arbitrary Command Execution Through Untrusted Gate Specifications
- Content
View full analysis
Vulnerability Details
File Location:
scripts/gate_switch.py, lines 138–143
Vulnerability Type: OS command injection throughshell=True
Risk Level: HighVulnerable Code
python if t == "script_exit": r = subprocess.run(c["cmd"], shell=True, capture_output=True, text=True, timeout=300) expect = c.get("expect", 0) ok = r.returncode == expect tail = (r.stdout or r.stderr).strip().splitlines() return ok, f"{label}: exit={r.returncode} expect={expect} {tail[-1] if tail else ''}"The dangerous command value can also be populated through unrestricted placeholder substitution in
scripts/gate_switch.py, lines 151–153:python raw = open(spec_path, encoding="utf-8").read() for k, v in bindings.items(): raw = raw.replace("{%s}" % k, v)Technical Analysis
The
script_exitcheck readschecks[].cmdfrom a supplied JSON specification and passes it directly tosubprocess.run()withshell=True. No executable allowlist, argument validation, shell-metacharacter filtering, or trust-boundary enforcement is applied.As a result, a specification author can execute arbitrary shell syntax rather than merely invoke a verification program. The documented
--set key=valuemechanism increases the exposure because binding values are inserted into the raw JSON before parsing. If a command field contains a placeholder, an attacker who controls the corresponding binding may introduce shell operators, command substitutions, redirections, pipelines, or additional commands.The timeout limits execution duration but does not constrain what the command can access or modify. Capturing output similarly provides no meaningful security boundary.
Attack Path
-
An attacker gains the ability to supply or modify the JSON file passed through
--spec, or controls a--setvalue used inside ascript_exitcommand. -
The attacker defines a check such as:
json { "gate": "attacker-controlled-gate", "che
...[truncated 1676 chars]
-
- Remediation
View remediation
Remediation Suggestions
-
Remove shell interpretation
- Replace command strings with JSON argument arrays.
- Invoke
subprocess.run()withshell=False, which is the default.
Example specification:
json { "type": "script_exit", "argv": ["python3", "scripts/check_result.py", "--input", "result.json"], "expect": 0 }Example implementation:
python if t == "script_exit": argv = c["argv"] if not isinstance(argv, list) or not argv or not all( isinstance(arg, str) for arg in argv ): raise SpecError("script_exit.argv must be a non-empty string array") r = subprocess.run( argv, shell=False, capture_output=True, text=True, timeout=300, check=False, ) -
Restrict executable selection
- Maintain an explicit allowlist of approved verification executables or scripts.
- Resolve paths canonically and ensure approved scripts remain inside a trusted directory.
- Reject symbolic-link escapes and relative traversal outside that directory.
-
Treat specifications as trusted code
- Do not execute
script_exitchecks from arbitrary or externally supplied specifications. - Require specifications to come from a controlled directory and consider integrity verification or signatures when crossing trust boundaries.
- Document that command-capable specifications carry code-execution authority.
- Do not execute
-
Harden placeholder handling
- Avoid textual substitution across raw JSON.
- Parse JSON first, then substitute only into explicitly permitted data fields.
- Keep command arguments as separate array elements so substituted values cannot become shell syntax.
- Validate each binding according to its expected type and permitted character set.
-
Apply least privilege
- Run verification in a sandbox or restricted subprocess account.
- Limit filesystem acc ...[truncated 457 chars]
-
