Skip to main content
Menu
Advisories

Import-time RCE in @orval/zod via unvalidated schema type emitted as a Zod method name

GHSA-V263-CP2V-VRRXPublished Updated GitHub

Description

The Zod client generator emits an OpenAPI schema's scalar type value into generated TypeScript as a Zod method name without any allow-list validation or escaping. An attacker who controls or influences an OpenAPI 3.1 document supplied to Orval (for example a partner specification, a specification fetched at build time, or a specification contributed via a pull request) can set a response or request-body schema type to a JavaScript payload. Because a top-level schema is rendered as a module-scope statement (export const XResponse = zod.<injected>()), importing, type-checking, or bundling the generated module executes the attacker's JavaScript on the developer or CI host. This is import-time remote code execution reachable in Orval's default configuration.

Root Cause

resolveZodType validates the schema type against a fixed allow-set (possibleSchemaTypes) only on the OpenAPI 3.1 array-of-types branch. The single-string branch returns the raw specification string with no allow-list check, so an arbitrary attacker-controlled string becomes the resolved type. That string is then stored verbatim as a Zod method name and later emitted into generated source with no escaping, prefixed with zod. The security invariant that generated code must only contain method names drawn from a known-safe set is violated on the scalar-type path; the guard that enforces it on the array path is simply absent on the scalar path.

packages/zod/src/index.ts
  resolveZodType (lines 118-162): array branch filters possibleSchemaTypes.has(t)
    at line 126; single-string branch at line 145 returns the raw schema.type with
    no allow-list filter (returns `type ?? 'unknown'` at line 161).
  generateZodValidationSchemaDefinition (lines 1538-1649): default switch case reaches
    line 1647, functions.push([type === 'integer' ? 'int' : type, undefined]); the raw
    attacker string is stored as the Zod method name.
  parseZodValidationSchemaDefinition inner parseProperty renderer (lines 2456-2707):
    an unknown method name matches no known branch and falls through to line 2706,
    return `.${fn}(${combinedArgs})`; line 2732 prefixes `zod`, yielding
    `zod.<payload>()` at module scope. No getKey/JSON.stringify/jsStringEscape/camel/
    pascal/sanitize touches the value on this path.

Impact

An attacker who controls the type value of a top-level (response or request-body) schema in an OpenAPI 3.1 document processed by Orval's Zod client achieves arbitrary JavaScript execution at import, type-check, or bundle time of the generated module. No authentication, non-default configuration, or victim interaction beyond running the normal code-generation and build workflow is required; the default-on specification validator accepts the arbitrary scalar type. The demonstrated capability is execution of an arbitrary shell command (child_process.execSync('id')) on the machine that imports the generated file, which in a typical developer or CI setting grants full code execution in that build environment. A property-nested schema with the same payload instead breaks the surrounding object literal and yields a build-breaking syntax error (denial of service); the top-level position is the code-execution instance.

Proof of Concept

Requires Node.js, the published orval 8.32.0 CLI, and the zod runtime package. Generate the Zod client from the malicious spec, then import the generated module.

  1. Create an OpenAPI 3.1 document whose GET /x 200 response schema has the malicious scalar type.
  2. Run the Orval CLI with a default Zod client configuration to generate the client.
  3. Import (or type-check/bundle) the generated module in a CommonJS build.
{
  "openapi": "3.1.0",
  "info": { "title": "evil", "version": "1.0.0" },
  "paths": {
    "/x": {
      "get": {
        "operationId": "getX",
        "responses": {
          "200": {
            "description": "ok",
            "content": {
              "application/json": {
                "schema": { "type": "string();globalThis.__ORVAL_PWNED__=require('child_process').execSync('id').toString();//" }
              }
            }
          }
        }
      }
    }
  }
}
// generated zod.ts (verbatim, module scope):
export const GetXResponse = zod.string();globalThis.__ORVAL_PWNED__=require('child_process').execSync('id').toString();//()

// on import, with the real zod package:
__ORVAL_PWNED__ = "uid=1000(ubuntu) gid=1000(ubuntu) groups=1000(ubuntu),4(adm),24(cdrom),27(sudo),30(dip),105(lxd)\n"

Attack Chain

  1. Exposure: An untrusted or attacker-influenced OpenAPI 3.1 document is supplied to the Orval CLI in its default configuration (partner spec, build-time fetched spec, or PR-contributed spec).
  2. Control: The attacker sets a top-level response or request-body schema's scalar type to an arbitrary string containing a JavaScript payload; the string survives parsing and the upgrade step unchanged.
  3. Path: The default-on validator accepts the document (the scalar type enum is not enforced for 3.1), so code generation proceeds; resolveZodType returns the raw string on the single-string branch.
  4. Guard: The possibleSchemaTypes allow-list that would reject an unknown type is applied only on the array-of-types branch (line 126) and is missing on the scalar branch (line 145); no escaping helper is applied to the value on this path.
  5. Primitive: The raw string is pushed as a Zod method name and emitted unescaped as zod.<payload>() at module scope in the generated file.
  6. Result: Importing, type-checking, or bundling the generated module executes the attacker's JavaScript on the developer or CI host, demonstrated by running an arbitrary shell command at import time.

Bypass Evidence

The relevant guard is the possibleSchemaTypes allow-list. On the array-of-types branch (line 126) the resolver filters each candidate with possibleSchemaTypes.has(t); on the single-string branch (line 145) it performs only an isString check and returns the value unchanged. A grep confirms possibleSchemaTypes is referenced only at line 126, never on the scalar path. The scalar value is not routed through any of the escaping or normalization helpers (getKey, JSON.stringify, jsStringEscape, camel, pascal, sanitize) that protect other emission sites; the emission renderer parseProperty matches the injected method name against no known branch and falls through to the raw .${fn}() template. Disproof attempts that failed: (1) the hypothesis that a strict 3.1 validator would reject an arbitrary type was refuted empirically, the shipped validator returned valid:true with no errors; (2) the hypothesis that an escaping helper covered the scalar path was refuted by tracing every branch; (3) fresh regeneration with the published 8.32.0 CLI produced byte-identical injected output and executing the module with the real zod package ran the injected command at import time. This is not covered by the prior key-quoting fix (GHSA-653q-5476-x79g), which quotes object keys via JSON.stringify and does not touch a method-name position; it is a distinct syntactic context with a distinct missing guard.

Affected Versions

  • Ecosystem: npm
  • Package: @orval/zod
  • Confirmed affected range: <= 8.32.0
  • Latest release checked: 8.32.0 (npm registry latest)
  • Fix status: not fixed

The vulnerable scalar-branch asymmetry is present in the shipped 8.32.0 source and no released fix is known. The lower bound cannot be established from the examined artifact because the repository history is squashed to 50 commits, so the introducing release is not diffable in-tree; the range is therefore stated conservatively as all releases up to and including the latest published version. The meta package orval ships in lockstep at the same version and pulls in the vulnerable generator.

Suggested Fix

Enforce the possibleSchemaTypes allow-list on the single-string branch of resolveZodType exactly as it is enforced on the array-of-types branch, so any type value not in the known-safe set resolves to a safe fallback (for example unknown) instead of being propagated. As defense in depth, the emission renderer should treat any method name that is not on an explicit allow-list of Zod methods as invalid rather than emitting it verbatim, and generated method names should be validated to be plain identifiers before being interpolated into source. Until a fixed release is available, run Orval only against fully trusted OpenAPI documents and review generated output before importing or building it.

Reported by zx (GitHub: @manus-pi).

Affected versions

References

GitHub · orval-labs/orval