T09 · Insecure Skill Coding Practices
Error
- Location
- SKILL.md:9540
- Finding
- Arbitrary Python Code Execution Through Unsafe Evaluation of Command-Line Arguments## Vulnerability Details **File Location**: `SKILL.md:9540-9552` and `SKILL.md:9571-9588` **Vulnerability Type**: Command injection through Python `eval()` **Risk Level**: High ### Vulnerable Code ```python # Broker cerebro.broker = bt.brokers.BackBroker(**eval('dict(' + args.broker + ')')) # Sizer cerebro.addsizer(bt.sizers.FixedSize, **eval('dict(' + args.sizer + ')')) # Strategy cerebro.addstrategy(St, **eval('dict(' + args.strat + ')')) # Execute cerebro.run(**eval('dict(' + args.cerebro + ')')) if args.plot: # Plot if requested to cerebro.plot(**eval('dict(' + args.plot + ')')) ``` The evaluated strings originate directly from command-line arguments: ```python parser.add_argument('--cerebro', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--broker', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--sizer', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--strat', required=False, default='', metavar='kwargs', help='kwargs in key=value format') parser.add_argument('--plot', required=False, default='', nargs='?', const='{}', metavar='kwargs', help='kwargs in key=value format') ``` ### Technical Analysis The examples concatenate user-controlled command-line strings into Python expressions and pass the resulting expressions to `eval()`. Python `eval()` is not limited to parsing dictionaries or keyword arguments. It can evaluate function calls, attribute access, imports reached through built-ins, and other expressions with side effects. The surrounding `dict(...)` expression does not provide a security boundary. An attacker who can influence one of the affected arguments can construct an expression th ...[truncated 2442 chars]
- Remediation
- ## Remediation Suggestions 1. Remove every use of `eval()` for parsing command-line or other externally supplied configuration. 2. Define explicit typed arguments with `argparse`, such as `type=int`, `type=float`, and constrained `choices`. 3. If flexible keyword arguments are required, use a non-executable format such as JSON: ```python import json broker_kwargs = json.loads(args.broker or '{}') ``` 4. Validate parsed objects before use: - Require the top-level value to be a dictionary. - Maintain an allowlist of accepted keys for each Backtrader component. - Reject nested objects unless specifically required. - Enforce expected types and safe numeric ranges. 5. Do not treat `ast.literal_eval()` as a complete fix. Although it prevents arbitrary expression execution, the resulting values still require schema, key, type, and range validation. 6. Update all duplicated examples in `SKILL.md`, `assets/`, and `references/` so the Skill cannot reproduce the vulnerable pattern from another source. 7. Add static-analysis checks that reject `eval()` and `exec()` in generated or bundled examples. 8. Add tests using malformed and adversarial argument values to verify that inputs are rejected without evaluation or side effects.
