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
customBasePathvalue is inserted verbatim into afrom ... 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
customBasePathto 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 ...orimportstatements) 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
customBasePathpatterns. - Organizations may also use SAST tools or custom pre-commit hooks tailored to detect unsafe code generation templates.

