VAITP Dataset

← Back to the dataset

CVE-2022-24065

Cookiecutter < 2.1.1: Command injection via "checkout" param, allows arbitrary command execution

  • CVSS 9.8
  • CWE-78
  • Input Validation and Sanitization
  • Remote

The package cookiecutter before 2.1.1 are vulnerable to Command Injection via hg argument injection. When calling the cookiecutter function from Python code with the checkout parameter, it is passed to the hg checkout command in a way that additional flags can be set. The additional flags can be used to perform a command injection.

CVSS base score
9.8
Published
2022-06-08
OWASP
A03 Injection
Orthogonal defect classification
Function
Code defect classification
Incorrect Functionality
Category
Input Validation and Sanitization
Subcategory
Command Injection
Accessibility scope
Remote
Impact
Arbitrary Code Execution
Affected component
cookiecutter
Fixed by upgrading
Yes

Solution

Update cookiecutter to version 2.1.1 or higher.

Vulnerable code sample

import subprocess

def vulnerable_cookiecutter(template, checkout=None):
    command = ["hg", "checkout"]
    if checkout:
        command.append(checkout)  # Directly appending user input to the command

    # Execute the command
    subprocess.run(command, check=True)

# Example usage
vulnerable_cookiecutter("my_template", checkout="valid-checkout; rm -rf /")  # Potential command injection

Patched code sample

import subprocess

def safe_cookiecutter(template, checkout=None):
    # Validate the checkout parameter to prevent command injection
    if checkout and not is_valid_checkout(checkout):
        raise ValueError("Invalid checkout parameter")

    command = ["hg", "checkout"]
    if checkout:
        command.append(checkout)

    # Execute the command safely
    subprocess.run(command, check=True)

def is_valid_checkout(checkout):
    # Implement validation logic (e.g., allow only specific formats or values)
    return isinstance(checkout, str) and all(c.isalnum() or c in ['-', '_'] for c in checkout)

# Example usage
try:
    safe_cookiecutter("my_template", checkout="valid-checkout")
except ValueError as e:
    print(e)

Published fix

Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.

Cite this entry

@misc{vaitp:cve202224065,
  title        = {{Cookiecutter < 2.1.1: Command injection via "checkout" param, allows arbitrary command execution}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2022},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2022-24065},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2022-24065/}}
}

Download this entry as JSON Download the whole dataset

Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.

The supreme art of war is to subdue the enemy without fighting.

Sun Tzu – “The Art of War”

:: Shaping the future through research and ingenuity ::

Before you use the VAITP AI models

Please read and accept the following disclaimer:

By choosing "I understand and accept", you acknowledge that you understand and agree to these terms.