Fix command injection and sandbox escape in CodeInterpreterTool#4643
Open
Ratnaditya-J wants to merge 2 commits intocrewAIInc:mainfrom
Open
Fix command injection and sandbox escape in CodeInterpreterTool#4643Ratnaditya-J wants to merge 2 commits intocrewAIInc:mainfrom
Ratnaditya-J wants to merge 2 commits intocrewAIInc:mainfrom
Conversation
Fix two security vulnerabilities in CodeInterpreterTool:
1. Command injection (CWE-78): Replace os.system(f"pip install {library}")
with subprocess.run(["pip", "install", library]) to prevent shell injection
via crafted library names.
2. Sandbox escape (CWE-94): Block __class__, __bases__, __subclasses__,
__mro__, and __qualname__ attribute access in SandboxPython to prevent
introspection-based escape. Also block getattr, setattr, delattr, and
breakpoint builtins.
Fixes crewAIInc#4516
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 3 potential issues.
Bugbot Free Tier Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
lib/crewai-tools/src/crewai_tools/tools/code_interpreter_tool/code_interpreter_tool.py
Show resolved
Hide resolved
lib/crewai-tools/src/crewai_tools/tools/code_interpreter_tool/code_interpreter_tool.py
Outdated
Show resolved
Hide resolved
Ratnaditya-J
pushed a commit
to Ratnaditya-J/RealBenchFramework
that referenced
this pull request
Feb 28, 2026
Addresses all 3 review comments on crewAIInc/crewAI#4643: HIGH: Replace substring matching with AST-based attribute detection in _check_restricted_attrs to prevent __getattribute__ string concatenation bypass. Add __getattribute__ and __getattr__ to RESTRICTED_ATTRS. MEDIUM: Wrap subprocess.run(check=True) in try/except for CalledProcessError so failed pip installs return an error message. LOW: Remove unused `call` import from test file. Also adds 4 new tests: __getattribute__ bypass, __getattr__ blocking, CalledProcessError handling, and AST-vs-substring behavior verification. Apply with: cd crewAI && git apply ../RealBenchFramework/crewai-pr-4643-review-fixes.patch https://claude.ai/code/session_01G7RS6SYfFXijkuAGC9Jr2G
- Block __getattribute__ and __getattr__ in sandbox RESTRICTED_ATTRS to prevent escape via string concatenation bypass - Wrap subprocess.run pip install in try/except CalledProcessError to return helpful error instead of propagating uncaught exception - Remove unused `call` import from test file
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.
Fixes the two security issues reported in #4516.
The first issue is command injection in pip install. The library name was interpolated into an os.system() call, so a crafted library name like "numpy; rm -rf /" could execute arbitrary commands. Fixed by switching to subprocess.run() with the arguments as a list, which prevents shell interpretation of special characters.
The second issue is sandbox escape via subclasses() introspection. Python's object model lets you walk up from any object to builtins through ().class.bases[0].subclasses(), which gives access to every loaded class including importers that can load blocked modules like os. Fixed by checking submitted code for class, bases, subclasses, mro, and qualname before execution, and also blocking getattr, setattr, delattr, and breakpoint from the sandbox builtins to close related bypass paths.
Both fixes include new tests. All 20 tests in the code interpreter test file pass, including the 9 pre-existing ones.
Note
Medium Risk
Touches code-execution paths and sandbox restrictions; it may break previously-working user snippets that rely on blocked builtins/attributes, but reduces remote-code-execution risk.
Overview
Closes a command-injection vector in
run_code_unsafeby replacingos.system("pip install ...")withsubprocess.run(["pip","install",...], check=True)and returning a clear error when installs fail.Hardens the restricted sandbox by removing additional attribute-manipulation builtins (e.g.,
getattr/setattr) and rejecting code containing high-risk dunder attribute names (e.g.,__class__,__subclasses__) before execution; adds focused tests covering both fixes and ensuring normal sandboxed code still runs.Written by Cursor Bugbot for commit 9752ff1. This will update automatically on new commits. Configure here.