CVE-2026-54655: Datamodel code generator `datamodel-code-generator` Vulnerable To Code Execution Vulnerability
HIGHCVE-2026-54655 is a HIGH (CVSS 7.8) `datamodel-code-generator` vulnerable to code execution 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 execution on import via `x-python-type` json-schema extension in datamodel-code-generator. Remediation is available by upgrading affected packages to version 0.60.2.
CVE-2026-54655 at a Glance
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).
How to Fix CVE-2026-54655
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.
pip install datamodel-code-generator>=0.60.2
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-54655, 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
CVE-2026-54655 Affected Datamodel code generator Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
datamodel-code-generator
|
PyPI |
≥ 0.51.0
< 0.60.2
|
0.60.2
|
References
Frequently Asked Questions
What is CVE-2026-54655?
Which Datamodel code generator versions are affected?
What version fixes CVE-2026-54655?
Is datamodel-code-generator affected?
What is the CVSS score of CVE-2026-54655?
What CWE is associated with CVE-2026-54655?
How can I check PyPI dependencies for CVE-2026-54655?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-m34r-v34r-rf9q",
"modified" : "2026-07-28T21:33:57Z",
"published" : "2026-07-28T21:33:57Z",
"aliases" : [ "CVE-2026-54655" ],
"summary" : "`datamodel-code-generator` vulnerable to code execution on import via `x-python-type` JSON-Schema extension in datamodel-code-generator",
"details" : "### Summary\n`datamodel-code-generator` honours a custom `x-python-type` JSON-Schema extension that lets a schema author override the generated Python type for a field. The value is forwarded verbatim into the generated Python source as the field annotation, with a single sanitisation pass that is trivial to bypass. An attacker who controls a JSON Schema fed to `datamodel-codegen` can therefore embed an arbitrary Python statement in the generated module, which executes at class-definition time the moment the developer imports the file. No `--extra-template-data` and no special flags are required; the vulnerable code is reachable with default settings.\n\n### Details\nSink: `src/datamodel_code_generator/parser/jsonschema.py`, `_get_python_type_override` (lines 2055–2096, at tag `0.60.1` / commit `a321547e`):\n\n```python\ndef _get_python_type_override(self, obj: JsonSchemaObject) -> DataType | None:\n x_python_type = obj.extras.get(\"x-python-type\")\n if not x_python_type or not isinstance(x_python_type, str):\n return None\n schema_type = obj.type if isinstance(obj.type, str) else None\n if self._is_compatible_python_type(schema_type, x_python_type):\n return None\n base_type = self._get_python_type_base(x_python_type)\n import_ = self._resolve_type_import(base_type)\n type_str = x_python_type\n prefix = x_python_type.split(\"[\", maxsplit=1)[0]\n if \".\" in prefix: # only sanitiser\n type_str = base_type + x_python_type[len(prefix):]\n ...\n ...\n result = self.data_type(type=type_str, import_=import_)\n ...\n return result\n```\n\n`DataType.type` flows unescaped into `{{ field.type_hint }}` in every model template\n(`model/template/pydantic_v2/BaseModel.jinja2`,\n`model/template/dataclass.jinja2`,\n`model/template/TypedDictClass.jinja2`,\n`model/template/msgspec.jinja2`, …).\n\nThe only sanitiser — the dot-rewrite at the marked line — fires only when `.` is in the substring **before the first `[`** in the value. Placing `[` early (e.g. `X[1]; <payload>`) keeps `prefix == \"X\"` so the rewrite is skipped and the whole value lands in the generated annotation.\n\n`from __future__ import annotations` (emitted by default) makes the `X[1]` portion a lazy string, so `X` does not need to resolve at runtime. Everything after `;` is parsed as a real statement in the class body and is executed when the class is constructed during `import`.\n\nOutput-model types confirmed vulnerable in testing: `pydantic_v2.BaseModel`, `dataclasses.dataclass`, `typing.TypedDict`. `msgspec.Struct` emits structurally identical code.\n\n### PoC\nA self-contained PoC is available at: https://gist.github.com/thegr1ffyn/1a7ff2561a581074c49785230b2c5700\n\n### Impact\nArbitrary code execution in the developer's interpreter / CI runner as soon as the generated module is imported. Reachable from any workflow that ingests an untrusted JSON Schema:\n\n- OpenAPI / JSON-Schema documents fetched from third-party services or public registries.\n- Customer-supplied schemas in B2B platforms that auto-generate client SDKs from user input.\n- Schema files added by a malicious commit in a polyglot repository that triggers CI code generation.\n\nThe compromise is silent: the schema is valid JSON, the generator emits syntactically clean Python (the trojan statement is a single indented line in the class body), and only the *use* of the generated file triggers the payload.\n\nAnyone running `datamodel-codegen` against an attacker-supplied schema is impacted. CI runners and developer workstations are the primary blast radius.\n\n### Resolution\n\nThe fix validates `x-python-type` before constructing the generated type annotation. The value is parsed with `ast.parse(..., mode=\"eval\")` and accepted only when the AST is shaped like a Python type annotation, including names, attributes, subscripts, tuple/list annotation arguments, `|` unions, and safe literal values where annotation syntax allows them. Statements, calls, and other executable expressions are rejected before code generation. The validator is cached to avoid repeated AST parsing for repeated values.\n\n### Remediation\n\nUpgrade to `datamodel-code-generator` `0.60.2` or later.\n\nThis issue affects `datamodel-code-generator` versions `>= 0.51.0, <= 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.51.0"
}, {
"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-m34r-v34r-rf9q"
}, {
"type" : "WEB",
"url" : "https://github.com/koxudaxi/datamodel-code-generator/commit/2c93c9b712f43391dcfa975a1e4aa0b7c93ccbba"
}, {
"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-94" ],
"severity" : "HIGH",
"github_reviewed" : true,
"github_reviewed_at" : "2026-07-28T21:33:57Z",
"nvd_published_at" : null
}
}