CVE-2026-54654: Datamodel code generator `datamodel-code-generator` Vulnerable To Code Injection Vulnerability

HIGH
Advisory Identifier: GHSA-wjv6-jcfj-mf9r
CVSS 7.8
CWE-94 CWE-1336
Published
Advisory Summary

CVE-2026-54654 is a HIGH (CVSS 7.8) `datamodel-code-generator` vulnerable to code injection vulnerability (CWE-94: Improper Control of Generation of Code ('Code Injection')) affecting Datamodel code generator libraries in the PyPI ecosystem. The issue involves `datamodel-code-generator` vulnerable to code injection via unescaped carriage return in `--extra-template-data` `comment` field. Remediation is available by upgrading affected packages to version 0.60.2.

CVE-2026-54654 at a Glance

CVE Identifier CVE-2026-54654
GHSA Identifier GHSA-wjv6-jcfj-mf9r
Severity HIGH
CVSS v3.1 Base Score 7.8
CWE Classification CWE-94: Improper Control of Generation of Code ('Code Injection')
Affected Product Datamodel code generator
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 7.8

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H).

Attack Vector Local (AV:L)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction Required (UI:R)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity High (I:H)
Availability High (A:H)

How to Fix CVE-2026-54654

To remediate this vulnerability, upgrade all affected Datamodel code generator dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

datamodel-code-generator fixed: 0.60.2
pip install datamodel-code-generator>=0.60.2
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show datamodel-code-generator

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2026-54654, inspect your installed packages using pip:

pip show datamodel-code-generator

Analyze the terminal output to locate any version of Datamodel code generator in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.60.2), your application is affected.

Note: The pip show datamodel-code-generator command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-94: Improper Control of Generation of Code ('Code Injection'), where the product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment. In Datamodel code generator, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Summary `datamodel-code-generator` is vulnerable to code injection when a developer passes an `--extra-template-data` file whose `comment` value contains a literal `\r` (carriage return). The `comment` variable is rendered into a Python `#` comment in six built-in templates with **no** line-terminator escaping. Python's tokenizer treats a bare CR as a physical-line terminator (see [Python language reference — Physical lines](https://docs.python.org/3/reference/lexical_analysis.html#physical-lines)), so the comment ends at the `\r` and the text after it is parsed as Python, including, when the CR is followed by suitable indentation, as a statement within the class body that follows on the next template line. ### Details The vulnerable templates each contain `# {{ comment }}` with no escaping: - `src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2:12` and `:19` - `src/datamodel_code_generator/model/template/TypeAliasType.jinja2:12` and `:19` - `src/datamodel_code_generator/model/template/TypeStatement.jinja2:12` and `:19` - `src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2:4` - `src/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2:19` - `src/datamodel_code_generator/model/template/pydantic_v2/RootModelTypeAlias.jinja2:13` The `pydantic_v2/BaseModel.jinja2:4` site is representative: ```jinja2 class {{ class_name }}({{ base_class }}):{% if comment is defined %} # {{ comment }}{% endif %} ``` When the developer-supplied extras file populates `comment` for a model, the value reaches the template via `DataModel.extra_template_data` (set in `src/datamodel_code_generator/model/base.py:736-742`) and Jinja2 interpolates it raw. None of the templates use `comment_safe`, `escape_docstring`, or any other line-terminator filter. ### PoC Complete self contained POC is available at my secret gist: https://gist.github.com/thegr1ffyn/8ad6b8cb3cc2be9d3a0144aeb6896a3f ### Impact - **Who's affected**: any developer or CI pipeline that runs `datamodel-codegen --extra-template-data <file>` where the extras file is influenced by attacker-controlled input. Realistic scenarios include: - Extras file generated from a third-party schema-annotation system. - Extras file vendored from an upstream repository. - Extras file produced by a script that merges multiple `comment` sources. - Build pipelines that template the extras file from environment variables, ticket descriptions, or commit metadata. - **What it gains**: arbitrary Python code execution in the importer's process at `import` time. - **What it does NOT need**: the schema itself can be entirely benign; only the extras file needs to contain the malicious `comment`. - **What does block it**: not passing `--extra-template-data`, or rejecting extras files whose `comment` values contain `\r`, `\x0b`, or `\x0c` before invocation. ### Resolution The fix normalizes `comment` values from built-in `--extra-template-data` before template rendering. Inline comments now convert CRLF, bare CR, vertical tab, and form feed into LF and prefix continuation lines with `# `, so attacker-controlled text stays inside the generated Python comment block. ### Remediation Upgrade to `datamodel-code-generator` `0.60.2` or later. This issue affects `datamodel-code-generator` versions `>= 0.14.1, <= 0.60.1` and is fixed in `0.60.2`. Submitted by: Hamza Haroon (thegr1ffyn)

CVE-2026-54654 Affected Datamodel code generator Versions

1 component(s)
Fixed Versions:
0.60.2
Product / Package Ecosystem Affected Versions Fixed Version
datamodel-code-generator
PyPI
≥ 0.14.1
< 0.60.2
0.60.2

References

Frequently Asked Questions

