Prevent OS Command Injection in cmd_lab View - #23358
Open
pixee-integration-test[bot] wants to merge 1 commit into
Open
Prevent OS Command Injection in cmd_lab View#23358pixee-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: 1870f3a5-f3b9-4f7c-b41a-6d920a86b611
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 OS Command Injection in cmd_lab View
Summary
Removed OS command injection in
introduction/views.py'scmd_labview by validating the domain and avoiding shell-based command construction.Vulnerability Description
OS Command Injection happens when an application passes attacker-controlled data to a shell interpreter without strict validation. Because the shell treats metacharacters and separators as executable syntax, an attacker can append or alter commands instead of supplying only the intended argument. This can lead to arbitrary command execution with the application's privileges, data theft, file tampering, or broader system compromise.
Changes Made
The
cmd_labview inintroduction/views.pypreviously builtnslookupordigcommands directly fromrequest.POST['domain']and executed them withsubprocess.Popen, which made shell injection possible if the input contained malicious characters. The fix normalized the submitted domain, stripped scheme andwwwprefixes, removed trailing dots, and then validated the value as either a literal IP address or a strict hostname before any command was built. If the value did not match those rules, the view now returnsInvalid domaininstead of executing a command. The command invocation was changed from a formatted string to an argument list,['nslookup', domain]or['dig', domain], andshell=Truewas removed so the OS shell can no longer interpret user-controlled data.Guidance Adherence
Source: Pixee Knowledge Base
Applied the following guidance from the remediation instructions:
domain, removes schemes/www, rejects invalid values withipaddress.ip_address(...)plus a hostname regex, and returns"Invalid domain"for bad input, which directly implements the guidance to prevent malicious command content."nslookup {}".format(domain)/"dig {}".format(domain)) with argument lists (['nslookup', domain]/['dig', domain]) and removesshell=True, which aligns with the secure command-execution approach implied by the guidance and eliminates shell injection risk.ipaddressimport is from Python’s stdlib, so no external dependency was introduced and no manifest update was required.The fix is therefore aligned with the security guidance and follows the expected secure coding pattern for command execution. It also remains stylistically consistent with the surrounding Python/Django code (same function structure and response rendering pattern).