Harden calculate_reward: replace unsafe eval() with safe arithmetic evaluator#1197
Harden calculate_reward: replace unsafe eval() with safe arithmetic evaluator#11970xmrma wants to merge 5 commits intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves the security posture of the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request significantly improves the security of the calculate_reward function by replacing the unsafe eval() with a custom, safe arithmetic evaluator using Python's ast module. While successfully addressing the security risk of using eval() on untrusted input, a critical syntax error was introduced in tunix/rl/agentic/rewards/reward.py where the _REGISTRY variable declaration was split, which will prevent the module from loading. Additionally, the docstring for calculate_reward should be updated to reflect that eval() is no longer used.
|
Thanks! Addressed Gemini feedback:
PR should be ready for review/merge. |
|
All set on my side
Happy to adjust operator support/limits or move helper placement if you prefer. |
|
Friendly ping PR #1197 is ready (CLA green, no open threads). It looks like GitHub is also showing “2 workflows awaiting approval” (fork PR). If a maintainer could approve/run the workflows + take a quick look when convenient, that’d be awesome. Happy to squash commits or adjust the allowlist/limits if you prefer. |
What changed
This PR removes Python
eval()usage fromtunix/rl/agentic/rewards/reward.py::calculate_rewardand replaces it with a safe arithmetic evaluator based on an AST allowlist.Why
calculate_rewardderives an expression fromtask["question"]and previously evaluated it with full Python semantics. If tasks/questions originate from untrusted sources (shared datasets/benchmarks/task files), this can lead to unintended code execution in the context of the running process.Implementation details
_safe_eval_math()that supports:+,-+,-,*,/,//,%,**(with exponent guardrails)UnsafeExpressionErrorcalculate_rewardnow uses_safe_eval_math(expression)and safely returns score0.0on invalid/unsafe expressions (existing try/except behavior preserved)Tests
Extends
tests/rl/agentic/rewards/reward_test.pyto cover:2 + 2)__import__('os').system(...))2 ** 5000)Note: local test execution on my environment is blocked by missing
abslin an externally managed Python environment; CI should validate.Security context
This is a defensive hardening change that eliminates an
eval()injection/code-execution risk while preserving expected arithmetic behavior.