What is CVE-2026-54654?
CVE-2026-54654 is a security vulnerability in Datamodel code generator involving `datamodel-code-generator` vulnerable to code injection via unescaped carriage return in `--extra-template-data` `comment` field under CWE-94: Improper Control of Generation of Code ('Code Injection').
Which Datamodel code generator versions are affected?
Affected versions include releases prior to 0.60.2 across affected packages: datamodel-code-generator.
What version fixes CVE-2026-54654?
Upgrading to 0.60.2 (or the latest patched release for your respective branch) resolves the vulnerability.
Is datamodel-code-generator affected?
Yes, datamodel-code-generator is affected in releases prior to 0.60.2. Upgrade your dependency to 0.60.2 or later.
What is the CVSS score of CVE-2026-54654?
CVE-2026-54654 has a CVSS v3.1 base score of 7.8 (HIGH) with vector string CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H.
What CWE is associated with CVE-2026-54654?
CVE-2026-54654 is classified under CWE-94: Improper Control of Generation of Code ('Code Injection') (The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.).
How can I check PyPI dependencies for CVE-2026-54654?
Run the dependency tree command: 'pip show datamodel-code-generator'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-wjv6-jcfj-mf9r",
  "modified" : "2026-07-28T21:50:09Z",
  "published" : "2026-07-28T21:50:09Z",
  "aliases" : [ "CVE-2026-54654" ],
  "summary" : "`datamodel-code-generator` vulnerable to code injection via unescaped carriage return in `--extra-template-data` `comment` field",
  "details" : "### Summary\n\n`datamodel-code-generator` is vulnerable to code injection when a developer passes an `--extra-template-data` file whose `comment` value contains a literal `\\r` (carriage return). The `comment` variable is rendered into a Python `#` comment in six built-in templates with **no** line-terminator escaping. Python's tokenizer treats a bare CR as a physical-line terminator (see [Python language reference — Physical lines](https://docs.python.org/3/reference/lexical_analysis.html#physical-lines)), so the comment ends at the `\\r` and the text after it is parsed as Python, including, when the CR is followed by suitable indentation, as a statement within the class body that follows on the next template line.\n\n### Details\n\nThe vulnerable templates each contain `# {{ comment }}` with no escaping:\n\n- `src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2:12` and `:19`\n- `src/datamodel_code_generator/model/template/TypeAliasType.jinja2:12` and `:19`\n- `src/datamodel_code_generator/model/template/TypeStatement.jinja2:12` and `:19`\n- `src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2:4`\n- `src/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2:19`\n- `src/datamodel_code_generator/model/template/pydantic_v2/RootModelTypeAlias.jinja2:13`\n\nThe `pydantic_v2/BaseModel.jinja2:4` site is representative:\n\n```jinja2\nclass {{ class_name }}({{ base_class }}):{% if comment is defined %}  # {{ comment }}{% endif %}\n```\n\nWhen the developer-supplied extras file populates `comment` for a model, the value reaches the template via `DataModel.extra_template_data` (set in `src/datamodel_code_generator/model/base.py:736-742`) and Jinja2 interpolates it raw. None of the templates use `comment_safe`, `escape_docstring`, or any other line-terminator filter.\n\n### PoC\nComplete self contained POC is available at my secret gist: https://gist.github.com/thegr1ffyn/8ad6b8cb3cc2be9d3a0144aeb6896a3f\n\n### Impact\n\n- **Who's affected**: any developer or CI pipeline that runs `datamodel-codegen --extra-template-data <file>` where the extras file is influenced by attacker-controlled input. Realistic scenarios include:\n  - Extras file generated from a third-party schema-annotation system.\n  - Extras file vendored from an upstream repository.\n  - Extras file produced by a script that merges multiple `comment` sources.\n  - Build pipelines that template the extras file from environment variables, ticket descriptions, or commit metadata.\n- **What it gains**: arbitrary Python code execution in the importer's process at `import` time.\n- **What it does NOT need**: the schema itself can be entirely benign; only the extras file needs to contain the malicious `comment`.\n- **What does block it**: not passing `--extra-template-data`, or rejecting extras files whose `comment` values contain `\\r`, `\\x0b`, or `\\x0c` before invocation.\n\n### Resolution\n\nThe fix normalizes `comment` values from built-in `--extra-template-data` before template rendering. Inline comments now convert CRLF, bare CR, vertical tab, and form feed into LF and prefix continuation lines with `# `, so attacker-controlled text stays inside the generated Python comment block.\n\n### Remediation\n\nUpgrade to `datamodel-code-generator` `0.60.2` or later.\n\nThis issue affects `datamodel-code-generator` versions `>= 0.14.1, <= 0.60.1` and is fixed in `0.60.2`.\n\nSubmitted by: Hamza Haroon (thegr1ffyn)",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "datamodel-code-generator"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0.14.1"
      }, {
        "fixed" : "0.60.2"
      } ]
    } ],
    "database_specific" : {
      "last_known_affected_version_range" : "<= 0.60.1"
    }
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/koxudaxi/datamodel-code-generator/security/advisories/GHSA-wjv6-jcfj-mf9r"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/koxudaxi/datamodel-code-generator/commit/b73abb5cd703a50471b8950bbd3bd0b82ad71de7"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/koxudaxi/datamodel-code-generator"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/koxudaxi/datamodel-code-generator/releases/tag/0.60.2"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-1336", "CWE-94" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-07-28T21:50:09Z",
    "nvd_published_at" : null
  }
}