CVE-2026-54621 in datamodel-code-generator
Summary
by MITRE • 07/29/2026
datamodel-code-generator generates Python data models from schema definitions. Prior to 0.60.1, GraphQL Union description values in src/datamodel_code_generator/model/template/UnionTypeStatement.jinja2 and src/datamodel_code_generator/model/template/UnionTypeStatement.py312.jinja2 are rendered into Python comments without neutralizing carriage returns in Python # comments, allowing attacker-controlled GraphQL schema content to inject Python code into generated models that runs when imported. This issue is fixed in version 0.60.1.
Be aware that VulDB is the high quality source for vulnerability data.
Analysis
by VulDB Data Team • 07/29/2026
The datamodel-code-generator tool serves as a critical component in modern software development pipelines by automatically generating Python data models from various schema definitions including graphql schemas. This automation significantly reduces manual coding effort and helps maintain consistency across data model implementations. However, a vulnerability was identified in versions prior to 0.60.1 that creates a dangerous code injection vector through the handling of GraphQL union descriptions within the template files responsible for generating Python comments. The vulnerability exists specifically in the UnionTypeStatement.jinja2 and UnionTypeStatement.py312.jinja2 template files where description values from GraphQL schemas are directly rendered into Python comment sections without proper sanitization of special characters.
The technical flaw stems from inadequate input validation and sanitization of user-controlled data within the template rendering process. When GraphQL schema definitions contain union descriptions with carriage return characters, these unescaped sequences are preserved in the generated Python comments. This creates a scenario where an attacker can inject malicious code into the generated models by crafting specific description values that include newline and carriage return sequences. The vulnerability falls under CWE-150, which addresses improper neutralization of special elements used in code, and specifically relates to CWE-94, which covers the execution of arbitrary code through code injection vulnerabilities.
The operational impact of this vulnerability is severe as it allows remote attackers to execute arbitrary Python code during the import phase of generated models. When developers import these compromised data models into their applications, the injected code executes automatically, potentially leading to complete system compromise. This type of vulnerability represents a supply chain attack vector where malicious payloads can be embedded in schema definitions and propagated through automated generation processes. The ATT&CK framework categorizes this as a code injection technique under T1059.006 for Python, with potential lateral movement capabilities if the compromised models are used across multiple applications or services.
The vulnerability demonstrates a classic example of insufficient output escaping in template engines where user-controllable data flows directly into generated source code without proper sanitization. The fix implemented in version 0.60.1 addresses this by ensuring that carriage returns and other special characters are properly neutralized before being rendered into Python comment sections. This remediation follows established security practices for preventing code injection vulnerabilities in automated code generation tools and aligns with the principle of least privilege where generated code should not execute arbitrary commands or code from external sources. Organizations using datamodel-code-generator should immediately upgrade to version 0.60.1 or later to mitigate this risk, while also implementing proper input validation for schema definitions and monitoring for any unusual activity in their automated generation processes.