VAITP Dataset

← Back to the dataset

CVE-2026-54656

datamodel-code-generator allows code execution via –extra-template-data.

  • CVSS 7.8
  • 94
  • Input Validation and Sanitization
  • Local

datamodel-code-generator generates Pydantic v2 models, dataclasses, TypedDict, and msgspec.Struct from OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, and raw JSON, YAML, or CSV. From 0.52.1 until 0.60.2, datamodel-code-generator interpolates validators from –extra-template-data in src/datamodel_code_generator/model/pydantic_v2/base_model.py through _process_validators into @field_validator decorators without safe validation, allowing Python code execution when the generated Pydantic v2 model is imported. This issue is fixed in version 0.60.2.

CWE
94
CVSS base score
7.8
Published
2026-07-28
OWASP
A08 Software and Data Integrity Failures
Orthogonal defect classification
Checking
Code defect classification
Missing Check
Category
Input Validation and Sanitization
Subcategory
Command Injection
Accessibility scope
Local
Impact
Arbitrary Code Execution
Affected component
datamodel-co
Fixed by upgrading
Yes

Solution

Upgrade `datamodel-code-generator` to version 0.60.2 or later.

Vulnerable code sample

from typing import List, Dict, Any

class PydanticV2BaseModel:
    fields: List[Any]
    validators: List[str]

    def _process_validators(self, extra_validators: Dict[str, str]) -> None:
        """
        Processes extra validators from --extra-template-data.
        This method is a simplified representation based on the CVE description.
        """
        for field in self.fields:
            # In a real implementation, field is an object with a .name attribute
            if validator_logic := extra_validators.get(field.name):
                # VULNERABLE: A user-provided string is formatted directly into a Python code string without validation, leading to code injection.
                validator_string = (
                    f'@field_validator("{field.name}")\n'
                    f'@classmethod\n'
                    f'def validate_{field.name}(cls, v):\n'
                    f'    {validator_logic}\n'
                    f'    return v'
                )
                self.validators.append(validator_string)

Patched code sample

import re
from typing import List, Dict, Any

class PydanticV2BaseModel:
    fields: List[Any]
    validators: List[str]

    def _process_validators(self, extra_validators: Dict[str, str]) -> None:
        """
        Processes extra validators from --extra-template-data.
        This method is a simplified representation based on the CVE description.
        """
        for field in self.fields:
            # In a real implementation, field is an object with a .name attribute
            if validator_logic := extra_validators.get(field.name):
                # FIX: Validate the user-provided string to ensure it does not contain characters that could be used to inject additional statements.
                if re.search(r"[;\n]", validator_logic):
                    raise ValueError(f"Potentially unsafe validator for field '{field.name}'")
                validator_string = (
                    f'@field_validator("{field.name}")\n'
                    f'@classmethod\n'
                    f'def validate_{field.name}(cls, v):\n'
                    f'    {validator_logic}\n'
                    f'    return v'
                )
                self.validators.append(validator_string)

Payload

{
  "validators": {
    "name": "mode='before')\n__import__('os').system('touch /tmp/pwned')\n#"
  }
}

Cite this entry

@misc{vaitp:cve202654656,
  title        = {{datamodel-code-generator allows code execution via --extra-template-data.}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2026},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2026-54656},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-54656/}}
}
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 ::