T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/excel.py:1772
- Finding
- Arbitrary Python Code Execution Through Unsafe Data Parsing<![CDATA[ ## Vulnerability Details **File Location**: `scripts/excel.py:1772-1776` **Vulnerability Type**: Unsafe use of `eval()` on attacker-controlled command-line input **Risk Level**: High ### Complete Code Snippet ```python def ld(s): try: return json.loads(s) except: try: return eval(s) except: return {} ``` The active `main_v3()` command handlers subsequently pass the user-controlled `--data` argument to this function: ```python if args.cmd == "create": path = X().from_dict(ld(args.data), args.sheet).save(args.output) elif args.cmd == "report": d = ld(args.data) ``` The same unsafe parsing pattern also appears in the legacy command handler at `scripts/excel.py:887-891`. ### Technical Analysis The parser first attempts to decode the supplied value as JSON. If JSON parsing fails, it passes the original string directly to Python's `eval()` function. Unlike a data parser, `eval()` evaluates arbitrary Python expressions in the context of the running process. The `--data` parameter is directly controllable by anyone able to invoke or influence invocation of the Skill. The `create`, `report`, and `dashboard` commands in the active `main_v3()` implementation call `ld(args.data)` without validation or sandboxing. Consequently, a malformed JSON value containing a Python expression becomes a code-execution payload. Catching exceptions does not provide security because side effects can occur before the expression returns or raises an exception. ### Attack Path 1. An attacker influences the `--data` argument passed to `scripts/excel.py`. 2. The attacker supplies text that is not valid JSON but is a valid Python expression, for example: ```bash python3 scripts/excel.py create \ --data '__import__("os").system("id > /tmp/eval-proof")' \ --output out.xlsx ``` 3. `json.loads()` rejects the value. 4. Execution enters the fallback and evaluates the payload with `eval(s)`. 5. The operating-system command runs w ...[truncated 948 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Remove every `eval()` fallback, including the duplicate implementation at `scripts/excel.py:887-891`. 2. Require strict JSON and return a clear error when parsing fails: ```python def ld(value): try: data = json.loads(value) except json.JSONDecodeError as exc: raise ValueError(f"Invalid JSON data: {exc}") from exc if not isinstance(data, dict): raise ValueError("Input data must be a JSON object") return data ``` 3. Validate the resulting schema before processing it. Require string keys and expected value types, such as arrays of supported scalar values. 4. If support for Python literals is absolutely necessary, use `ast.literal_eval()` rather than `eval()`, followed by the same strict schema validation. Strict JSON is preferable. 5. Add tests proving that expressions involving `__import__`, function calls, attribute access, and comprehensions are rejected without side effects. 6. Place limits on input size, number of columns, and number of rows to reduce memory-exhaustion risks. ]]>
