Skip to content

fix: replace os.system with subprocess.run in unsafe mode pip install#4891

Merged
greysonlalonde merged 3 commits intomainfrom
fix/unsafe-mode-command-injection
Mar 16, 2026
Merged

fix: replace os.system with subprocess.run in unsafe mode pip install#4891
greysonlalonde merged 3 commits intomainfrom
fix/unsafe-mode-command-injection

Conversation

@theCyberTech
Copy link
Member

@theCyberTech theCyberTech commented Mar 15, 2026

Eliminates shell injection risk (A05) where a malicious library name like "pkg; rm -rf /" could execute arbitrary host commands. Using list-form subprocess.run with shell=False ensures the library name is always treated as a single argument with no shell metacharacter expansion.

Adds two tests: one verifying list-form invocation, one verifying that shell metacharacters in a library name cannot trigger shell execution.


Note

Low Risk
Low risk: limited to how dependencies are installed in unsafe_mode, plus targeted tests; primary behavior change is reduced command-injection surface area.

Overview
Hardens CodeInterpreterTool.run_code_unsafe by replacing os.system("pip install ...") with list-form subprocess.run([sys.executable, "-m", "pip", "install", ...]), avoiding shell parsing of user-provided library names.

Adds tests asserting subprocess.run is invoked without a shell and that library strings containing shell metacharacters are passed as a single argument (no command injection).

Written by Cursor Bugbot for commit d8b489f. This will update automatically on new commits. Configure here.

Eliminates shell injection risk (A05) where a malicious library name like
"pkg; rm -rf /" could execute arbitrary host commands. Using list-form
subprocess.run with shell=False ensures the library name is always treated
as a single argument with no shell metacharacter expansion.

Adds two tests: one verifying list-form invocation, one verifying that
shell metacharacters in a library name cannot trigger shell execution.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 15, 2026 07:39
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens CodeInterpreterTool unsafe-mode dependency installation by replacing os.system with subprocess.run list-form invocation to eliminate shell metacharacter expansion, and adds regression tests to prevent reintroduction of shell injection.

Changes:

  • Replace os.system("pip install ...") with subprocess.run([...]) in unsafe mode to avoid shell injection.
  • Add tests asserting list-form subprocess.run is used and that shell metacharacters remain a single argument.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/crewai-tools/src/crewai_tools/tools/code_interpreter_tool/code_interpreter_tool.py Switches unsafe-mode installs to subprocess.run to prevent shell parsing.
lib/crewai-tools/tests/tools/test_code_interpreter_tool.py Adds tests to verify safe invocation form and non-execution of shell metacharacters.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

S607 flags partial executable paths like ["pip", ...]. Using
[sys.executable, "-m", "pip", ...] provides an absolute path and also
ensures installation targets the correct Python environment.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@theCyberTech theCyberTech force-pushed the fix/unsafe-mode-command-injection branch from 9c0649e to c1c2572 Compare March 15, 2026 15:40
@greysonlalonde greysonlalonde merged commit 9acb327 into main Mar 16, 2026
46 checks passed
@greysonlalonde greysonlalonde deleted the fix/unsafe-mode-command-injection branch March 16, 2026 06:04
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.

3 participants