Skip to content

Mitigating command injection - security issue #1549

@mkirkeng-leukeleu

Description

@mkirkeng-leukeleu

Hey,

we've been looking at possibly adopting PyInfra for infrastructure management and one aspect is of this security, especially for a tool that will be running numerous shell commands on production servers. I couldn't find anything on how security is handled in this project.

Given the nature of the tool, a main concern is shell/command injection. Looking around the code I see there are a lot of cases of commands being created by simply formatting user input into them in operations and this also not being handled at a lower layer (when the command is actually executed.

Experimenting also seems to indicate that this is not mitigated properly. The snippet below demonstrates the problem. It is a bit of a contrived example of course but you can very much imagine getting the value of an operation argument from a config file or other source. So an attacker could then add a command-injected value in said configuration elsewhere which would cause the command to be executed on the host, without it being obvious from the users pyinfra code that this might happen.

Because this is not just an issue for the server.shell operation (where the user might expect these risks to be present and mitigate it themselves) but also for other non-command arguments of operations, there is a high chance of the user not being aware of these risks.

# inventory.py
my_hosts = [
    ("@local", {"requirements_txt_path": "requirements.txt; rm -rf /"}),
]

# deploy.py
from pyinfra.operations import pip
from pyinfra import host

pip.packages(
    name="Install packages",
    virtualenv=".venv",
    requirements=host.data.get("requirements_txt_path"),
)

# running `pyinfra inventory.py deploy.py -y` would now try to delete the root folder on localhost.

So I'm wondering is there any global system or development standard that should prevent this, unittests that should catch these kinds of issues or point on a roadmap to address these issues? Or am I maybe just missing something in my research here (very possible 😄).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions