VAITP Dataset

← Back to the dataset

CVE-2026-54653

datamodel-code-generator code injection via unsanitized `default_factory`.

  • CVSS 8.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.17.0 until 0.60.2, datamodel-code-generator preserves attacker-controlled default_factory values in src/datamodel_code_generator/parser/jsonschema.py through JsonSchemaObject.init and get_field_extras and emits them into Field(default_factory=…) or field(default_factory=…), allowing Python expression execution when the generated model is imported. This issue is fixed in version 0.60.2.

CWE
94
CVSS base score
8.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 Any, Dict

# Simplified from datamodel_code_generator.parser.jsonschema
class JsonSchemaObject:
    """
    A class representing a JSON schema object, used to generate model fields.
    """
    def __init__(self, source: Dict[str, Any]):
        self.source: Dict[str, Any] = source

    def get_field_extras(self) -> Dict[str, Any]:
        """
        Get extra keyword arguments for a pydantic.Field or dataclasses.field.
        """
        extras: Dict[str, Any] = {}

        default_factory = self.source.get("x-default-factory")
        default = self.source.get("default")

        if default_factory:
            # VULNERABLE: Attacker-controlled string from schema is used directly as code
            # for the 'default_factory' argument, leading to code execution.
            extras["default_factory"] = default_factory
        elif default is not None:
            if isinstance(default, list):
                extras["default_factory"] = "list"
            elif isinstance(default, dict):
                extras["default_factory"] = "dict"
            else:
                extras["default"] = repr(default)

        return extras

Patched code sample

from typing import Any, Dict

# Simplified from datamodel_code_generator.parser.jsonschema
class JsonSchemaObject:
    """
    A class representing a JSON schema object, used to generate model fields.
    """
    def __init__(self, source: Dict[str, Any]):
        self.source: Dict[str, Any] = source

    def get_field_extras(self) -> Dict[str, Any]:
        """
        Get extra keyword arguments for a pydantic.Field or dataclasses.field.
        """
        extras: Dict[str, Any] = {}

        default_factory = self.source.get("x-default-factory")
        default = self.source.get("default")

        if default_factory:
            # FIX: The value of 'x-default-factory' is validated against an allowlist
            # of safe built-in callables to prevent arbitrary code injection.
            if default_factory in ("list", "dict"):
                extras["default_factory"] = default_factory
        elif default is not None:
            if isinstance(default, list):
                extras["default_factory"] = "list"
            elif isinstance(default, dict):
                extras["default_factory"] = "dict"
            else:
                extras["default"] = repr(default)

        return extras

Payload

{
  "title": "Exploit",
  "type": "object",
  "properties": {
    "malicious_field": {
      "type": "string",
      "default_factory": "__import__('os').system('touch /tmp/pwned')"
    }
  }
}

Cite this entry

@misc{vaitp:cve202654653,
  title        = {{datamodel-code-generator code injection via unsanitized `default_factory`.}},
  author       = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
  year         = {2026},
  note         = {VAITP Python Vulnerability Dataset, entry CVE-2026-54653},
  howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-54653/}}
}
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 ::