/

CVE-2026-55389

Severity:

HIGH RISK

(7.5)

Path Traversal (CWE-22) in datamodel-code-generator

CVE-2026-55389

Analyzed

Vulnerability type

Category:

Path Traversal / Insecure Resource Resolution

CWE:

CWE-22, CWE-200, CWE-610

Impact:

Arbitrary Local File Read / Information Disclosure

Severity:

HIGH RISK

(7.5)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

datamodel-code-generator improperly resolves JSON Schema $ref values, allowing schema references to access arbitrary local files outside the intended input directory.

When an attacker can influence or provide OpenAPI/JSON Schema inputs, they may abuse $ref resolution to read sensitive files from the generator's filesystem, even when remote references are supposedly disabled.

datamodel-code-generator improperly resolves JSON Schema $ref values, allowing schema references to access arbitrary local files outside the intended input directory.

When an attacker can influence or provide OpenAPI/JSON Schema inputs, they may abuse $ref resolution to read sensitive files from the generator's filesystem, even when remote references are supposedly disabled.

Affected context

This vulnerability arises when datamodel-code-generator is used to generate models (Pydantic v2, dataclasses, TypedDict, msgspec.Struct) from untrusted or partially trusted specifications such as OpenAPI or JSON Schema.

It affects workflows where $ref resolution is performed against user-supplied schemas, especially in CI pipelines, web-based generators, or shared tooling environments that run with access to sensitive files on the host.

This vulnerability arises when datamodel-code-generator is used to generate models (Pydantic v2, dataclasses, TypedDict, msgspec.Struct) from untrusted or partially trusted specifications such as OpenAPI or JSON Schema.

It affects workflows where $ref resolution is performed against user-supplied schemas, especially in CI pipelines, web-based generators, or shared tooling environments that run with access to sensitive files on the host.

Exploit preconditions

  • Attack surface: JSON Schema/OpenAPI/other spec inputs consumed by datamodel-code-generator.
  • Required access: Ability to submit, upload, or modify schemas that the tool processes (e.g., via a web service, automation pipeline, or shared repo).
  • Trigger point: During $ref resolution in parser/jsonschema.py, when is_url and _get_ref_body process references without restricting file:// schemes or ../ traversal.
  • Why exploitation is possible: The resolver trusts $ref targets and fails to constrain them to the intended input directory or configuration flags, enabling references to arbitrary local paths.
  • Attack surface: JSON Schema/OpenAPI/other spec inputs consumed by datamodel-code-generator.
  • Required access: Ability to submit, upload, or modify schemas that the tool processes (e.g., via a web service, automation pipeline, or shared repo).
  • Trigger point: During $ref resolution in parser/jsonschema.py, when is_url and _get_ref_body process references without restricting file:// schemes or ../ traversal.
  • Why exploitation is possible: The resolver trusts $ref targets and fails to constrain them to the intended input directory or configuration flags, enabling references to arbitrary local paths.

Why this happens

The core issue is insufficient validation and restriction of external entity references in JSON Schema resolution logic.

The $ref handling code uses helpers like is_url and _get_ref_body to fetch referenced content but does not enforce a strict root directory, sanitize traversal sequences, or reject local file:// references when remote refs should be disabled.

This aligns with:

  • CWE-22 (Improper Limitation of a Pathname to a Restricted Directory): $ref paths can escape the intended schema directory via traversal or local URI schemes.
  • CWE-610 (ExternaI Control of File Name or Path): Attackers control the $ref values that drive file selection.
  • Resulting in CWE-200 (Information Exposure): Local files not meant to be part of the schema processing can be read and surfaced as part of model generation.

The core issue is insufficient validation and restriction of external entity references in JSON Schema resolution logic.

The $ref handling code uses helpers like is_url and _get_ref_body to fetch referenced content but does not enforce a strict root directory, sanitize traversal sequences, or reject local file:// references when remote refs should be disabled.

This aligns with:

  • CWE-22 (Improper Limitation of a Pathname to a Restricted Directory): $ref paths can escape the intended schema directory via traversal or local URI schemes.
  • CWE-610 (ExternaI Control of File Name or Path): Attackers control the $ref values that drive file selection.
  • Resulting in CWE-200 (Information Exposure): Local files not meant to be part of the schema processing can be read and surfaced as part of model generation.

Potential impact

  • Attackers may read sensitive configuration files from the host system.
  • Schema processing could expose credentials, API keys, or access tokens stored in local files.
  • Internal source code or proprietary schemas may be disclosed through generated outputs.
  • Local environment details (paths, structure, internal documentation) can be inferred or leaked, supporting further attacks.
  • Attackers may read sensitive configuration files from the host system.
  • Schema processing could expose credentials, API keys, or access tokens stored in local files.
  • Internal source code or proprietary schemas may be disclosed through generated outputs.
  • Local environment details (paths, structure, internal documentation) can be inferred or leaked, supporting further attacks.

General mitigation guidance

Effective mitigation focuses on constraining reference resolution and reducing trust in external schema inputs.

Common defensive patterns include:

  • Enforcing a strict root directory for all $ref resolution and rejecting paths that attempt traversal outside this boundary.
  • Disallowing file:// and similar local URI schemes in user-controlled $ref values unless explicitly whitelisted.
  • Treating schema files as untrusted input, applying validation and normalization to reference paths before resolution.
  • Running generators with least-privilege file system access so that sensitive directories are not readable even if traversal is attempted.
  • Providing configuration options that default to safe behavior, such as disabling remote or out-of-tree references unless explicitly opted in.

Effective mitigation focuses on constraining reference resolution and reducing trust in external schema inputs.

Common defensive patterns include:

  • Enforcing a strict root directory for all $ref resolution and rejecting paths that attempt traversal outside this boundary.
  • Disallowing file:// and similar local URI schemes in user-controlled $ref values unless explicitly whitelisted.
  • Treating schema files as untrusted input, applying validation and normalization to reference paths before resolution.
  • Running generators with least-privilege file system access so that sensitive directories are not readable even if traversal is attempted.
  • Providing configuration options that default to safe behavior, such as disabling remote or out-of-tree references unless explicitly opted in.

How teams detect issues like this

Detection typically combines code-level analysis and operational monitoring:

  • Use static analysis or manual code review to identify file resolution logic that accepts user-controlled $ref or path values without normalization or boundary checks.
  • Flag usages of open(), URL/URI handlers, or custom loaders in schema parsers where path constraints are missing or optional.
  • Implement security-focused tests that submit crafted schemas with ../ segments or file:// URIs to verify they are rejected or safely handled.
  • Apply policy enforcement in CI/CD to restrict introduction of new schema resolution mechanisms that do not honor secure defaults.
  • Complement code controls with host-level monitoring for unexpected file access patterns from tooling processes that handle untrusted input.

Detection typically combines code-level analysis and operational monitoring:

  • Use static analysis or manual code review to identify file resolution logic that accepts user-controlled $ref or path values without normalization or boundary checks.
  • Flag usages of open(), URL/URI handlers, or custom loaders in schema parsers where path constraints are missing or optional.
  • Implement security-focused tests that submit crafted schemas with ../ segments or file:// URIs to verify they are rejected or safely handled.
  • Apply policy enforcement in CI/CD to restrict introduction of new schema resolution mechanisms that do not honor secure defaults.
  • Complement code controls with host-level monitoring for unexpected file access patterns from tooling processes that handle untrusted input.

Sources

SourceLink
github.comgithub.com/koxudaxi/datamodel-code-generator/commit/2ff4a72b4550a2b2069754c5b075b1655067e5fb
github.comgithub.com/koxudaxi/datamodel-code-generator/releases/tag/0.62.0
github.comgithub.com/koxudaxi/datamodel-code-generator/security/advisories/GHSA-8359-h9fx-j6v9

Frequently asked

questions

It can be, if datamodel-code-generator is exposed through a remote service or automation that accepts schemas from external users. In such cases, attackers may supply malicious $ref values to read local files on the server.

The attacker must be able to influence or provide the OpenAPI/JSON Schema (or other supported formats) that the generator processes. They do not need shell access to the host, only control over the schema content.

Constrain $ref resolution to a predefined directory root, reject traversal and local URI schemes by default, and treat remote or out-of-tree references as opt-in with explicit validation. Combine this with least-privilege file system access for the generator process.

Design schema and configuration parsers with strict trust boundaries: normalize and validate all externally controlled paths, enforce directory sandboxing, and prefer deny-by-default behavior for remote or local references. Add automated checks and tests around file resolution code to catch unsafe patterns early.

It can be, if datamodel-code-generator is exposed through a remote service or automation that accepts schemas from external users. In such cases, attackers may supply malicious $ref values to read local files on the server.

The attacker must be able to influence or provide the OpenAPI/JSON Schema (or other supported formats) that the generator processes. They do not need shell access to the host, only control over the schema content.

Constrain $ref resolution to a predefined directory root, reject traversal and local URI schemes by default, and treat remote or out-of-tree references as opt-in with explicit validation. Combine this with least-privilege file system access for the generator process.

Design schema and configuration parsers with strict trust boundaries: normalize and validate all externally controlled paths, enforce directory sandboxing, and prefer deny-by-default behavior for remote or local references. Add automated checks and tests around file resolution code to catch unsafe patterns early.

Find out what's already
exploitable in your codebase.