/

CVE-2026-54653

Severity:

HIGH RISK

(8.8)

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

CVE-2026-54653

Analyzed

Vulnerability type

Category:

Code Injection

CWE:

CWE-94, CWE-1336

Impact:

Arbitrary Code Execution at Model Import Time

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

datamodel-code-generator between versions 0.17.0 and 0.60.2 preserves attacker-controlled default_factory values when generating Python models from schemas.

If an attacker can influence the input schema, they can cause arbitrary Python expressions to be emitted into Field(default_factory=...) or field(default_factory=...), leading to code execution when the generated models are imported.

datamodel-code-generator between versions 0.17.0 and 0.60.2 preserves attacker-controlled default_factory values when generating Python models from schemas.

If an attacker can influence the input schema, they can cause arbitrary Python expressions to be emitted into Field(default_factory=...) or field(default_factory=...), leading to code execution when the generated models are imported.

Affected context

This vulnerability arises in workflows where Python models are automatically generated from external specifications such as OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, or raw JSON/YAML/CSV.

It is most relevant when:

  • Schemas are partially or fully controlled by external parties (e.g., partners, tenants, or untrusted input sources)
  • Generated models are committed to source control or imported in application code without manual review
  • default_factory metadata from the schema is trusted and directly translated into Python expressions

This vulnerability arises in workflows where Python models are automatically generated from external specifications such as OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, or raw JSON/YAML/CSV.

It is most relevant when:

  • Schemas are partially or fully controlled by external parties (e.g., partners, tenants, or untrusted input sources)
  • Generated models are committed to source control or imported in application code without manual review
  • default_factory metadata from the schema is trusted and directly translated into Python expressions

Exploit preconditions

  • Attack surface: The schema input consumed by datamodel-code-generator (JSON Schema, OpenAPI, etc.)
  • Required access: Ability to modify or supply a schema that will be processed by datamodel-code-generator
  • Trigger point: Importing or executing the generated Python models containing malicious default_factory expressions
  • Why exploitation is possible: The generator passes attacker-controlled default_factory values through JsonSchemaObject.init and get_field_extras into Field(default_factory=...) or field(default_factory=...) without validation or sanitization, treating them as executable Python callables.
  • Attack surface: The schema input consumed by datamodel-code-generator (JSON Schema, OpenAPI, etc.)
  • Required access: Ability to modify or supply a schema that will be processed by datamodel-code-generator
  • Trigger point: Importing or executing the generated Python models containing malicious default_factory expressions
  • Why exploitation is possible: The generator passes attacker-controlled default_factory values through JsonSchemaObject.init and get_field_extras into Field(default_factory=...) or field(default_factory=...) without validation or sanitization, treating them as executable Python callables.

Why this happens

The core issue maps to CWE-94: Improper Control of Generation of Code and is closely related to CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine.

datamodel-code-generator treats schema-supplied default_factory values as trusted configuration and directly embeds them into generated Python source code.

Because these values are emitted into Field(default_factory=...) or field(default_factory=...) constructs, an attacker can craft schema metadata that becomes arbitrary Python expressions.

When the generated modules are imported, Python evaluates these expressions, effectively turning data-driven configuration into executable code without enforcing clear trust boundaries or sanitizing template inputs.

The core issue maps to CWE-94: Improper Control of Generation of Code and is closely related to CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine.

datamodel-code-generator treats schema-supplied default_factory values as trusted configuration and directly embeds them into generated Python source code.

Because these values are emitted into Field(default_factory=...) or field(default_factory=...) constructs, an attacker can craft schema metadata that becomes arbitrary Python expressions.

When the generated modules are imported, Python evaluates these expressions, effectively turning data-driven configuration into executable code without enforcing clear trust boundaries or sanitizing template inputs.

Potential impact

  • Arbitrary Python code execution during import of generated models
  • Compromise of development tools, build pipelines, or code generation servers
  • Theft or corruption of sensitive data processed by applications using the generated models
  • Lateral movement from code generation environments into broader infrastructure
  • Introduction of persistent backdoors via malicious logic embedded in generated source files
  • Arbitrary Python code execution during import of generated models
  • Compromise of development tools, build pipelines, or code generation servers
  • Theft or corruption of sensitive data processed by applications using the generated models
  • Lateral movement from code generation environments into broader infrastructure
  • Introduction of persistent backdoors via malicious logic embedded in generated source files

General mitigation guidance

Effective mitigation focuses on reducing trust in schema-driven code generation and constraining how dynamic values are translated into executable Python.

Pattern-based defensive approaches include:

  • Treating schema metadata, especially default_factory and similar hooks, as untrusted input and blocking or strictly validating it before incorporating into generated code
  • Restricting generators to use only a fixed set of safe default factories (e.g., built-in collection constructors) rather than arbitrary expressions
  • Separating data description from execution logic: avoid emitting callables or code paths derived directly from external schema content
  • Running code generation tools in isolated, least-privilege environments to limit blast radius if malicious code is produced
  • Encouraging manual or automated review of generated source files, especially any dynamic constructs, before they are imported by production applications

