/

CVE-2026-54655

Severity:

HIGH RISK

(7.8)

Code Injection (CWE-94) via JSON Schema in Generator

CVE-2026-54655

Deferred

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Arbitrary Code Execution at Generation-Time Import

Severity:

HIGH RISK

(7.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 flaw in datamodel-code-generator allows attacker-controlled JSON Schema attributes to be inserted directly into generated Python type annotations.

When such a generated module is imported, the embedded Python expressions may be executed, making schema-driven workflows risky if schemas are not fully trusted.

A code injection flaw in datamodel-code-generator allows attacker-controlled JSON Schema attributes to be inserted directly into generated Python type annotations.

When such a generated module is imported, the embedded Python expressions may be executed, making schema-driven workflows risky if schemas are not fully trusted.

Affected context

This vulnerability appears in environments that use datamodel-code-generator to automatically generate Python models from JSON Schema or similar specifications.

It becomes relevant when:

  • Schemas are obtained from external or semi-trusted sources (e.g., user uploads, partner APIs, shared repositories)
  • The generator processes x-python-type extensions without strict validation
  • Generated modules are imported into application code, tooling, or pipelines

In tightly controlled, fully trusted schema authoring workflows, the practical risk may be lower but still warrants review.

This vulnerability appears in environments that use datamodel-code-generator to automatically generate Python models from JSON Schema or similar specifications.

It becomes relevant when:

  • Schemas are obtained from external or semi-trusted sources (e.g., user uploads, partner APIs, shared repositories)
  • The generator processes x-python-type extensions without strict validation
  • Generated modules are imported into application code, tooling, or pipelines

In tightly controlled, fully trusted schema authoring workflows, the practical risk may be lower but still warrants review.

Exploit preconditions

  • Attack surface: JSON Schema or similar input consumed by datamodel-code-generator
  • Required access: Ability to provide or modify schema content that the generator will process (e.g., commit rights, upload capability, or control of an API supplying schemas)
  • Trigger point: Running datamodel-code-generator on the malicious schema and then importing the resulting Python module
  • Why exploitation is possible: The _get_python_type_override logic inserts x-python-type values directly into field annotations without adequate validation or sanitization, allowing arbitrary Python expressions to be evaluated during import.
  • Attack surface: JSON Schema or similar input consumed by datamodel-code-generator
  • Required access: Ability to provide or modify schema content that the generator will process (e.g., commit rights, upload capability, or control of an API supplying schemas)
  • Trigger point: Running datamodel-code-generator on the malicious schema and then importing the resulting Python module
  • Why exploitation is possible: The _get_python_type_override logic inserts x-python-type values directly into field annotations without adequate validation or sanitization, allowing arbitrary Python expressions to be evaluated during import.

Why this happens

The underlying issue is a direct trust of schema-provided type override metadata as executable Python code.

In jsonschema.py, _get_python_type_override treats x-python-type as a safe directive and embeds its value into generated annotations, effectively creating code from data.

This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where untrusted input is allowed to influence code structure and behavior without enforcement of a safe subset or strict validation.

The underlying issue is a direct trust of schema-provided type override metadata as executable Python code.

In jsonschema.py, _get_python_type_override treats x-python-type as a safe directive and embeds its value into generated annotations, effectively creating code from data.

This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where untrusted input is allowed to influence code structure and behavior without enforcement of a safe subset or strict validation.

Potential impact

  • Arbitrary code execution when importing generated Python modules
  • Compromise of build or generation environments (CI/CD runners, developer workstations)
  • Installation of backdoors or data exfiltration logic in application code
  • Tampering with business logic or security controls through poisoned models
  • Lateral movement from the generation pipeline into connected systems or services
  • Arbitrary code execution when importing generated Python modules
  • Compromise of build or generation environments (CI/CD runners, developer workstations)
  • Installation of backdoors or data exfiltration logic in application code
  • Tampering with business logic or security controls through poisoned models
  • Lateral movement from the generation pipeline into connected systems or services

General mitigation guidance

Effective mitigation focuses on reducing trust in schema-driven code generation and constraining what can influence generated code.

Typical strategies include:

  • Treating all external schemas as untrusted input and avoiding direct embedding of extension fields into executable code
  • Limiting or removing support for arbitrary code-bearing extensions such as x-python-type, or strictly validating them against a conservative allowlist of safe constructs
  • Separating code generation environments from production systems and running generators with least privilege
  • Introducing a review and approval process for schemas that can influence model generation in shared repositories
  • Favoring declarative, non-executable metadata in schemas instead of extension fields that are interpreted as Python expressions

Effective mitigation focuses on reducing trust in schema-driven code generation and constraining what can influence generated code.

Typical strategies include:

  • Treating all external schemas as untrusted input and avoiding direct embedding of extension fields into executable code
  • Limiting or removing support for arbitrary code-bearing extensions such as x-python-type, or strictly validating them against a conservative allowlist of safe constructs
  • Separating code generation environments from production systems and running generators with least privilege
  • Introducing a review and approval process for schemas that can influence model generation in shared repositories
  • Favoring declarative, non-executable metadata in schemas instead of extension fields that are interpreted as Python expressions

How teams detect issues like this

Detection can rely on both code-level and workflow-focused controls:

  • Static analysis or linting rules that flag dynamic code generation patterns and direct insertion of user-controlled strings into type annotations or executable contexts
  • Targeted scans of generated code for suspicious constructs, such as unexpected function calls, imports, or arbitrary expressions in annotations originating from schema metadata
  • Code review practices that require inspection of changes to schema files, especially fields like x-python-type or other custom extensions that influence generation
  • CI policies that restrict or warn on use of extension fields and enforce a schema validation step before generation
  • Optional use of automated security review tools integrated with the generation pipeline to catch unsafe patterns early

Detection can rely on both code-level and workflow-focused controls:

  • Static analysis or linting rules that flag dynamic code generation patterns and direct insertion of user-controlled strings into type annotations or executable contexts
  • Targeted scans of generated code for suspicious constructs, such as unexpected function calls, imports, or arbitrary expressions in annotations originating from schema metadata
  • Code review practices that require inspection of changes to schema files, especially fields like x-python-type or other custom extensions that influence generation
  • CI policies that restrict or warn on use of extension fields and enforce a schema validation step before generation
  • Optional use of automated security review tools integrated with the generation pipeline to catch unsafe patterns early

Sources

SourceLink
github.comgithub.com/koxudaxi/datamodel-code-generator/commit/2c93c9b712f43391dcfa975a1e4aa0b7c93ccbba
github.comgithub.com/koxudaxi/datamodel-code-generator/releases/tag/0.60.2
github.comgithub.com/koxudaxi/datamodel-code-generator/security/advisories/GHSA-m34r-v34r-rf9q

Frequently asked

questions

It can be, if an attacker can supply or modify schemas that your environment consumes remotely (for example, via an upload endpoint or an integrated partner service). The exploit occurs locally when your tooling generates and imports the resulting Python modules.

The attacker must be able to influence the JSON Schema content processed by datamodel-code-generator. This may involve repository write access, control over an API feeding schemas, or user-level capabilities to upload or configure schemas that are later used in automated generation.

The safest approach is to prevent untrusted schema metadata from becoming executable code. Concretely, this means disallowing or strictly validating fields like x-python-type, constraining them to a safe subset, and ensuring the generator treats schemas as data rather than code.

Design code generation workflows around strict trust boundaries: avoid embedding free-form strings into generated code, validate and sanitize all extension fields, run generators in isolated, least-privilege environments, and add automated checks in CI to flag code paths where data can influence executable output.

It can be, if an attacker can supply or modify schemas that your environment consumes remotely (for example, via an upload endpoint or an integrated partner service). The exploit occurs locally when your tooling generates and imports the resulting Python modules.

The attacker must be able to influence the JSON Schema content processed by datamodel-code-generator. This may involve repository write access, control over an API feeding schemas, or user-level capabilities to upload or configure schemas that are later used in automated generation.

The safest approach is to prevent untrusted schema metadata from becoming executable code. Concretely, this means disallowing or strictly validating fields like x-python-type, constraining them to a safe subset, and ensuring the generator treats schemas as data rather than code.

Design code generation workflows around strict trust boundaries: avoid embedding free-form strings into generated code, validate and sanitize all extension fields, run generators in isolated, least-privilege environments, and add automated checks in CI to flag code paths where data can influence executable output.

Find out what's already
exploitable in your codebase.