T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/create-market-with-odds.sh:35
- Finding
- Arbitrary Python Code Execution Through estimated_prob Argument<![CDATA[ ## Vulnerability Details **File Location**: `scripts/create-market-with-odds.sh`, lines 35–39 **Vulnerability Type**: Python source-code injection **Risk Level**: High ### Vulnerable Code ```bash # Validate estimated_prob if ! python3 -c " p = float('$EST_PROB') assert 0.01 <= p <= 0.99, f'estimated_prob must be 0.01-0.99, got {p}' " 2>/dev/null; then echo "Error: estimated_prob must be between 0.01 and 0.99 (got: $EST_PROB)" exit 1 fi ``` ### Technical Analysis The attacker-controlled `EST_PROB` shell argument is interpolated directly into source code supplied to `python3 -c`. Shell quoting does not make this safe because the resulting value becomes part of the Python program rather than data passed to that program. An argument containing a closing quote and additional Python statements can terminate the intended `float()` expression and inject arbitrary Python code. The injected statement executes while validation is being performed. It can execute even if the subsequent assertion fails and the script exits. This violates the code/data separation required for safe interpreter invocation. Numeric validation occurs only after the untrusted value has already been interpreted as executable Python. ### Attack Path 1. An attacker influences the fourth argument passed to `create-market-with-odds.sh`. 2. The argument is constructed to close the single-quoted Python string and append a Python statement. 3. Bash substitutes the crafted value into the multiline `python3 -c` program. 4. Python parses the attacker-supplied content as source code. 5. The injected code executes with the permissions of the user running the Skill. 6. The code can read `~/secrets/moltmarkets-api-key`, modify user-accessible files, launch subprocesses, or make authenticated API requests. ### Impact Assessment Successful exploitation provides arbitrary code execution under the invoking user's account. The resulting process can access all files and services available to tha ...[truncated 420 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Pass the value as data through `sys.argv` instead of interpolating it into Python source: ```bash if ! python3 -c ' import sys try: probability = float(sys.argv[1]) except ValueError: raise SystemExit(1) if not 0.01 <= probability <= 0.99: raise SystemExit(1) ' "$EST_PROB"; then printf 'Error: estimated_prob must be between 0.01 and 0.99 (got: %s)\n' "$EST_PROB" >&2 exit 1 fi ``` Additional hardening should include: - Perform strict shell-level syntax validation before invoking Python, such as accepting only a documented decimal format. - Pass all external values through positional arguments, standard input, or environment variables and never concatenate them into interpreter source. - Add regression tests containing quotes, semicolons, newlines, backslashes, and Python syntax. - Run the trading scripts under a dedicated, minimally privileged account whose credential can perform only required API operations. ]]>
