Skip to content

Fix command injection and sandbox escape in CodeInterpreterTool#4643

Open
Ratnaditya-J wants to merge 2 commits intocrewAIInc:mainfrom
Ratnaditya-J:fix/code-interpreter-security
Open

Fix command injection and sandbox escape in CodeInterpreterTool#4643
Ratnaditya-J wants to merge 2 commits intocrewAIInc:mainfrom
Ratnaditya-J:fix/code-interpreter-security

Conversation

@Ratnaditya-J
Copy link

@Ratnaditya-J Ratnaditya-J commented Feb 28, 2026

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_unsafe by replacing os.system("pip install ...") with subprocess.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.

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
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant