CVE-2026-127208
Code injection via crafted x‑python‑type in JSON Schema leads to arbitrary exec in datamodel‑code‑generator.
- CVSS 8.6
- CWE-94
- Input Validation and Sanitization
- Remote
datamodel-code-generator before 0.84.0 contains a code injection vulnerability that allows attackers to achieve arbitrary code execution by supplying a malicious JSON Schema with a crafted x-python-type annotation containing a string ast.Constant node in a subscript slice position, which bypasses validation in parse_python_type_annotation and _python_type_expr_from_ast. Attackers can embed malicious Python expressions such as __import__ calls within quoted string literals in the generated source code, which Pydantic v2 evaluates as a ForwardRef via eval() during model_rebuild(), resulting in arbitrary code execution on developer workstations, CI/CD pipelines, or applications that generate and import the produced models at runtime.
- CWE
- CWE-94
- CVSS base score
- 8.6
- Published
- 2026-11-12
- OWASP
- A03 Injection
- Orthogonal defect classification
- Checking
- Code defect classification
- Missing Check
- Category
- Input Validation and Sanitization
- Subcategory
- Insecure Parsing or Deserialization
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- datamodel-code-generator
- Fixed by upgrading
- Yes
Solution
Upgrade datamodel‑code‑generator to version 0.84.0 or later.
Vulnerable code sample
import ast
from typing import Any, Dict
def generate_model(schema: Dict[str, Any]) -> str:
"""
Very simplified version of datamodel-code-generator's handling of
the custom x-python-type annotation.
"""
fields = []
for prop, definition in schema.get("properties", {}).items():
# VULNERABLE: directly embed unvalidated annotation string into AST
ann = definition.get("x-python-type", "str")
# Build a fake subscript expression like List["__import__('os').system('rm -rf /')"]
expr = ast.parse(f"typing.{ann}", mode="eval").body
# Convert AST back to source (simulating code generation)
annotation = ast.unparse(expr)
fields.append(f" {prop}: {annotation}")
return "\n".join(["class Model:", *fields])Patched code sample
import ast
from typing import Any, Dict
def generate_model(schema: Dict[str, Any]) -> str:
"""
Hardened version that validates the AST of x-python-type before use.
"""
fields = []
for prop, definition in schema.get("properties", {}).items():
# FIX: validate AST to allow only Name or Subscript with Name bases
ann = definition.get("x-python-type", "str")
expr = ast.parse(f"typing.{ann}", mode="eval").body
if not _is_safe_type_expr(expr):
raise ValueError("Unsafe type annotation")
annotation = ast.unparse(expr)
fields.append(f" {prop}: {annotation}")
return "\n".join(["class Model:", *fields])
def _is_safe_type_expr(node: ast.AST) -> bool:
"""
Allow only simple Name nodes and Subscript where the slice is a Name.
"""
if isinstance(node, ast.Name):
return True
if isinstance(node, ast.Subscript):
# base must be Name (e.g., List) and slice must be Name (e.g., int)
return isinstance(node.value, ast.Name) and isinstance(node.slice, ast.Name)
return FalsePublished fix
Public sources link this CVE to the changes below. The code samples above were generated to illustrate the defect; these links lead to the code the project actually changed.
- Merge commit from fork (datamodel-code-generator/datamodel-code-generator)
src/datamodel_code_generator/_python_type_annotation_codec.py,src/datamodel_code_generator/_python_type_annotation_codec.py · _python_type_expr_from_ast
Cite this entry
@misc{vaitp:cve2026127208,
title = {{Code injection via crafted x‑python‑type in JSON Schema leads to arbitrary exec in datamodel‑code‑generator.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2026},
note = {VAITP Python Vulnerability Dataset, entry CVE-2026-127208},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2026-127208/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
