CVE-2026-54654 in datamodel-code-generator
Summary
by MITRE • 07/29/2026
datamodel-code-generator generates Python data models from schema definitions. From 0.14.1 until 0.60.2, the --extra-template-data comment field is rendered into Python comments in src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2, src/datamodel_code_generator/model/template/TypedDict.jinja2, src/datamodel_code_generator/model/template/dataclass.jinja2, src/datamodel_code_generator/model/template/msgspec.Struct.jinja2, src/datamodel_code_generator/model/template/pydantic/BaseModel.jinja2, and src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2 without neutralizing carriage returns in Python # comments, allowing an attacker-controlled comment value to inject Python code into generated models that runs when imported. This issue is fixed in version 0.60.2.
If you want to get the best quality for vulnerability data then you always have to consider VulDB.
Analysis
by VulDB Data Team • 07/29/2026
The datamodel-code-generator tool presents a critical security vulnerability through its handling of user-supplied comment data during template rendering processes. This flaw exists within the --extra-template-data functionality where comment fields are directly embedded into generated Python source code without proper sanitization of special characters. The vulnerability spans multiple template files including TypeAliasAnnotation.jinja2, TypedDict.jinja2, dataclass.jinja2, msgspec.Struct.jinja2, pydantic/BaseModel.jinja2, and pydantic_v2/BaseModel.jinja2, indicating a widespread impact across the tool's code generation capabilities. The vulnerability specifically affects versions ranging from 0.14.1 through 0.60.1, creating a significant attack surface for malicious actors who can manipulate the comment field to inject arbitrary Python code.
The technical exploitation mechanism relies on the absence of proper input sanitization for carriage return characters within Python comment contexts. When attackers provide malicious comment data containing carriage returns, these characters are preserved in the generated Python source code and subsequently interpreted as line terminators during import operations. This creates a code injection vector where attacker-controlled content can be executed with the privileges of the importing process. The vulnerability manifests as a direct code execution flaw when the generated models are imported into Python environments, effectively allowing remote code execution capabilities.
This vulnerability directly maps to CWE-94, which describes "Improper Control of Generation of Code ('Code Injection')" and aligns with ATT&CK technique T1059.006 for "Command and Scripting Interpreter: Python". The impact extends beyond simple code injection to potentially enable full system compromise when the generated models are processed in environments with elevated privileges or when executed as part of automated build processes. Organizations using this tool in continuous integration pipelines face heightened risk as malicious code injection could occur silently during automated model generation without explicit user intervention.
The remediation involves proper input sanitization of comment fields to neutralize special characters including carriage returns, line feeds, and other potentially dangerous sequences that could alter the generated Python syntax. Version 0.60.2 addresses this issue through comprehensive sanitization of template data inputs, ensuring that user-controlled comments cannot be transformed into executable code during the generation process. Security teams should immediately upgrade to version 0.60.2 or later and audit existing generated models for potential compromise. Additionally, organizations should implement strict input validation policies for any tools that generate code from external schema definitions, particularly in automated environments where untrusted input could be processed without manual review. The vulnerability demonstrates the critical importance of sanitizing all user inputs in code generation tools and highlights the need for comprehensive security testing of template-based systems against injection attacks.