🛡️ CVE-2026-54621 — datamodel-code-generator

🟠 CVSS 8.0 — High ✅ No Known Exploit CWE-94 NVD
8.0
CVSS Score
0 Low4 Medium7 High9 Critical10

Description

datamodel-code-generator vulnerable to code injection via unescaped carriage return in GraphQL Union description

Summary

datamodel-code-generator is vulnerable to code injection when generating Python models from an attacker-controlled GraphQL schema. A description on a Union type, written in the regular-string form ("...") with a literal \r escape, is rendered into a Python # comment by a Jinja2 filter that handles only \n. Python's tokenizer treats a bare CR as a physical-line terminator, so the comment ends at the \r and the text after it is parsed as module-level Python. The injected code executes at import time of the generated .py, in the context of any consumer that imports the model. No special CLI flags are required.

This affects versions >=0.25.0, <0.60.1 and is fixed in 0.60.1.

Details

The vulnerable output was generated by the GraphQL Union templates:

  • src/datamodel_code_generator/model/template/UnionTypeStatement.jinja2
  • src/datamodel_code_generator/model/template/UnionTypeAliasAnnotation.jinja2
  • src/datamodel_code_generator/model/template/UnionTypeAliasType.jinja2

Before 0.60.1, Union descriptions were rendered as Python comments using logic equivalent to # {{ description | replace('\n', '\n# ') }}.

The Jinja2 replace filter handled \n, but bare \r was not normalized. Python's lexical analysis treats CR, LF, and CRLF as physical line terminators. As a result, a malicious GraphQL schema could cause the generated Python comment to end early and emit attacker-controlled text as module-level Python code.

The description value reaches Union generation from graphql-core through description=union_object.description. GraphQL regular string values can contain \r escapes, and graphql-core preserves that value. Block strings are not affected in the same way because their line endings are normalized before reaching code generation.

Impact

An attacker who can provide or influence a GraphQL schema processed by datamodel-code-generator could cause arbitrary Python code to be generated into the output file. That code would execute with the privileges of the process importing the generated model.

This can affect developers, CI pipelines, or applications that run datamodel-codegen --input-file-type graphql on untrusted or third-party GraphQL schemas and then import the generated Python module.

No custom templates, special CLI flags, or remote reference options are required.

Remediation

Upgrade to datamodel-code-generator 0.60.1 or later.

The fix normalizes carriage returns in GraphQL Union descriptions before rendering them as Python comments, so injected text remains inside the comment block.

Resolution

The fix applies comment_safe to GraphQL Union descriptions before template rendering. It normalizes CRLF and bare CR to LF so the existing Union comment templates keep the whole description inside the generated Python comment block.

Submitted by: Hamza Haroon (thegr1ffyn)

How this vulnerability can be exploited

This issue can be reached with local access to the system, attack complexity is low, an attacker needs no privileges on the target. A user must be tricked into taking some action. The scope is unchanged, so the impact stays within the vulnerable component. Rated impact: confidentiality high, integrity high, availability high.

Weakness class

CVE-2026-54621 is classified as CWE-94: Code Injection. Input is incorporated into code that the runtime evaluates, so an attacker can have their own code executed.

Affected software

CVE-2026-54621 is recorded against 2 packages.

  • datamodel-code-generator (from 0.25.0 up to 0.60.1)
  • unknown

Timeline and source

Published on 28 July 2026 and last revised on 4 August 2026. No public exploit is currently recorded for this entry. Record sourced from NVD.

References

github.com (Web)
github.com (Package)
github.com (Web)

Details

Severity HIGH
CVSS Score 8.0
CVSS Vector CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H
CWE CWE-94
Public Exploit ✅ No
Source NVD
Published 2026-07-28
Updated 2026-08-12
Modified 2026-08-04
Fix URL N/A

Affected Packages

Software From version Fixed in
datamodel-code-generator 0.25.0 0.60.1
unknown

Similar Threats

Site Security Check

Is datamodel-code-generator part of your stack?

CVE-2026-54621 is rated CVSS 8.0 High. BotEraser scans your installation against known CVE records and tells you whether this vulnerability applies to the versions you actually run.

Scan My Site Free →

No credit card required  ·  Results in minutes

ⓘ Data Notice: The information presented above has been compiled from publicly available internet sources. Boteraser aggregates this data solely for informational purposes and does not independently classify, evaluate, or endorse any findings about the vulnerabilities listed. The accuracy and completeness of this information is the sole responsibility of the original publishers. Boteraser and its operators accept no liability for any decisions made based on this data.