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-typevalues 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.

