/

CVE-2026-127208

Severity:

HIGH RISK

(8.8)

Code Injection (CWE-94) in datamodel-code-generator

CVE-2026-127208

Deferred

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Arbitrary Code Execution

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

A code injection vulnerability in datamodel-code-generator can allow arbitrary code execution when a malicious JSON Schema includes a crafted x-python-type annotation. The risk arises when generated models are imported or rebuilt by Pydantic v2, which may evaluate an injected expression as a forward reference.

Affected context

The issue affects workflows that use datamodel-code-generator to process untrusted or insufficiently validated JSON Schema documents containing x-python-type annotations. A crafted annotation can pass through Python type annotation parsing and appear in generated source; execution may occur later when the generated models are imported or rebuilt with Pydantic v2.

Exploit preconditions

  • Attack surface: Processing a JSON Schema with datamodel-code-generator
  • Required access: Ability to supply or influence a schema that the generator processes
  • Trigger point: Importing generated models or rebuilding them with Pydantic v2
  • Why exploitation is possible: A crafted annotation can bypass validation and produce an expression that is evaluated as a forward reference

Why this happens

The vulnerability is a code injection flaw mapped to CWE-94: Improper Control of Generation of Code ('Code Injection'). Validation of Python type annotation syntax does not adequately constrain a crafted AST node in a subscript slice position. As a result, attacker-controlled annotation content can flow into generated source and later be evaluated during model rebuilding.

Potential impact

  • Arbitrary code could execute on developer workstations that process and load affected generated models.
  • CI/CD workers could be compromised if they process a malicious schema and execute the generated models.
  • Applications that generate and import models at runtime could run attacker-controlled code in their process.
  • Secrets, data, or other resources accessible to the executing process could be exposed or altered.

General mitigation guidance

Defensive approaches focus on treating schemas and annotations as untrusted data rather than executable code.

  • Restrict x-python-type values to a narrowly defined allowlist of supported type expressions.
  • Reject unexpected AST node types and validate annotations against a strict grammar before code generation.
  • Prefer fixed mappings from schema types to approved Python types over accepting arbitrary Python expressions.
  • Avoid evaluating generated annotations or other user-influenced strings where possible.
  • Run schema processing and generated model execution with least privilege, limiting access to secrets and sensitive resources.

How teams detect issues like this

  • Use static analysis to identify paths where schema-derived strings enter generated Python source or evaluation APIs.
  • Review annotation parsing and AST conversion code for incomplete node validation and unsafe handling of string constants.
  • Add tests that verify malformed or unsupported annotation structures are rejected before source generation.
  • Enforce schema policies in ingestion and CI workflows, including allowlists for custom annotations.
  • Automated code review tools can help flag dynamic evaluation and untrusted data flows.

Sources

SourceLink
github.comgithub.com/datamodel-code-generator/datamodel-code-generator/commit/ebfc11d0644d37a71374b28aac06d98748e584ec
github.comgithub.com/datamodel-code-generator/datamodel-code-generator/releases/tag/0.84.0
vulncheck.comvulncheck.com/advisories/datamodel-code-generator-code-injection-via-x-python-type-annotation

Frequently asked

questions

The described issue is triggered by processing a malicious schema; remote exploitation depends on whether a service or application accepts schemas from remote or otherwise untrusted sources.

An attacker needs a way to influence a schema processed by the generator. Code execution generally requires the resulting models to be imported or rebuilt.

Treat annotation values as data: allow only explicitly supported types, reject unexpected syntax, and avoid evaluating attacker-influenced expressions.

Validate custom schema annotations with strict allowlists and tests, review data flows into generated code, and run generation and model loading with least privilege.

Find out what's already
exploitable in your codebase.