Skip to content

Avoid eval when parsing btrun kwargs - #521

Open
williamhsiao0403 wants to merge 1 commit into
mementum:masterfrom
williamhsiao0403:fix-btrun-safe-kwargs
Open

williamhsiao0403 wants to merge 1 commit into
mementum:masterfrom
williamhsiao0403:fix-btrun-safe-kwargs

Conversation

@williamhsiao0403

Copy link
Copy Markdown

Summary

Replaces eval() usage in backtrader.btrun when parsing CLI kwargs with a safer AST-based parser.

The previous implementation evaluated user-controlled command-line input such as:

  • --cerebro
  • --writer
  • --plot
  • strategy / indicator / observer / analyzer kwargs
  • hook kwargs

This keeps the existing key=value syntax for literal values while rejecting function calls and other executable Python expressions.

Changes

  • Add parse_kwargs() using ast.parse + ast.literal_eval
  • Replace eval('dict(...)') calls in btrun.py
  • Add regression tests for literal parsing and rejected code execution

Verification

Tested with Python 3.13.1:

python tests/test_btrun.py
python -m compileall -q backtrader/btrun tests/test_btrun.py
python -m bandit -r backtrader/btrun -s B311

pip-audit also reported no known vulnerabilities.

Parse btrun key=value arguments with ast and literal values instead of evaluating arbitrary Python code.

This keeps the existing CLI kwargs syntax for literal values while rejecting function calls such as __import__().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant