{"id":"CVE-2026-54654","aliases":["CVE-2026-54654","GHSA-wjv6-jcfj-mf9r","PYSEC-2026-3566"],"related":[],"kind":"vulnerability","state":"published","title":"`datamodel-code-generator` vulnerable to code injection via unescaped carriage return in `--extra-template-data` `comment` field","summary":"datamodel-code-generator generates Python data models from schema definitions. From 0.14.1 until 0.60.2, the --extra-template-data comment field is rendered into Python comments in src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2, src/datamodel_code_generator/model/template/TypedDict.jinja2, src/datamodel_code_generator/model/template/dataclass.jinja2, src/datamodel_code_generator/model/template/msgspec.Struct.jinja2, src/datamodel_code_generator/model/template/pydantic/BaseModel.jinja2, and src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2 without neutralizing carriage returns in Python # comments, allowing an attacker-controlled comment value to inject Python code into generated models that runs when imported. This issue is fixed in version 0.60.2.","summarySource":"cvelist","descriptions":[{"lang":"en","text":"datamodel-code-generator generates Python data models from schema definitions. From 0.14.1 until 0.60.2, the --extra-template-data comment field is rendered into Python comments in src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2, src/datamodel_code_generator/model/template/TypedDict.jinja2, src/datamodel_code_generator/model/template/dataclass.jinja2, src/datamodel_code_generator/model/template/msgspec.Struct.jinja2, src/datamodel_code_generator/model/template/pydantic/BaseModel.jinja2, and src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2 without neutralizing carriage returns in Python # comments, allowing an attacker-controlled comment value to inject Python code into generated models that runs when imported. This issue is fixed in version 0.60.2.","source":"cvelist"},{"lang":"en","text":"### Summary\n\n`datamodel-code-generator` is vulnerable to code injection when a developer passes an `--extra-template-data` file whose `comment` value contains a literal `\\r` (carriage return). The `comment` variable is rendered into a Python `#` comment in six built-in templates with **no** line-terminator escaping. Python's tokenizer treats a bare CR as a physical-line terminator (see [Python language reference — Physical lines](https://docs.python.org/3/reference/lexical_analysis.html#physical-lines)), so the comment ends at the `\\r` and the text after it is parsed as Python, including, when the CR is followed by suitable indentation, as a statement within the class body that follows on the next template line.\n\n### Details\n\nThe vulnerable templates each contain `# {{ comment }}` with no escaping:\n\n- `src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2:12` and `:19`\n- `src/datamodel_code_generator/model/template/TypeAliasType.jinja2:12` and `:19`\n- `src/datamodel_code_generator/model/template/TypeStatement.jinja2:12` and `:19`\n- `src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2:4`\n- `src/datamodel_code_generator/model/template/pydantic_v2/RootModel.jinja2:19`\n- `src/datamodel_code_generator/model/template/pydantic_v2/RootModelTypeAlias.jinja2:13`\n\nThe `pydantic_v2/BaseModel.jinja2:4` site is representative:\n\n```jinja2\nclass {{ class_name }}({{ base_class }}):{% if comment is defined %}  # {{ comment }}{% endif %}\n```\n\nWhen the developer-supplied extras file populates `comment` for a model, the value reaches the template via `DataModel.extra_template_data` (set in `src/datamodel_code_generator/model/base.py:736-742`) and Jinja2 interpolates it raw. None of the templates use `comment_safe`, `escape_docstring`, or any other line-terminator filter.\n\n### PoC\nComplete self contained POC is available at my secret gist: https://gist.github.com/thegr1ffyn/8ad6b8cb3cc2be9d3a0144aeb6896a3f\n\n### Impact\n\n- **Who's affected**: any developer or CI pipeline that runs `datamodel-codegen --extra-template-data <file>` where the extras file is influenced by attacker-controlled input. Realistic scenarios include:\n  - Extras file generated from a third-party schema-annotation system.\n  - Extras file vendored from an upstream repository.\n  - Extras file produced by a script that merges multiple `comment` sources.\n  - Build pipelines that template the extras file from environment variables, ticket descriptions, or commit metadata.\n- **What it gains**: arbitrary Python code execution in the importer's process at `import` time.\n- **What it does NOT need**: the schema itself can be entirely benign; only the extras file needs to contain the malicious `comment`.\n- **What does block it**: not passing `--extra-template-data`, or rejecting extras files whose `comment` values contain `\\r`, `\\x0b`, or `\\x0c` before invocation.\n\n### Resolution\n\nThe fix normalizes `comment` values from built-in `--extra-template-data` before template rendering. Inline comments now convert CRLF, bare CR, vertical tab, and form feed into LF and prefix continuation lines with `# `, so attacker-controlled text stays inside the generated Python comment block.\n\n### Remediation\n\nUpgrade to `datamodel-code-generator` `0.60.2` or later.\n\nThis issue affects `datamodel-code-generator` versions `>= 0.14.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 de datos Python a partir de definiciones de esquema. Desde la versión 0.14.1 hasta la 0.60.2, el campo de comentario --extra-template-data se renderiza en comentarios de Python en src/datamodel_code_generator/model/template/TypeAliasAnnotation.jinja2, src/datamodel_code_generator/model/template/TypedDict.jinja2, src/datamodel_code_generator/model/template/dataclass.jinja2, src/datamodel_code_generator/model/template/msgspec.Struct.jinja2, src/datamodel_code_generator/model/template/pydantic/BaseModel.jinja2, y src/datamodel_code_generator/model/template/pydantic_v2/BaseModel.jinja2 sin neutralizar los retornos de carro en los comentarios de Python #, permitiendo que un valor de comentario controlado por el atacante inyecte código Python en los modelos generados que se ejecuta al ser importado. Este problema está solucionado en la versión 0.60.2.","source":"nvd"}],"published":"2026-07-28T21:22:52.070Z","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","CWE-1336"],"affected":[{"ranges":[{"version":">= 0.14.1, < 0.60.2","status":"affected"}],"vendor":"koxudaxi","product":"datamodel-code-generator","source":"cvelist"},{"ranges":[{"type":"ecosystem","introduced":"0.14.1","status":"affected","fixed":"0.60.2"}],"ecosystem":"PyPI","package":"datamodel-code-generator","purl":"pkg:pypi/datamodel-code-generator","source":"ghsa"},{"ranges":[{"type":"ecosystem","introduced":"0.14.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-wjv6-jcfj-mf9r","tags":["exploit","web","x-refsource-confirm"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/koxudaxi/datamodel-code-generator/commit/b73abb5cd703a50471b8950bbd3bd0b82ad71de7","tags":["web","x-refsource-misc"],"sources":["cvelist","ghsa","nvd","pysec"]},{"url":"https://github.com/koxudaxi/datamodel-code-generator/releases/tag/0.60.2","tags":["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-wjv6-jcfj-mf9r","tags":["advisory"],"sources":["pysec"]},{"url":"https://nvd.nist.gov/vuln/detail/CVE-2026-54654","tags":["advisory"],"sources":["pysec"]}],"credits":[],"assigner":"GitHub_M","ssvc":{"scorer":"CISA-ADP","exploitation":"poc","automatable":"no","technicalImpact":"total","date":"2026-07-29T14:33:39.468Z"},"sources":[{"source":"cvelist","name":"CVE List","id":"CVE-2026-54654","url":"https://www.cve.org/CVERecord?id=CVE-2026-54654","licence":"CVE Terms of Use","modified":"2026-07-29T15:24:23.911Z"},{"source":"ghsa","name":"GitHub Advisory Database","id":"GHSA-wjv6-jcfj-mf9r","url":"https://github.com/advisories/GHSA-wjv6-jcfj-mf9r","licence":"CC-BY-4.0","modified":"2026-09-10T03:51:12.477Z"},{"source":"nvd","name":"NVD","id":"CVE-2026-54654","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-54654","licence":"Public domain","modified":"2026-10-23T23:10:00.157Z"},{"source":"pysec","name":"PyPA Advisory Database","id":"PYSEC-2026-3566","url":"https://osv.dev/vulnerability/PYSEC-2026-3566","licence":"CC-BY-4.0","modified":"2026-08-04T14:30:26.210Z"}],"updatedAt":"2026-11-25T22:59:30.386Z","epss":{"score":0.00142,"percentile":0.03754,"date":"2026-12-01T12:00:22.000Z"}}