Import-time code execution in @orval/fetch via unescaped OpenAPI response status key emitted into a generated TypeScript type
Description
The @orval/fetch client generator emits an OpenAPI Responses Object status key verbatim into a TypeScript type position of the generated code. The shared helper getStatusCodeType in packages/core/src/utils/http-status.ts returns any key that is not a recognized status code or NXX wildcard unchanged, and the fetch generator splices that value directly into an emitted export type ... = { data: ...; status: <KEY> }. A response key crafted to close the type object early turns the remainder of the key into a top-level statement in the generated module, so importing that module through a type-stripping build (esbuild, Vite, tsx, swc, or Babel) runs attacker-chosen JavaScript at import time. Exploitation requires the victim to generate against a document with input.unsafeDisableValidation set to true and to use a type-stripping (not type-checking) build. The @orval/axios client contains the same raw-key emission in an opt-in code path.
Root Cause
The security invariant is that an untrusted OpenAPI document must never be able to place executable code into generated source. The generator relies on the schema validator (@scalar/openapi-parser) to reject non-conforming Responses Object keys, but it does no neutralization at the emission sink itself. When validation is disabled, getStatusCodeType returns the raw key and the fetch emitter interpolates it into an unquoted TypeScript type position, so the key content becomes generated code rather than data. The project already recognized this exact class for the mock (MSW) generator and neutralizes the key at the sink there with assertSafeStatusCode, which runs unconditionally in the generator regardless of the validation flag. The fetch and axios emitters and the shared getStatusCodeType helper were never given equivalent sink-side protection.
Affected functions:
- getStatusCodeType, packages/core/src/utils/http-status.ts (returns the raw key at line 23)
- generateRequestFunction (fetch response-type emitter), packages/fetch/src/index.ts (interpolates the raw key into an emitted `status: <KEY>` type position; default on)
- getAxiosResponseTypes and getEmptyResponseStatusCondition, packages/axios/src/index.ts (emit the raw key into type positions and a runtime `response.status === <KEY>` condition; opt-in)Impact
An attacker who controls or supplies an OpenAPI document consumed by a victim developer can achieve arbitrary code execution in that developer's build or runtime process at the moment the generated fetch client module is imported. The prerequisites are that the victim runs generation with input.unsafeDisableValidation set to true and then builds or imports the generated module with a toolchain that strips TypeScript types without type checking (esbuild, Vite, tsx, swc, Babel). The fetch response-type emission is on by default (override.fetch.includeHttpResponseReturnType defaults to true). Demonstrated capability is import-time execution of an arbitrary statement; from there the attacker has the full authority of the developer's process (file read and write, process control, and denial of service are all plausible direct consequences of arbitrary code execution). The axios path additionally requires override.axios.includeHttpResponseReturnType set to true (default false) and is therefore a weaker, opt-in variant of the same root cause.
Proof of Concept
Dependencies: node, esbuild, and @orval/fetch / @orval/core at a version in the affected range. The self-contained reproduction reuses the exact getStatusCodeType behavior and fetch emission shape taken from source, then drives the generated code through esbuild to demonstrate import-time execution.
Steps:
- Author an OpenAPI document whose operation has a
responsesmap with a normal200entry and a second key set to the payload below. - Run orval with
client: 'fetch'andinput.unsafeDisableValidation: true. - Build or import the generated client through a type-stripping loader (esbuild
loader: 'ts', Vite, tsx, or swc).
// Reproduces http-status.ts getStatusCodeType (returns the raw key) and the
// fetch emitter shape, then transpiles with esbuild (a type-stripping build).
const esbuild = require('esbuild');
function getStatusCodeType(key) {
if (/^[1-5]XX$/i.test(key)) return `HTTPStatusCode${key[0]}xx`;
return key; // raw key: the root cause
}
function emit(name, key) {
return `export type ${name} = {\n data: unknown\n status: ${getStatusCodeType(key)}\n}\nexport const marker = 42;\n`;
}
const proof = 'require("fs").writeFileSync(__dirname+"/RCE_PROOF","import-time-rce\\n")';
const key = `number }; ${proof}; type _Ignore = { _z: number`;
const js = esbuild.transformSync(emit('GetThingResponseInjected', key), { loader: 'ts', format: 'cjs' }).code;
const fs = require('fs'), path = require('path');
const gen = path.join(__dirname, `.gen-${Date.now()}.cjs`);
fs.writeFileSync(gen, js);
require(gen); // import-time
console.log('import-time RCE fired:', fs.existsSync(path.join(__dirname, 'RCE_PROOF')));=== A: arbitrary key (needs unsafeDisableValidation) -> IMPORT-TIME RCE ===
>> import-time RCE fired: true
=== B: x-* key (passes default validation) -> BLOCKED at type parse ===
>> transpiler PARSE ERROR (breakout blocked): Unexpected "-"Attack Chain
- Exposure: A victim developer runs the orval fetch client generator against an OpenAPI document authored or served by the attacker (for example a third-party or remotely fetched spec).
- Control: The attacker sets a Responses Object key to a value such as
number }; <JS>; type _Ignore = { _z: number. The key content is fully attacker controlled. - Guard: By default
@scalar/openapi-parservalidate()(invoked inpackages/orval/src/import-specs.ts) rejects the malformed key. This guard is skipped only when the victim setsinput.unsafeDisableValidationto true, which prints a warning framed as a stability disclaimer. - Path: With validation disabled,
getResponseiterates the responses viaObject.entrieswith no key filtering, the key flows throughgetResReqTypesintotype.key, and the fetch emitter callsgetStatusCodeType(r.key), which returns it verbatim (packages/core/src/utils/http-status.tsline 23). - Guard: The emitted module would be rejected by a type-checking build (
tsc --noEmitOnError), but a type-stripping build (esbuild, Vite, tsx, swc, Babel) emits and runs it without type checking. - Primitive: The raw key closes the
status: ...type object early, so the injected fragment becomes a top-level statement in the generated module and a trailing dummy type absorbs the closing brace. - Result: Importing the generated fetch client executes the injected statement, giving the attacker arbitrary code execution in the developer's build or runtime process.
Bypass Evidence
The relevant guard is the schema validator. An independent test using @scalar/openapi-parser version 0.28.16 (which satisfies the pinned ^0.28.11 dependency, and the pin is present at the 8.30.0, 8.31.0, and 8.32.0 releases) established the following. The breakout key number }; require(...); type _Ignore = { _z: number is rejected (valid is false). The maintainer's own example malicious key of the comma-expression form is rejected. Plain non-status keys such as abc, four-digit values such as 2000, and 20X are rejected. Only default, [1-5]XX wildcards, three-digit status codes, and x- prefixed extension keys pass validation. The sole validation-passing, attacker-flexible shape is an x- extension key, and emitting status: x-... is a hard TypeScript parse error in a type position (Unexpected "-"), so it does not produce code execution. Therefore, with validation enabled the type sink is not exploitable, and the breakout requires input.unsafeDisableValidation set to true.
The strongest argument against reporting this is that the victim explicitly set a flag named unsafeDisableValidation and was warned. That argument does not hold here because the maintainers do not treat the validator as the security boundary for status-key emission: their fix for the mock generator (assertSafeStatusCode in packages/mock/src/msw/index.ts) neutralizes the key at the sink and runs unconditionally in the generator, even when validation is disabled, and its code comment states the intent to refuse the document rather than emit whatever it says. That fix is present in all three released versions but does not touch getStatusCodeType or the fetch and axios emitters, which remain exposed. This is therefore a distinct, unfixed sibling of that remediation rather than a bypass of it.
Affected Versions
Ecosystem: npmPackage: @orval/fetch (primary; shared helper in @orval/core; @orval/axios opt-in secondary)Confirmed affected range: >= 8.30.0, <= 8.32.0Latest release checked: 8.32.0 (npm registry; equals the analyzed source revision)Fix status: not fixed
The fetch raw-key sink (with the response-type emission on by default) was verified present in the released artifacts at 8.30.0, 8.31.0, and 8.32.0. At 8.30.0 and 8.31.0 the helper is inline in the fetch package; it was refactored into the shared @orval/core helper at 8.32.0, at which point the axios path and the shared helper also carry the raw-key emission. The introduction boundary below 8.30.0 could not be established from the available history and is not asserted. No released or unreleased fix exists on the latest revision for the fetch or axios path.
Suggested Fix
Enforce the status-key invariant at the emission sink rather than relying on the schema validator. Neutralize or reject any Responses Object key that is not a valid status code inside getStatusCodeType (packages/core/src/utils/http-status.ts), mirroring the existing assertSafeStatusCode used by the mock generator. Because the fetch and axios emitters both route through this helper, a single sink-side check closes the fetch type sink, the axios type sink, and the axios runtime response.status === <KEY> condition at once. As an interim mitigation, avoid setting input.unsafeDisableValidation to true on untrusted documents, and prefer a type-checking build step so that a malformed generated type fails the build rather than being stripped and executed.
Reported by zx (GitHub: @manus-pi).