Command coming from incoming request

This page is not yet available in Spanish. We are working on its translation.
If you have any questions or feedback about our current translation project, feel free to reach out to us!

Metadata

ID: python-django/subprocess-from-request

Language: Python

Severity: Error

Category: Security

CWE: 20

Description

Execute a process using unsanitized and unvalidated user-inputs. The user data should be sanitized and validated before being used to launch a new process.

Learn More

Non-Compliant Code Examples

import subprocess

def execute_command(request):
    cmd = request.GET.get("cmd")
    print("foobar")
    subprocess.run(cmd)
    subprocess.call(cmd)
    subprocess.capture_output(cmd)
    subprocess.call(["bash", cmd])

    bli = subprocess.run(cmd)
    bla = subprocess.call(cmd)
    ble = subprocess.capture_output(cmd)
    blo = subprocess.call(["bash", cmd])
    blip = subprocess.call("bash {0}".format(cmd))
    blop = subprocess.call("bash " + cmd)

Compliant Code Examples

import subprocess

def execute_command(request):
    cmd = request.GET.get("cmd")
    print("foobar")
    subprocess.run(shlex.escape(cmd))
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis