T09 · Insecure Skill Coding Practices
- Location
SKILL.md:9540- Finding
Arbitrary Python Code Execution Through Unsafe Evaluation of Command-Line Arguments
- Content
View full analysis
Vulnerability Details
File Location:
SKILL.md:9540-9552andSKILL.md:9571-9588
Vulnerability Type: Command injection through Pythoneval()
Risk Level: HighVulnerable 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(). Pythoneval()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
View remediation
Remediation Suggestions
- Remove every use of
eval()for parsing command-line or other externally supplied configuration. - Define explicit typed arguments with
argparse, such astype=int,type=float, and constrainedchoices. - If flexible keyword arguments are required, use a non-executable format such as JSON:
python import json broker_kwargs = json.loads(args.broker or '{}') - 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.
- 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. - Update all duplicated examples in
SKILL.md,assets/, andreferences/so the Skill cannot reproduce the vulnerable pattern from another source. - Add static-analysis checks that reject
eval()andexec()in generated or bundled examples. - Add tests using malformed and adversarial argument values to verify that inputs are rejected without evaluation or side effects.
- Remove every use of
