Fix command injection vulnerability in grep_processes endpoint#11
Draft
semgrep-code-studentsca023-rgb[bot] wants to merge 1 commit into
Draft
Fix command injection vulnerability in grep_processes endpoint#11semgrep-code-studentsca023-rgb[bot] wants to merge 1 commit into
semgrep-code-studentsca023-rgb[bot] wants to merge 1 commit into
Conversation
Fix command injection vulnerability by isolating subprocess call from user input in the grep_processes endpoint.
## Changes
- Extract `subprocess.run` into a separate helper function `_get_process_list()` that accepts no parameters
- Add input validation to return an error when the `name` query parameter is missing
- Keep process filtering in pure Python, ensuring user input never reaches the subprocess call
## Why
The original code had user input (`request.args.get("name")`) and a subprocess call in the same function scope. While the user input was only used for Python-side filtering, static analysis tools like Semgrep flag this pattern as potentially dangerous because it's difficult to verify that user input won't flow into the command.
By extracting the subprocess call into a dedicated helper function with no parameters, we make it architecturally impossible for user input to influence the executed command. This satisfies the security requirement and makes the code's safety properties explicit and verifiable.
## 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.
@267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169009) from the detection rule [python.flask.os.tainted-os-command-stdlib-flask.tainted-os-command-stdlib-flask](https://semgrep.dev/r/python.flask.os.tainted-os-command-stdlib-flask.tainted-os-command-stdlib-flask).
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 by isolating subprocess call from user input in the grep_processes endpoint.
Changes
subprocess.runinto a separate helper function_get_process_list()that accepts no parametersnamequery parameter is missingWhy
The original code had user input (
request.args.get("name")) and a subprocess call in the same function scope. While the user input was only used for Python-side filtering, static analysis tools like Semgrep flag this pattern as potentially dangerous because it's difficult to verify that user input won't flow into the command.By extracting the subprocess call into a dedicated helper function with no parameters, we make it architecturally impossible for user input to influence the executed command. This satisfies the security requirement and makes the code's safety properties explicit and verifiable.
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.
@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.flask.os.tainted-os-command-stdlib-flask.tainted-os-command-stdlib-flask.