Skip to content

fix: replace eval() with safe AST evaluator in calculator template#5307

Open
xr843 wants to merge 1 commit intocrewAIInc:mainfrom
xr843:fix/safe-eval-calculator
Open

fix: replace eval() with safe AST evaluator in calculator template#5307
xr843 wants to merge 1 commit intocrewAIInc:mainfrom
xr843:fix/safe-eval-calculator

Conversation

@xr843
Copy link
Copy Markdown

@xr843 xr843 commented Apr 6, 2026

Summary

Fixes #5056

  • Replace unsafe eval() call in the Calculator tool example (AGENTS.md line 773) with a safe AST-based arithmetic evaluator
  • The eval() on unvalidated LLM output is a remote code execution (RCE) vulnerability — an adversarial prompt or malicious input can execute arbitrary Python code
  • The safe_eval() function uses ast.parse() and only allows numeric literals and arithmetic operators (+, -, *, /, **, %, unary -)

Security Impact

Severity: High — Since this is a template that users copy into their projects, the vulnerable pattern propagates to every new crewAI project that uses the Calculator tool example.

Test plan

  • Verify safe_eval("2 + 3 * 4") returns 14.0
  • Verify safe_eval("__import__('os').system('whoami')") raises ValueError
  • Verify all arithmetic operators work: +, -, *, /, **, %

🤖 Generated with Claude Code

The calculator tool example used eval() on unvalidated LLM input,
enabling remote code execution. Replace with an AST-based evaluator
that only allows arithmetic operations and numeric literals.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@xr843 xr843 force-pushed the fix/safe-eval-calculator branch from 2f8613e to 839d446 Compare April 9, 2026 04:11
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.

[Security] crewai create ships template with eval() on unsanitized LLM input

1 participant