fix: replace eval() with safe AST-based math evaluator in AGENTS.md template#5058
fix: replace eval() with safe AST-based math evaluator in AGENTS.md template#5058devin-ai-integration[bot] wants to merge 1 commit intomainfrom
Conversation
…emplate The Calculator tool example in the AGENTS.md template used eval() on unsanitized LLM input, creating a remote code execution vulnerability in every new CrewAI project. Replace eval() with an AST-based evaluator that only supports arithmetic operators (+, -, *, /, **) and numeric literals, preventing arbitrary code execution while preserving calculator functionality. Closes #5056 Co-Authored-By: João <joao@crewai.com>
|
Prompt hidden (unlisted session) |
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
The AST-walking approach looks solid, whitelist-only on Two minor notes:
Nice turnaround on the fix. Thanks for the quick response. Heady |
|
@joaomdmoura Both PRs (#5058, #5059) have passing tests and review comments. Would appreciate a review when you get a chance. Also happy to discuss integrating agent-audit as a CI security check for incoming PRs if that's of interest. |
Summary
Fixes #5056 — The Calculator tool example in the
AGENTS.mdtemplate (shipped viacrewai create) usedeval()on unsanitized LLM input, creating a remote code execution vulnerability in every newly scaffolded project.Replaced
eval(expression)with an AST-walking evaluator that only permits arithmetic operators (+,-,*,/,**) and numeric literals (int,float). No new dependencies required.Added 28 tests that extract the calculator source directly from the markdown template and verify:
ValueErrororSyntaxError(14 cases)eval()(2 sanity checks)Review & Testing Checklist for Human
_safe_evalfunction's whitelist approach (onlyast.Constant,ast.BinOp,ast.UnaryOpwith explicit operator map) cannot be bypassed. This is the core security claim.@tooldecorator example, or if a simpler safe alternative (or removing the calculator entirely) would be better for the template's purpose.exec()it. This keeps tests in sync with the template but is fragile to markdown structural changes (heading renames, code block reformatting). Decide if this coupling is acceptable.crewai create crew test_projectand verify the generatedAGENTS.mdcontains the new safe calculator example.Notes
%) and floor division (//) — reasonable for a minimal example but worth noting.ZeroDivisionErroris not caught; it will bubble up naturally fromoperator.truediv, which is acceptable behavior.Link to Devin session: https://app.devin.ai/sessions/a69dae035ed249adb723d4efc6aec062