{"id":"CVE-2026-54656","aliases":["CVE-2026-54656","GHSA-8m8r-38jm-f355","PYSEC-2026-3559"],"related":[],"kind":"vulnerability","state":"published","title":"`datamodel-code-generator` vulnerable to code execution on import via unescaped `validators` entries in --extra-template-data","summary":"datamodel-code-generator generates Pydantic v2 models, dataclasses, TypedDict, and msgspec.Struct from OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, and raw JSON, YAML, or CSV. From 0.52.1 until 0.60.2, datamodel-code-generator interpolates validators from --extra-template-data in src/datamodel_code_generator/model/pydantic_v2/base_model.py through _process_validators into @field_validator decorators without safe validation, allowing Python code execution when the generated Pydantic v2 model is imported. This issue is fixed in version 0.60.2.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"datamodel-code-generator generates Pydantic v2 models, dataclasses, TypedDict, and msgspec.Struct from OpenAPI, JSON Schema, GraphQL, Avro, Protobuf, and raw JSON, YAML, or CSV. From 0.52.1 until 0.60.2, datamodel-code-generator interpolates validators from --extra-template-data in src/datamodel_code_generator/model/pydantic_v2/base_model.py through _process_validators into @field_validator decorators without safe validation, allowing Python code execution when the generated Pydantic v2 model is imported. This issue is fixed in version 0.60.2.","source":"cvelist"},{"lang":"en","text":"### Summary\n\nWhen the Pydantic v2 output mode is in use, `datamodel-code-generator` reads a `validators` array from each model entry in the `--extra-template-data` file and synthesises a Pydantic `@field_validator(...)` decorator from each entry. The field names and the validator mode are interpolated into the decorator call wrapped in *unescaped* single quotes. A value containing `'` breaks out of the string literal, letting an attacker emit an arbitrary positional Python expression into the decorator. The expression is evaluated at class-definition time, i.e. the moment the developer imports the generated module. This is the same trust model as the recently-published GHSA-wjv6-jcfj-mf9r (extras-file comment injection) but the impact is full RCE rather than a docstring leak.\n\n### Details\n\nSink: `src/datamodel_code_generator/model/pydantic_v2/base_model.py`, `_process_validators` (lines 405–449, at tag `0.60.1` / commit `a321547e`):\n\n```python\ndef _process_validators(self) -> None:\n    validators = self.extra_template_data.get(\"validators\")\n    if not validators:\n        return\n    ...\n    for validator in validators:\n        fields = validator.get(\"fields\") or [validator.get(\"field\")]\n        fields = [f for f in fields if f]\n        if not fields:\n            continue\n        function_path: str = validator[\"function\"]\n        function_name = function_path.rsplit(\".\", 1)[-1]\n        mode = validator.get(\"mode\", \"after\")\n        fields_str = \", \".join(f\"'{f}'\" for f in fields)     # (A) UNESCAPED\n        ...\n        mode_str = f\"mode='{mode}'\"                          # (B) UNESCAPED\n        prepared_validators.append({\n            \"fields_str\": fields_str,\n            \"mode_str\":   mode_str,\n            \"method_name\": method_name,\n            \"function_name\": function_name,\n            \"mode\": mode,\n        })\n        self._additional_imports.append(Import.from_full_path(function_path))  # (C)\n```\n\nThe strings from (A) and (B) flow verbatim into `src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2`:\n\n```jinja\n@field_validator({{ v.fields_str }}, {{ v.mode_str }})\n```\n\nThere is no `repr()` call, no identifier check, and no quote-escaping.\n\nSecondary sink at (C): `Import.from_full_path(function_path)` splits on the last `.` and emits `from <prefix> import <suffix>`. A `;` in `function_path` therefore lands in the generated import line and runs as a statement at module load.\n\n### PoC\n\nA self-contained one-file PoC is available here: https://gist.github.com/thegr1ffyn/34d5c647e74487ffb2be27c76dace2aa\n\n### Impact\n\nArbitrary code execution in the developer's interpreter / CI runner the moment the generated module is imported. Anyone who accepts a `--extra-template-data` file from an untrusted source is impacted:\n\n- Pull requests adding or modifying project-local `*.template-data.json` / `.codegen.json` files consumed by a `make codegen` rule or pre-commit hook.\n- Configuration snippets pasted from issue templates, READMEs, or third-party guides.\n- Multi-tenant CI systems where one tenant's config file is read by another tenant's build.\n\nSame blast radius as GHSA-wjv6-jcfj-mf9r, but silent RCE rather than a docstring leak — significantly higher impact under the same threat model.\n\n\n \n> Introduced in 0.52.1 by commit [`a2b27562`](https://github.com/koxudaxi/datamodel-code-generator/commit/a2b27562) (*Add --validators option for Pydantic v2 field validators*).\n\n### Resolution\n\nThe fix validates `validators` entries with Pydantic models before rendering them. Field names must be valid non-keyword Python identifiers, `function` must be a dotted Python identifier path, and `mode` must be one of Pydantic's supported validator modes. The generated decorator arguments now render field names with `repr()` and mode with `!r`, so validated values are still emitted as Python string literals.\n\n### Remediation\n\nUpgrade to `datamodel-code-generator` `0.60.2` or later.\n\nThis issue affects `datamodel-code-generator` versions `>= 0.52.1, <= 0.60.1` and is fixed in `0.60.2`.\n\nSubmitted by: Hamza Haroon (thegr1ffyn)","format":"markdown","source":"ghsa"},{"lang":"es","text":"datamodel-code-generator genera modelos Pydantic v2, dataclasses, TypedDict y msgspec.Struct a partir de OpenAPI, JSON Schema, GraphQL, Avro, Protobuf y JSON, YAML o CSV en bruto. Desde 0.52.1 hasta 0.60.2, datamodel-code-generator interpola validadores desde --extra-template-data en src/datamodel_code_generator/model/pydantic_v2/base_model.py a través de _process_validators en decoradores @field_validator sin validación segura, permitiendo la ejecución de código Python cuando se importa el modelo Pydantic v2 generado. Este problema está corregido en la versión 0.60.2.","source":"nvd"}],"published":"2026-07-28T21:43:56.286Z","modified":"2026-10-23T23:10:00.157Z","severity":{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"},"severities":[{"scorer":"GitHub_M","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"},{"scorer":"GitHub","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"},{"scorer":"PyPA Advisory Database","scheme":"CVSS:3.1","vector":"CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H","score":7.8,"rating":"HIGH"}],"cwes":["CWE-94"],"affected":[{"ranges":[{"version":">= 0.52.1, < 0.60.2","status":"affected"}],"vendor":"koxudaxi","product":"datamodel-code-generator","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0.52.1","status":"affected","fixed":"0.60.2"}],"ecosystem":"PyPI","package":"datamodel-code-generator","purl":"pkg:pypi/datamodel-code-generator","source":"ghsa"},{"ranges":[{"status":"affected","introduced":"0.52.1","fixed":"0.60.2"}],"vendor":"koxudaxi","product":"datamodel-code-generator","cpe":"cpe:2.3:a:koxudaxi:datamodel-code-generator:*:*:*:*:*:*:*:*","source":"nvd"},{"ranges":[{"type":"ecosystem","introduced":"0.52.1","status":"affected","fixed":"0.60.2"}],"ecosystem":"PyPI","package":"datamodel-code-generator","purl":"pkg:pypi/datamodel-code-generator","source":"pysec"}],"references":[{"url":"https://github.com/koxudaxi/datamodel-code-generator/security/advisories/GHSA-8m8r-38jm-f355","tags":["exploit","vendor-advisory","web","x-refsource-confirm"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/koxudaxi/datamodel-code-generator/commit/a43d02906111a2fdcaf13ee5b62eb2da85376f19","tags":["patch","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/koxudaxi/datamodel-code-generator/releases/tag/0.60.2","tags":["release-notes","web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/koxudaxi/datamodel-code-generator","tags":["package"],"sources":["ghsa","pysec"]},{"url":"https://pypi.org/project/datamodel-code-generator","tags":["package"],"sources":["pysec"]},{"url":"https://github.com/advisories/GHSA-8m8r-38jm-f355","tags":["advisory"],"sources":["pysec"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2026-54656","tags":["advisory"],"sources":["pysec"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"poc","automatable":"no","technicalImpact":"total","date":"2026-07-29T14:12:01.711Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-54656","url":"https://www.cve.org/CVERecord?id=CVE-2026-54656","licence":"CVE Terms of Use","modified":"2026-07-29T14:12:17.588Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-8m8r-38jm-f355","url":"https://github.com/advisories/GHSA-8m8r-38jm-f355","licence":"CC-BY-4.0","modified":"2026-08-04T14:40:45.155Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-54656","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-54656","licence":"Public domain","modified":"2026-10-23T23:10:00.157Z"},{"source":"pysec","name":"PyPA Advisory Database","id":"PYSEC-2026-3559","url":"https://osv.dev/vulnerability/PYSEC-2026-3559","licence":"CC-BY-4.0","modified":"2026-08-04T14:30:14.930Z"}],"updatedAt":"2026-11-25T22:59:30.387Z","epss":{"score":0.00171,"percentile":0.06748,"date":"2026-12-01T12:00:22.000Z"}}