Fix command injection vulnerability in grep_processes endpoint#9
Merged
Conversation
Fix command injection vulnerability in the `/grep_processes` endpoint by removing `shell=True` and handling filtering in Python. ## Changes - Removed `shell=True` from `subprocess.run()` call - Changed command from shell string to safe argument list `["ps", "aux"]` - Replaced shell-based `grep` and `awk` pipeline with Python string filtering - Added input validation to check if `name` parameter is provided ## Why The original code concatenated untrusted user input (`name` parameter) directly into a shell command with `shell=True`. This allowed attackers to inject arbitrary shell commands (e.g., `; rm -rf /` or `$(malicious_command)`). By removing `shell=True` and passing arguments as a list, the subprocess module no longer invokes a shell interpreter, eliminating the command injection vector. The filtering logic is now performed safely in Python. ## Semgrep Finding Details Untrusted input might be injected into a command executed by the application, which can lead to a command injection vulnerability. An attacker can execute arbitrary commands, potentially gaining complete control of the system. To prevent this vulnerability, avoid executing OS commands with user input. If this is unavoidable, validate and sanitize the input, and use safe methods for executing the commands. Untrusted input in a command can lead to command injection, allowing attackers to execute arbitrary commands and gain control of the system. To prevent this: 1. Avoid direct command execution: Don't run OS commands with user input directly. 2. Validate and sanitize input: Ensure input is safe by removing or escaping dangerous characters. 3. (preferred) Use safe methods: Use `subprocess.run` without `shell=True` to safely execute commands, as it doesn't call a system shell by default. If `shell=True` is necessary, properly quote and escape all input to prevent shell injection. This is a secure by default approach. @267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169010) from the detection rule [python.flask.os.tainted-os-command-stdlib-flask-secure-default.tainted-os-command-stdlib-flask-secure-default](https://semgrep.dev/r/python.flask.os.tainted-os-command-stdlib-flask-secure-default.tainted-os-command-stdlib-flask-secure-default).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix command injection vulnerability in the
/grep_processesendpoint by removingshell=Trueand handling filtering in Python.Changes
shell=Truefromsubprocess.run()call["ps", "aux"]grepandawkpipeline with Python string filteringnameparameter is providedWhy
The original code concatenated untrusted user input (
nameparameter) directly into a shell command withshell=True. This allowed attackers to inject arbitrary shell commands (e.g.,; rm -rf /or$(malicious_command)).By removing
shell=Trueand passing arguments as a list, the subprocess module no longer invokes a shell interpreter, eliminating the command injection vector. The filtering logic is now performed safely in Python.Semgrep Finding Details
Untrusted input might be injected into a command executed by the application, which can lead to a command injection vulnerability. An attacker can execute arbitrary commands, potentially gaining complete control of the system.
To prevent this vulnerability, avoid executing OS commands with user input. If this is unavoidable, validate and sanitize the input, and use safe methods for executing the commands.
Untrusted input in a command can lead to command injection,
allowing attackers to execute arbitrary commands and gain
control of the system. To prevent this:
with user input directly.
removing or escaping dangerous characters.
subprocess.runwithout
shell=Trueto safely execute commands, as itdoesn't call a system shell by default. If
shell=Trueis necessary, properly quote and escape all input to prevent
shell injection. This is a secure by default approach.
@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.flask.os.tainted-os-command-stdlib-flask-secure-default.tainted-os-command-stdlib-flask-secure-default.