Replace bare eval() with AST-whitelist sandbox for safe expression evaluation#45
Open
yunsmall wants to merge 1 commit into
Open
Replace bare eval() with AST-whitelist sandbox for safe expression evaluation#45yunsmall wants to merge 1 commit into
yunsmall wants to merge 1 commit into
Conversation
573985b to
e59d98a
Compare
…aluation - Add safe_eval.py: AST node-by-node whitelist validation - Add whitelist.py: data-only module with allowed nodes/functions/modules - Rewrite calculator.py: use safe_eval, return errors to AI instead of crashing - Add test_sandbox.py: 390 tests covering functionality and sandbox escape attempts - Support math, random, and numpy (optional) with submodule nesting (numpy.fft.fft, etc.) - Block all dunder attribute escapes, dangerous builtins, and file I/O
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR replaces the unsafe
eval()call incalculator.pywith an AST-whitelist-based sandbox evaluator, preventing sandbox escape attacks while preserving full mathematical functionality.Problem
The original code used:
This is vulnerable to Python sandbox escape. Because
__builtins__is auto-injected by CPython, an attacker can bypass the restricted globals via:Solution
Architecture
whitelist.py(277 lines)safe_eval.py(173 lines)compile()+eval()calculator.py(44 lines)Security model
__builtins__set to empty dict — no builtins leaknumpy.fft.fftare allowed only if every intermediate segment is a registered submodule with its own whitelist{success: False, error: "..."}so the AI can self-correct instead of crashingWhat's supported
numpy.linalg.*), FFT (numpy.fft.*), random distributions (numpy.random.*)numpy.fft.fft,numpy.linalg.det, etc.)npalias fully supportedabs,min,max,sum,range,len,sorted,zip,map, etc.@matrix multiply operatoraxis=,dtype=, etc.)What's blocked
__class__,__bases__,__subclasses__,__globals__, etc.)open,eval,exec,__import__,compile,globals,getattr, etc.)numpy.load,numpy.save,numpy.fromfile, etc.)Testing
test_sandbox.py— 456 tests, all passing: