/

CVE-2026-63720

Severity:

HIGH RISK

(7.5)

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

CVE-2026-63720

Deferred

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Remote Code Execution

Severity:

HIGH RISK

(7.5)

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 before version 0.70.0 contains a code injection flaw in how it embeds user-controlled schema values into generated Python imports.

Attackers who control input schemas can craft a malicious customBasePath value that is emitted verbatim into a from ... import ... statement, enabling remote code execution when the generated module is imported.

Affected context

This vulnerability appears in workflows that use datamodel-code-generator to produce Python models from untrusted or semi-trusted input schemas.

It specifically affects configurations where the schema can control the customBasePath field, and the generated code is later imported and executed in an application or tooling environment.

The risk is higher in automated pipelines (e.g., CI/CD or code generation services) that ingest external schemas and automatically import the generated modules without manual review.

Exploit preconditions

  • Attack surface: Schema ingestion pipeline of datamodel-code-generator.
  • Required access: Ability to submit or modify the input schema (including customBasePath) consumed by the generator.
  • Trigger point: Importing the generated Python module or otherwise executing the generated code.
  • Why exploitation is possible: The customBasePath value is inserted verbatim into a from ... import ... statement without identifier validation or escaping, allowing crafted content with embedded newlines and a dot-free Python expression to break out of the intended syntax and inject arbitrary code.

Why this happens

The underlying issue is unsafe construction of Python source code using untrusted data, aligning with CWE-94: Improper Control of Generation of Code ('Code Injection').

Instead of treating customBasePath as untrusted and validating it as a safe identifier or module path, the generator interpolates it directly into a from ... import ... statement.

This pattern allows user-controlled strings to alter the structure of generated code, including injecting new statements or expressions that run as soon as the module is imported.

The generator effectively blurs the boundary between data and code, creating an injection point in the code generation template.

Potential impact

  • Arbitrary Python code execution when the generated module is imported.
  • Compromise of the application or tooling that imports generated models.
  • Theft or tampering of sensitive data accessible to the importing process.
  • Lateral movement within the environment running the generator or importer.
  • Integrity compromise of downstream services relying on generated models.

General mitigation guidance

Effective mitigation focuses on re-establishing a strict separation between data and code in code generation templates and reducing trust in schema-supplied values:

  • Constrain customBasePath to a tightly validated set of characters (e.g., Python identifier and dot-separated module path syntax) and reject values containing newlines or other control characters.
  • Use structured APIs to build import paths instead of string concatenation, or map untrusted values to safe internal identifiers.
  • Treat all schema-derived text as untrusted and apply rigorous validation or encoding before embedding it into generated code.
  • Execute code generation in a least-privilege, isolated environment so that any unexpected code in generated modules has limited impact.
  • Consider designs where external configuration influences data structures, not raw code fragments, to avoid code generation paths that can be steered by user input.

How teams detect issues like this

Detection can rely on both code-centric and process-centric approaches:

  • Static analysis of the generator: Search for patterns where user-controlled configuration fields are interpolated into Python code templates (especially from ... import ... or import statements) without validation.
  • Template and code review: Flag any code generation logic that allows arbitrary strings from schemas or configs to influence executable Python constructs, enforcing a policy that only validated identifiers are allowed.
  • Automated scanning of generated code: Run linters or custom rules on generated modules to detect suspicious constructs such as unexpected newlines in import statements or non-literal import targets.
  • Pipeline monitoring: In CI/CD or automated services, log and review schemas and generated outputs associated with untrusted sources, and alert on unusual or complex customBasePath patterns.
  • Organizations may also use SAST tools or custom pre-commit hooks tailored to detect unsafe code generation templates.

Sources

SourceLink
github.comgithub.com/koxudaxi/datamodel-code-generator
github.comgithub.com/koxudaxi/datamodel-code-generator/commit/545a96c5
github.comgithub.com/rahulreddykarne/CVE-2026-63720-datamodel-code-generator
rahulkarne.comrahulkarne.com/#/cve/CVE-2026-63720
vulncheck.comvulncheck.com/advisories/datamodel-code-generator-code-injection-via-unvalidated-custombasepath-schema-field

Frequently asked

questions

It can be, if an attacker can submit or influence schemas consumed by datamodel-code-generator over a network interface (for example, via an API or integration that accepts external schemas) and the generated modules are later imported automatically.

The attacker needs the ability to control or modify the input schema, specifically the customBasePath field, that the generator processes. No direct access to the runtime environment is required beyond influencing the schema.

The safest approach is to eliminate direct embedding of untrusted schema values into Python code. Validate customBasePath as a safe module path or map it to internal, predefined identifiers, and ensure that no schema-controlled field can alter Python syntax in generated files.

Design code generation systems under the assumption that all external inputs are untrusted, enforce strict validation and encoding on any values that influence generated code, and incorporate security-focused reviews and automated checks for injection patterns in templates and generator logic.

Find out what's already
exploitable in your codebase.