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 #6

Merged

Conversation

pixeebot[bot]
Copy link
Contributor

@pixeebot pixeebot bot commented Dec 19, 2023

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.

💡 This codemod adds a dependency to your project. Currently we add the dependency to a file named `requirements.txt` if it exists in your project.

There are a number of other places where Python project dependencies can be expressed, including `setup.py`, `pyproject.toml`, and `setup.cfg`. We are working on adding support for these files, but for now you may need to update these files manually before accepting this change.
More reading

Powered by: pixeebot (codemod ID: pixee:python/sandbox-process-creation)

@@ -15,6 +15,7 @@ dependencies = [
"click",
"prompt-toolkit",
"questionary~=2.0.0",
"security~=1.2.0",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This library holds security tools for protecting Python API calls.

License: MITOpen SourceMore facts

@drdavella drdavella force-pushed the pixeebot/drip-2023-12-19-pixee-python/sandbox-process-creation branch from 25321ac to 54bd876 Compare December 20, 2023 16:41
Copy link

sonarcloud bot commented Dec 20, 2023

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@drdavella drdavella merged commit 9c1054d into main Dec 20, 2023
3 checks passed
@pixeebot pixeebot bot deleted the pixeebot/drip-2023-12-19-pixee-python/sandbox-process-creation branch December 20, 2023 16:42
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.

2 participants