Categories
CVE-2026-54621 high CVSS 7.8

Code Injection in datamodel-code-generator (CVE-2026-54621)

Summary

Code injection in datamodel-code-generator (CVE-2026-54621). Successful exploitation can lead to full system takeover. Exploitable via import. Mitigation: upgrade to 0.60.1 or later.

Original description

The description as published by the source.

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…

Source: GitHub Security Advisories (Global) NVD GitHub Advisory

Auto-generated summary

Assembled by SecPulse from the source data (CVSS, CWE, affected versions) using templates — no AI involved. Check the original description and references below for exact details.

A vulnerability tracked as CVE-2026-54621 has been found in datamodel-code-generator.
Attackers can target a specific entry point like import over the network to misuse the product.
Successful exploitation can lead to full system takeover. CVSS score: 7.8/10.
What to do: upgrade datamodel-code-generator to 0.60.1 or later.
If unsure, ask your IT team or search "datamodel-code-generator CVE-2026-54621" on the vendor's site.

CVE-2026-54621 (datamodel-code-generator) — CWE-94 / CVSS v3 7.8
Attack vector: local / unauthenticated
Attack surface: import / replace / description / comment_safe
Patched: 0.60.1 — apply immediately
Plan: 1) Audit SBOM/dependencies, 2) Stage→prod upgrade, 3) Add WAF/proxy monitoring on affected endpoints, 4) Hunt IOCs in logs.
Refs: see the GHSA / vendor advisory / patched release linked on this page.

❓ What is the problem

Arbitrary code execution (CWE-94) exists in datamodel-code-generator. Attackers reach the vulnerable code path via import without authentication.

📍 Affected scope

Check the vendor advisory for the affected versions of datamodel-code-generator. Attack surface: import / replace / description / comment_safe.

🔥 Severity

Severity: High (CVSS 7.8/10). Successful exploitation can lead to full system takeover.

🔧 How to fix

Update to 0.60.1.

🛡️ Workaround

Until the patch is applied: disable the affected feature, apply WAF rules, or restrict access via network ACLs.

🔍 Detection

Search webserver/proxy logs for unusual request patterns matching this CVE's known IOCs. Run grep -r 'datamodel-code-generator' . against your dependency files (package-lock.json, requirements.txt, go.sum) to find affected services.

Response actions

Concrete steps and command examples for SOC/SRE teams to execute in order

  1. 1
    Identify exposure identify
    grep -r 'datamodel-code-generator' . | grep -v node_modules

    リポジトリと本番環境の依存ファイル (package-lock.json / requirements.txt / go.sum / Gemfile.lock 等) で datamodel-code-generator を grep し、稼働しているサービス・バージョンを把握する。

  2. 2
    Apply patch patch
    Upgrade datamodel-code-generator to 0.60.1

    ステージング環境で 0.60.1 に上げて回帰テスト → 本番反映。回帰テストはアプリの主要ハッピーパスを含めること。

  3. 3
    Post-deployment verification verify
    Confirm patched version is live in production

    パッチ適用後、ステージングで PoC または同等の悪用パターンを再現して脆弱性が閉じたことを確認。本番ではアクセスログ・WAF でアラート再発が無いか継続監視。

Affected packages

pip datamodel-code-generator
[{"type":"ECOSYSTEM","events":[{"introduced":"0"},{"fixed":"0.60.1"}]}]
PyPI datamodel-code-generator
[{"type":"ECOSYSTEM","events":[{"introduced":"0.25.0"},{"fixed":"0.60.1"}]}]

References

We use cookies for analytics and ad measurement. This setting applies to all Game co., Ltd. sites. Details