Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Uncontrolled command line

ID: py/command-line-injectionKind: path-problemSecurity severity: 9.8Severity: errorPrecision: highTags:   - correctness   - security   - external/cwe/cwe-078   - external/cwe/cwe-088Query suites:   - python-code-scanning.qls   - python-security-extended.qls   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

Code that passes user input directly toexec,eval, or some other library routine that executes a command, allows the user to execute malicious code.

Recommendation

If possible, use hard-coded string literals to specify the command to run or the library to load. Instead of passing the user input directly to the process or library function, examine the user input and then choose among hard-coded string literals.

If the applicable libraries or commands cannot be determined at compile time, then add code to verify that the user input string is safe before using it.

Example

The following example shows two functions. The first is unsafe as it takes a shell script that can be changed by a user, and passes it straight tosubprocess.call() without examining it first. The second is safe as it selects the command from a predefined allowlist.

urlpatterns=[# Route to command_executionurl(r'^command-ex1$',command_execution_unsafe,name='command-execution-unsafe'),url(r'^command-ex2$',command_execution_safe,name='command-execution-safe')]COMMANDS={"list":"ls","stat":"stat"}defcommand_execution_unsafe(request):ifrequest.method=='POST':action=request.POST.get('action','')#BAD -- No sanitizing of inputsubprocess.call(["application",action])defcommand_execution_safe(request):ifrequest.method=='POST':action=request.POST.get('action','')#GOOD -- Use an allowlistsubprocess.call(["application",COMMANDS[action]])

References


[8]ページ先頭

©2009-2025 Movatter.jp