OS command injection High

Constructing operating system or shell commands with unsanitized user input can lead to inadvertently running malicious code.

Detector ID
python/os-command-injection@v1.0
Category

Noncompliant example

1def exec_command_noncompliant():
2    from paramiko import client
3    from flask import request
4    address = request.args.get("address")
5    cmd = "ping -c 1 %s" % address
6    client = client.SSHClient()
7    client.connect("ssh.samplehost.com")
8    # Noncompliant: address argument is not sanitized.
9    client.exec_command(cmd)

Compliant example

1def exec_command_compliant():
2    from paramiko import client
3    from flask import request
4    address = request.args.get("address")
5    # Compliant: address argument is sanitized (shell-escaped).
6    address = shlex.quote(request.args.get("address"))
7    cmd = "ping -c 1 %s" % address
8    client = client.SSHClient()
9    client.connect("ssh.samplehost.com")
10    client.exec_command(cmd)