Effective mitigation focuses on reducing trust in schema-driven code generation and constraining how dynamic values are translated into executable Python.

Pattern-based defensive approaches include:

  • Treating schema metadata, especially default_factory and similar hooks, as untrusted input and blocking or strictly validating it before incorporating into generated code
  • Restricting generators to use only a fixed set of safe default factories (e.g., built-in collection constructors) rather than arbitrary expressions
  • Separating data description from execution logic: avoid emitting callables or code paths derived directly from external schema content
  • Running code generation tools in isolated, least-privilege environments to limit blast radius if malicious code is produced
  • Encouraging manual or automated review of generated source files, especially any dynamic constructs, before they are imported by production applications

How teams detect issues like this

Detection generally centers on inspecting both generator behavior and the resulting source code:

  • Static analysis rules that flag generated code where default_factory or similar parameters reference non-whitelisted callables or suspicious expressions
  • Code review checklists highlighting generated models as high-risk artifacts, with special attention to Field(default_factory=...) and field(default_factory=...) usage
  • Template and generator security policies that forbid direct embedding of externally supplied expressions into code, enforced via CI hooks or pre-commit checks
  • Periodic scans of repositories for patterns indicative of schema-driven injection (e.g., unusual lambdas, eval-like constructs, or imports appearing only in generated files)
  • Use of automated code review tools to monitor code generation outputs for dangerous constructs, while keeping tool configuration focused on pattern detection rather than specific versions

Detection generally centers on inspecting both generator behavior and the resulting source code:

  • Static analysis rules that flag generated code where default_factory or similar parameters reference non-whitelisted callables or suspicious expressions
  • Code review checklists highlighting generated models as high-risk artifacts, with special attention to Field(default_factory=...) and field(default_factory=...) usage
  • Template and generator security policies that forbid direct embedding of externally supplied expressions into code, enforced via CI hooks or pre-commit checks
  • Periodic scans of repositories for patterns indicative of schema-driven injection (e.g., unusual lambdas, eval-like constructs, or imports appearing only in generated files)
  • Use of automated code review tools to monitor code generation outputs for dangerous constructs, while keeping tool configuration focused on pattern detection rather than specific versions

Sources

SourceLink
github.comgithub.com/koxudaxi/datamodel-code-generator/commit/17fc235e234cbcfaaadef8c74cb72c9687db0d1d
github.comgithub.com/koxudaxi/datamodel-code-generator/releases/tag/0.60.2
github.comgithub.com/koxudaxi/datamodel-code-generator/security/advisories/GHSA-386q-5hp3-95m9
gist.github.comgist.github.com/thegr1ffyn/9648b0fe4fcf7d569ac8e61dd11eebaf

Frequently asked

questions

Exploitation depends on how schemas are sourced. If an attacker can supply or modify schemas consumed by datamodel-code-generator in a CI/CD pipeline, shared development tool, or hosted service, they can potentially achieve remote code execution wherever the generated models are imported.

The attacker must be able to influence the schema input—through direct write access, an upstream integration, a multi-tenant API that stores user-provided schemas, or any path that feeds untrusted specifications into the generator. No access to the generated Python files themselves is required beyond ensuring they are produced and imported.

The safest pattern is to eliminate direct translation of untrusted schema metadata into executable code. That typically entails removing support for arbitrary default_factory expressions from external inputs, enforcing strict whitelisting of allowed defaults, and treating all code-like schema values as data that must not be executed.

Future tools should design templates and generators with clear trust boundaries: never embed external content as executable expressions, use schema validation and type restrictions to constrain dynamic behavior, and add security-focused reviews and automated checks for generated artifacts. Running generators in isolated environments and adopting secure-by-default templates further reduces the chance of injection-style flaws.

Exploitation depends on how schemas are sourced. If an attacker can supply or modify schemas consumed by datamodel-code-generator in a CI/CD pipeline, shared development tool, or hosted service, they can potentially achieve remote code execution wherever the generated models are imported.

The attacker must be able to influence the schema input—through direct write access, an upstream integration, a multi-tenant API that stores user-provided schemas, or any path that feeds untrusted specifications into the generator. No access to the generated Python files themselves is required beyond ensuring they are produced and imported.

The safest pattern is to eliminate direct translation of untrusted schema metadata into executable code. That typically entails removing support for arbitrary default_factory expressions from external inputs, enforcing strict whitelisting of allowed defaults, and treating all code-like schema values as data that must not be executed.

Future tools should design templates and generators with clear trust boundaries: never embed external content as executable expressions, use schema validation and type restrictions to constrain dynamic behavior, and add security-focused reviews and automated checks for generated artifacts. Running generators in isolated environments and adopting secure-by-default templates further reduces the chance of injection-style flaws.

Find out what's already
exploitable in your codebase.