CVE-2026-54656 Overview
CVE-2026-54656 is a code injection vulnerability [CWE-94] in datamodel-code-generator, a Python tool that generates Pydantic v2 models, dataclasses, TypedDict, and msgspec.Struct from schemas such as OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, JSON, YAML, and CSV. Versions from 0.52.1 up to (but not including) 0.60.2 interpolate validator content from --extra-template-data into @field_validator decorators without safe validation. When a developer imports the generated Pydantic v2 model, attacker-controlled Python code executes in the developer's context. The issue is fixed in version 0.60.2.
Critical Impact
Attackers who control --extra-template-data input can achieve arbitrary Python code execution when the generated model file is imported, compromising developer machines and CI/CD pipelines.
Affected Products
- datamodel-code-generator versions 0.52.1 through 0.60.1
- Downstream projects generating Pydantic v2 models from untrusted schema or template data inputs
- CI/CD pipelines invoking datamodel-code-generator on external schemas
Discovery Timeline
- 2026-07-28 - CVE-2026-54656 published to NVD
- 2026-07-29 - Last updated in NVD database
Technical Details for CVE-2026-54656
Vulnerability Analysis
The vulnerability resides in src/datamodel_code_generator/model/pydantic_v2/base_model.py. The _process_validators function reads validator definitions supplied via the --extra-template-data command-line option and interpolates them directly into @field_validator decorators in the generated model source. Because the interpolated content is emitted as raw Python, any expression provided as template data becomes executable code once the resulting module is imported.
The attack requires user interaction: a developer or automation job must import the generated model. Impact spans confidentiality, integrity, and availability because the injected code runs with the privileges of the importing process.
Root Cause
The root cause is unsanitized templating of validator strings from external configuration into generated Python source. _process_validators treated --extra-template-data values as trusted, performing no syntactic or semantic validation before writing them into decorator bodies. This design conflates configuration data with executable code, which classifies as improper control of generation of code [CWE-94].
Attack Vector
An attacker supplies crafted validator content through --extra-template-data, either by controlling the JSON template-data file passed on the command line or by influencing an upstream configuration that a build system feeds into datamodel-code-generator. When the generated Pydantic v2 model is imported, the injected Python payload executes locally.
# Patch excerpt: src/datamodel_code_generator/validators.py
# Adds safe validation primitives (ValidationError, field_validator, keyword)
# and enforces a minimum dotted-path structure for validator references.
from __future__ import annotations
import keyword
from enum import Enum
from typing import Any
from pydantic import BaseModel, RootModel, ValidationError, field_validator
_MIN_DOTTED_PATH_PARTS = 2
class ValidatorMode(str, Enum):
...
Source: GitHub commit a43d029
# Patch excerpt: src/datamodel_code_generator/model/pydantic_v2/base_model.py
# Imports ValidationError so _process_validators can reject unsafe input
# instead of interpolating it verbatim into @field_validator decorators.
from collections import defaultdict
from typing import TYPE_CHECKING, Any, ClassVar, Literal, NamedTuple, Optional
from pydantic import Field, ValidationError, field_validator, model_validator
from datamodel_code_generator.imports import IMPORT_ANY, Import
from datamodel_code_generator.model import _rebuild_model_with_datamodel_namespace
Source: GitHub commit a43d029
Detection Methods for CVE-2026-54656
Indicators of Compromise
- Generated Pydantic v2 model files containing unexpected imports, subprocess calls, or network I/O inside @field_validator decorator bodies.
- datamodel-code-generator invocations that pass --extra-template-data sourced from untrusted or externally writable locations.
- Unexplained outbound connections or child processes spawned by Python processes shortly after importing recently generated schema modules.
Detection Strategies
- Diff newly generated model files against previous known-good versions and flag validator bodies that contain executable statements beyond simple type checks.
- Inventory installed versions of datamodel-code-generator across developer workstations and build agents; alert on any version between 0.52.1 and 0.60.1.
- Audit CI/CD job definitions for datamodel-code-generator steps that consume template-data files from pull requests, third-party repositories, or shared artifact stores.
Monitoring Recommendations
- Monitor developer endpoints and build runners for anomalous process trees where python imports generated modules and then executes shells, package managers, or credential-access tools.
- Log and review changes to --extra-template-data JSON files in source control and configuration management systems.
- Track outbound network connections initiated by Python interpreters during code-generation and test phases of the build pipeline.
How to Mitigate CVE-2026-54656
Immediate Actions Required
- Upgrade datamodel-code-generator to version 0.60.2 or later on all developer machines, build servers, and container images.
- Regenerate any Pydantic v2 models produced by affected versions after the upgrade and review the resulting files for unexpected code in validators.
- Restrict write access to --extra-template-data input files to trusted maintainers only.
Patch Information
The fix is delivered in datamodel-code-generator 0.60.2. The remediation, documented in GitHub Security Advisory GHSA-8m8r-38jm-f355 and implemented in commit a43d029, introduces Pydantic-based validation of validator entries and enforces structural checks such as _MIN_DOTTED_PATH_PARTS on dotted validator references before they are emitted into generated code.
Workarounds
- Avoid using --extra-template-data until the upgrade is complete, or supply only static template-data files reviewed by trusted maintainers.
- Run datamodel-code-generator inside an isolated, unprivileged container or sandbox so that any injected code cannot reach developer credentials or production secrets.
- Treat generated model files as build artifacts that require code review before being imported by application or test code.
# Upgrade to the patched release
pip install --upgrade 'datamodel-code-generator>=0.60.2'
# Verify the installed version
datamodel-codegen --version
# Pin the minimum safe version in requirements.txt
echo 'datamodel-code-generator>=0.60.2' >> requirements.txt
Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

