Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sandbox Process Creation #780

Conversation

pixeebot[bot]
Copy link
Contributor

@pixeebot pixeebot bot commented Sep 3, 2024

This codemod sandboxes all instances of subprocess.run and subprocess.call to offer protection against attack.

Left unchecked, subprocess.run and subprocess.call can execute any arbitrary system command. If an attacker can control part of the strings used as program paths or arguments, they could execute arbitrary programs, install malware, and anything else they could do if they had a shell open on the application host.

Our change introduces a sandbox which protects the application:

  import subprocess
+ from security import safe_command
  ...
- subprocess.run("echo 'hi'", shell=True)
+ safe_command.run(subprocess.run, "echo 'hi'", shell=True)
  ...
- subprocess.call(["ls", "-l"])
+ safe_command.call(subprocess.call, ["ls", "-l"])

The default safe_command restrictions applied are the following:

  • Prevent command chaining. Many exploits work by injecting command separators and causing the shell to interpret a second, malicious command. The safe_command functions attempt to parse the given command, and throw a SecurityException if multiple commands are present.
  • Prevent arguments targeting sensitive files. There is little reason for custom code to target sensitive system files like /etc/passwd, so the sandbox prevents arguments that point to these files that may be targets for exfiltration.

There are more options for sandboxing if you are interested in locking down system commands even more.

Dependency Updates

This codemod relies on an external dependency. However, we were unable to automatically add the dependency to your project.

This library holds security tools for protecting Python API calls.

There are a number of places where Python project dependencies can be expressed, including setup.py, pyproject.toml, setup.cfg, and requirements.txt files. You may need to manually add this dependency to the proper location in your project.

Manual Installation

For setup.py:

 install_requires=[
+    "security==1.3.1",
 ],

For pyproject.toml (using setuptools):

 [project]
 dependencies = [
+    "security==1.3.1",
 ]

For setup.cfg:

 [options]
 install_requires =
+    security==1.3.1

For requirements.txt:

+security==1.3.1

For more information on adding dependencies to setuptools projects, see the setuptools documentation.

If you are using another build system, please refer to the documentation for that system to determine how to add dependencies.

More reading

🧚🤖 Powered by Pixeebot

Feedback | Community | Docs | Codemod ID: pixee:python/sandbox-process-creation

@pixeebot pixeebot bot requested a review from a team as a code owner September 3, 2024 05:58
Copy link
Contributor Author

pixeebot bot commented Sep 11, 2024

I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it?

If this change was not helpful, or you have suggestions for improvements, please let me know!

Copy link
Contributor Author

pixeebot bot commented Sep 12, 2024

Just a friendly ping to remind you about this change. If there are concerns about it, we'd love to hear about them!

Copy link
Contributor Author

pixeebot bot commented Sep 18, 2024

This change may not be a priority right now, so I'll close it. If there was something I could have done better, please let me know!

You can also customize me to make sure I'm working with you in the way you want.

@pixeebot pixeebot bot closed this Sep 18, 2024
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.

0 participants