Skip to content

fix(pyinfra): command injection in server.home fact #1641

@johnny-official

Description

@johnny-official

Description

The Home fact constructs a shell command using an f-string with the user argument directly (f"echo ~{user}"). If the user argument is controlled by an attacker (e.g., via inventory data or a custom operation), they can inject arbitrary shell commands. The ~ expansion happens in the shell, meaning input like "; rm -rf /" would be interpreted as a separate command, leading to remote code execution on the target host.

File: src/pyinfra/facts/server.py

Suggested Fix

Use shlex.quote to properly escape the user input when constructing the shell command. For retrieving a user's home directory, getent passwd is a more robust and safer approach than relying on shell ~ expansion.

Metadata

Metadata

Labels

bugLabel for all kind of bugs.factsIssues with facts.

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions