Description
datamodel-code-generator generates Pydantic v2 models, dataclasses, TypedDict, and msgspec.Struct from OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, and raw JSON, YAML, or CSV. From 0.17.0 until 0.60.2, datamodel-code-generator preserves attacker-controlled default_factory values in src/datamodel_code_generator/parser/jsonschema.py through JsonSchemaObject.init and get_field_extras and emits them into Field(default_factory=...) or field(default_factory=...), allowing Python expression execution when the generated model is imported. This issue is fixed in version 0.60.2.
Published: 2026-07-28
Score: 8.8 High
EPSS: < 1% Very Low
KEV: No
Impact: n/a
Action: n/a
AI Analysis

Impact

This vulnerability occurs in the datamodel‑code‑generator tool, which transforms various data schema formats into Python code. When a schema defines a default_factory field, the generator preserves the provided expression and emits it directly into the generated Field(... ) or field(... ) constructor. This means that importing the generated module will evaluate the expression, enabling arbitrary Python code execution. The weakness is a classic code injection flaw, illustrated by CWE‑94, and a misuse of default_factory leading to unforeseen code running, classified as CWE‑1336. If the generated code is executed in a privileged context, the attacker can compromise confidentiality, integrity, or availability of the affected system.

Affected Systems

The vulnerable product is datamodel-code-generator from koxudaxi. Versions 0.17.0 through 0.60.1 (inclusive) contain the flaw, as the issue was fixed in 0.60.2. No other vendors or product lines are affected. Users running the generator on untrusted schemas should treat these releases as vulnerable.

Risk and Exploitability

The CVSS score is 8.8, which places the vulnerability in the high severity range. However, the EPSS score is below 1 %, indicating a low probability of exploitation at present. The flaw is not in the CISA known‑exploited vulnerabilities list. The attack vector requires an attacker to supply a malicious schema to the code generator or influence the schema generation process. Once the bad schema is processed, any Python interpreter that imports the generated model will execute the injected expression, potentially allowing local code execution and, if the interpreter runs with elevated privileges, remote code execution. Because the vulnerability is discovered through the code generator, remediation by upgrading to the patched version is the most effective countermeasure.

Generated by OpenCVE AI on August 3, 2026 at 14:09 UTC.

Remediation

No vendor fix or workaround currently provided.

OpenCVE Recommended Actions

  • Upgrade datamodel‑code_generator to version 0.60.2 or later.
  • Verify that generated code does not contain unexpected default_factory expressions before importing it.
  • Sanitize or restrict input schemas to the code generator so that only trusted schemas are processed.

Generated by OpenCVE AI on August 3, 2026 at 14:09 UTC.

Tracking

Sign in to view the affected projects.

Advisories
Source ID Title
Github GHSA Github GHSA GHSA-386q-5hp3-95m9 `datamodel-code-generator` vulnerable to code injection in via attacker-controlled `default_factory` schema field
History

Wed, 29 Jul 2026 14:30:00 +0000

Type Values Removed Values Added
References
Metrics ssvc

{'options': {'Automatable': 'no', 'Exploitation': 'none', 'Technical Impact': 'total'}, 'version': '2.0.3'}


Tue, 28 Jul 2026 23:45:00 +0000

Type Values Removed Values Added
First Time appeared Koxudaxi
Koxudaxi datamodel-code-generator
Vendors & Products Koxudaxi
Koxudaxi datamodel-code-generator

Tue, 28 Jul 2026 22:00:00 +0000

Type Values Removed Values Added
Description datamodel-code-generator generates Pydantic v2 models, dataclasses, TypedDict, and msgspec.Struct from OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, and raw JSON, YAML, or CSV. From 0.17.0 until 0.60.2, datamodel-code-generator preserves attacker-controlled default_factory values in src/datamodel_code_generator/parser/jsonschema.py through JsonSchemaObject.init and get_field_extras and emits them into Field(default_factory=...) or field(default_factory=...), allowing Python expression execution when the generated model is imported. This issue is fixed in version 0.60.2.
Title `datamodel-code-generator` vulnerable to code injection in via attacker-controlled `default_factory` schema field
Weaknesses CWE-1336
CWE-94
References
Metrics cvssV3_1

{'score': 8.8, 'vector': 'CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H'}


Subscriptions

Koxudaxi Datamodel-code-generator
cve-icon MITRE

Status: PUBLISHED

Assigner: GitHub_M

Published:

Updated: 2026-07-29T13:27:46.750Z

Reserved: 2026-06-15T20:16:46.199Z

Link: CVE-2026-54653

cve-icon Vulnrichment

Updated: 2026-07-29T13:27:31.645Z

cve-icon NVD

Status : Analyzed

Published: 2026-07-28T22:17:40.077

Modified: 2026-08-06T20:14:06.983

Link: CVE-2026-54653

cve-icon Redhat

No data.

cve-icon OpenCVE Enrichment

Updated: 2026-08-03T14:15:05Z

Weaknesses
  • CWE-1336

    Improper Neutralization of Special Elements Used in a Template Engine

  • CWE-94

    Improper Control of Generation of Code ('Code Injection')