Prevent Command Injection in introduction/views.py - #23367
Open
pixee-integration-test[bot] wants to merge 1 commit into
Open
Prevent Command Injection in introduction/views.py#23367pixee-integration-test[bot] wants to merge 1 commit into
pixee-integration-test[bot] wants to merge 1 commit into
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: 115f7fab-c7b6-48c8-9833-129de3641014
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 introduction/views.py
Summary
Prevent command injection in
introduction/views.py.Vulnerability Description
Command Injection occurs when an application builds an operating system command from untrusted input and passes it to a shell or command interpreter. An attacker can abuse this pattern to run arbitrary commands with the privileges of the application, which can lead to data exposure, system compromise, or lateral movement. The risk is especially high when user input is inserted directly into a command string without strict validation.
Changes Made
The vulnerable code in
introduction/views.pybuiltnslookupanddigcommands by formatting the user-controlleddomainvalue into a shell command and executing it withsubprocess.Popen(..., shell=True). That pattern allowed attacker-supplied metacharacters indomainto be interpreted by the OS shell. The fix added server-side validation fordomainusingipaddress.ip_addressand a hostname allowlist regex, rejecting invalid values with an "Invalid domain" response before any subprocess call.The command execution path was also changed to pass an argument list, `[\
Guidance Adherence
Source: Pixee Knowledge Base
Applied the following guidance from the remediation guidance section for pythonsecurity:S2076:
domainbefore execution, usingipaddress.ip_address(domain)and a regexfullmatch(...)fallback, and rejects invalid input withInvalid domain. This matches the guidance’s requirement to validate/sanitize user-controlled data so it cannot carry malicious command content."nslookup {}".format(domain)/"dig {}".format(domain)and instead passes argument lists['nslookup', domain]/['dig', domain]tosubprocess.run(..., shell=False, capture_output=True, text=True). This is the correct command-injection mitigation because it avoids shell interpretation entirely.ipaddress,re) already available in the runtime, so there was no manifest update needed.