Prevent Command Injection in cmd_lab - #23364
Open
pixee-integration-test[bot] wants to merge 1 commit into
Open
Conversation
|
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.



Pixee Scan: 42d5e1fa-8134-47c0-8a1e-da7f97c83698
Confidence: HIGH
Fix confidence is a rating derived from an internal benchmark and includes High, Medium, and Low confidence fixes. It comprises three weighted scores reflecting the safety, effectiveness and cleanliness of Pixee's code changes within a fix. View Details in Pixee.
Remediation
This change fixes finding AZVdjxO-h0BHfERFoIIK.
Details
Prevent Command Injection in cmd_lab
Summary
Prevent Command Injection in the cmd_lab view in introduction/views.py.
Vulnerability Description
Command Injection happens when an application passes attacker-controlled data into an operating system command without strict validation. If the shell interprets that data, an attacker can append extra commands or metacharacters and execute arbitrary code with the application's privileges. That can lead to data theft, system tampering, or full host compromise.
Changes Made
The
cmd_labview inintroduction/views.pypreviously insertedrequest.POST.get('domain')directly intonslookupordigand executed it withsubprocess.Popen(..., shell=True), which allowed the shell to interpret user input. The fix added input checks that reject missing values and normalize the supplied domain by trimming whitespace, removing an optional scheme andwww, and stripping any path, query, fragment, or trailing dot. It then validates the remaining value against a strict domain-name regular expression before continuing. The command is now built as an argument list, `[Guidance Adherence
Source: Pixee Knowledge Base
Applied the following guidance from Sonar remediation guidance for pythonsecurity:S2076:
domain, strips URL prefixes/path fragments, and enforces a strict whitelist regex for valid domain names before any command is built. This follows the guidance to ensure user-controlled data does not contain malicious commands."nslookup {}".format(domain)/"dig {}".format(domain)) to argv-style lists (["nslookup", domain]/["dig", domain]) and removedshell=True, which aligns with the remediation goal of avoiding shell-based command construction and command injection.re,subprocess) already available in the file, so no manifest update was needed.Overall, the patch matches the security guidance and uses the standard safe subprocess pattern with input validation.