fix: harden AST blocklist in _safe_import_check for attribute-style and dynamic dispatch dangerous calls#85
Open
nobugpal wants to merge 2 commits into
Conversation
added 2 commits
July 21, 2026 23:36
…angerous calls The _safe_import_check function in python_executor.py walks the Python AST to detect dangerous function calls. While direct Name calls (eval(...), exec(...)) were already blocked, attribute-style calls (builtins.eval(...), builtins.exec(...)) parsed as ast.Attribute nodes were not covered by the same blocklist. This commit adds 'eval', 'exec', and 'compile' to the attribute blocklist, ensuring consistent coverage regardless of call style. Also adds a dedicated test suite (test_python_executor_blocklist.py) with 14 tests covering: - Direct Name calls for eval/exec/compile/__import__ - Attribute calls for builtins.eval/exec/compile - Import-based dangerous module checks - Safe operations (sum, print, len, range) remain unaffected All 68 tests pass including existing regression suites. Security note: defense-in-depth hardening for the code execution tool path, per CONTRIBUTING.md guidelines.
…nd dynamic dispatch dangerous calls The _safe_import_check function uses AST analysis to detect dangerous function calls. Previously, attribute-style calls (builtins.eval(...)) were not covered by the same blocklist as direct Name calls (eval(...)). Changes: 1. Added 'eval', 'exec', 'compile' to the ast.Attribute blocklist (closes the builtins.eval bypass path) 2. Added getattr-with-dangerous-argument detection (blocks getattr(obj, 'eval'/'exec'/...) patterns) 3. Added subscript-access detection for dangerous function names (blocks obj.__dict__['eval'] patterns) All changes are scoped to constant string arguments only, preserving legitimate dynamic getattr and subscript use. Tests: 74 pass (20 new blocklist tests + 54 existing regression tests) No regressions. Security note: defense-in-depth hardening for the code execution tool path (CWE-184). Per CONTRIBUTING.md guidelines.
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
Harden the AST-based blocklist in
python_executor.py:_safe_import_check()to cover attribute-style calls (builtins.eval(...)),getattr-based dynamic dispatch (getattr(obj, "eval")(...)), and subscript access patterns (obj.__dict__["eval"](...)), in addition to the existing direct Name call coverage.Changes
LightAgent/builtin_tools/python_executor.pyThree defenses added:
eval,exec,compileto theast.Attributeblocklist — closes thebuiltins.eval(...)bypass pathgetattr()is called with a constant second argument matching dangerous function names, the call is blocked — closesgetattr(obj, "eval")(...)patternsobj[...]) with a constant key matching dangerous function names is blocked — closesobj.__dict__["eval"](...)patternsAll pattern checks are scoped to constant string arguments only, preserving legitimate dynamic access (e.g.,
getattr(obj, variable_name)ord[variable_key]).tests/test_python_executor_blocklist.py(new — 20 tests)Testing
No regressions. All existing tests pass.
Security Note
Per CONTRIBUTING.md: this change affects the code execution tool path and includes a security hardening note. The fix closes defense-in-depth gaps (CWE-184: Incomplete Blocklist) in the Python code execution sandbox by ensuring consistent blocking regardless of call syntax or dispatch mechanism.
Checklist
python -m compileall -q LightAgentpasses