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

Merged

Conversation

pixeebot[bot]
Copy link
Contributor

@pixeebot pixeebot bot commented Jun 24, 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. We have automatically added this dependency to your project's pyproject.toml file.

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. If this change is incorrect, or if you are using another packaging system such as poetry, it may be necessary for you to manually add the dependency to the proper location in your project.

More reading

🧚🤖 Powered by Pixeebot

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

pyproject.toml Outdated
@@ -14,6 +14,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">=3.11,<3.13"
security = "==1.2.1"
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

Copy link
Contributor

coderabbitai bot commented Jun 24, 2024

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@burnettk
Copy link
Contributor

burnettk commented Jun 24, 2024

@pixeebot, this change is causing issues with mypy like:

analyzing "security": module is installed, but missing library stubs or py.typed
marker  [import-untyped]
    from security import safe_command
    ^
src/spiffworkflow_backend/services/git_service.py:15:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
src/spiffworkflow_backend/services/git_service.py: note: In member "run_shell_command" of class "GitService":
src/spiffworkflow_backend/services/git_service.py:195:13: error: Returning Any
from function declared to return "CompletedProcess[bytes] | bool" 
[no-any-return]
                return result.returncode == 0
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/spiffworkflow_backend/services/git_service.py:202:9: error: Returning Any
from function declared to return "CompletedProcess[bytes] | bool" 
[no-any-return]
            return result
            ^~~~~~~~~~~~~
Found 3 errors in 1 file (checked 277 source files)

does the python security package you wrote have type hints? it would be nice to not lose type hints with this change. thank you.

@drdavella
Copy link

@burnettk thank you very much for the feedback! The security package does include type annotations on main. I will create a new release so that it is available downstream.

@drdavella
Copy link

@burnettk burnettk merged commit 26149ae into main Jun 25, 2024
23 checks passed
@burnettk burnettk deleted the pixeebot/drip-2024-06-24-pixee-python/sandbox-process-creation branch June 25, 2024 23:24
@burnettk
Copy link
Contributor

@drdavella , thank you.

@burnettk
Copy link
Contributor

@drdavella in security lib api.py, is the print(executable_path) inside of def check supposed to be there? we started getting "random" None's on stdout when upgrading from 1.2.1 to 1.3.0. it might be coming from another line of code, but that's the first print statement i noticed in the diff.

@drdavella
Copy link

Hi @burnettk, no that looks like a leftover from debugging/development. I'll fix it and release a bugfix.

@burnettk
Copy link
Contributor

confirmed fixed in 1.3.1

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.

None yet

2 participants