Code injection in datamodel-code-generator default_factory handling
CVE-2026-54653 is a code injection vulnerability in datamodel-code-generator affecting versions 0.17.0 through 0.60.1. The flaw arises during JSON Schema parsing, where attacker-controlled default_factory values are preserved in the parser logic and propagated through JsonSchemaObject.init and get_field_extras into generated Python source. The generator emits these values into Field(default_factory=...) or field(default_factory=...) constructs in generated Pydantic or dataclass models. Because the injected value is rendered as executable Python expression syntax, importing the generated model can trigger arbitrary Python expression execution in the importing process. The issue was fixed in version 0.60.2.
What it means and what to do now: patch path, interim mitigations, and the assume-compromise checklist.
What an attacker gets, and what they’ve been doing with it.
Successful exploitation allows arbitrary Python code execution in the context of the user or process that imports the generated model module. This can lead to compromise of developer workstations, CI/CD runners, or other automation systems that generate and then import models from untrusted schemas. Likely consequences include unauthorized filesystem access, environment and secret exfiltration, outbound network activity, integrity compromise of generated code or build artifacts, and full loss of confidentiality, integrity, and availability within the affected execution context.
If you can’t patch tonight, do this now.
Do not generate Python models from untrusted or third-party schemas until the patched version is deployed. If model generation from untrusted input is unavoidable, prefer output formats such as TypedDict that do not emit field or Field default_factory calls and therefore drop this attack surface. Avoid importing previously generated Python modules derived from untrusted schemas, and isolate schema processing and code generation in constrained environments with minimal privileges until remediation is complete.
Patch, then assume compromise.
Upgrade datamodel-code-generator to version 0.60.2 or later. The fix restricts schema-supplied default_factory values to supported safe factory names such as dict, list, and set, and rejects unsupported values before code generation. Regenerate any models produced from potentially untrusted schemas using the patched version, and review previously generated code for unsafe default_factory usage before importing it into production, development, or CI environments.
No public exploits tracked yet. Mallory keeps watching.
Products and vendors Mallory has correlated with this vulnerability. Open in Mallory to drill down to specific CPE configurations and version ranges.
4 sources tracked across advisories, community write-ups, and news. New activity surfaces here as Mallory finds it.