/

CVE-2026-41134

Severity:

HIGH RISK

(7.8)

Code Injection in Generated Code in Kiota OpenAPI Client

CVE-2026-41134

Modified

Vulnerability type

Category:

Code Injection in Generated Code

CWE:

CWE-94

Impact:

Arbitrary Code Injection into Generated Client Applications

Severity:

HIGH RISK

(7.8)

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

Kiota versions prior to 1.31.1 contain a code generation literal injection flaw where untrusted values from an OpenAPI description can be emitted directly into generated client source code without proper escaping.

If an attacker can supply or tamper with the OpenAPI specification, they may inject additional code into generated clients by breaking out of string literals in areas such as serialization keys, URL templates, or enum metadata.

Affected context

This vulnerability affects environments that use Kiota (pre-1.31.1) to generate HTTP client code from OpenAPI descriptions.

It is most relevant when:

  • The OpenAPI description originates from an untrusted party, is fetched dynamically, or is stored in a location subject to tampering.
  • Generated clients are checked into source control or deployed without thorough code review.
  • Tooling or CI pipelines regenerate clients automatically from potentially unvalidated OpenAPI inputs.

In these situations, malicious specification content can propagate directly into application source code.

Exploit preconditions

  • Attack surface: The OpenAPI description consumed by Kiota during client generation.
  • Required access: Ability to provide, modify, or tamper with the OpenAPI document used as the input for generation (e.g., compromised spec repository, MITM on spec download, or poisoned artifact).
  • Trigger point: Running the Kiota generator against the malicious OpenAPI description to produce new or updated client code, followed by compiling and using that client in an application.
  • Why exploitation is possible: Multiple writer sinks in Kiota interpolate OpenAPI metadata (such as serialization/deserialization keys, path/query parameter mappings, URL template metadata, enum/property metadata, and default values) into string literals or code constructs without context-appropriate escaping. This enables an attacker-controlled value to terminate a literal and append arbitrary language-native code in the generated output.

Why this happens

The core issue is that Kiota treats OpenAPI description content as trusted when emitting code, embedding it directly into generated source without escaping or validation tailored to the target programming language and syntactic context.

This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where untrusted input influences code generation logic.

Instead of treating OpenAPI metadata as data to be safely encoded within literals or metadata structures, the generator uses raw values in positions where syntactic integrity of the surrounding code depends on proper quoting and escaping.

As a result, specially crafted fields in the specification can break out of the intended string or identifier boundaries and introduce attacker-controlled statements or expressions into the compiled client.

Potential impact

  • Injection of arbitrary code into generated client libraries, which may execute when the client is compiled or at runtime depending on language and placement.
  • Execution of attacker-influenced logic in applications that integrate the generated client, potentially leading to data exfiltration, request tampering, or privilege abuse.
  • Introduction of backdoors or logic bombs into otherwise trusted client codebases, impacting downstream applications that consume the generated libraries.
  • Compromise of build integrity and supply chain, especially in environments that consume generated clients as reusable packages.
  • Erosion of trust in API contracts and associated tooling, as malicious specifications can silently alter application behavior.

General mitigation guidance

Effective mitigation centers on treating OpenAPI specifications as untrusted inputs and hardening the code generation pipeline:

  • Apply context-aware escaping for all injected values: ensure that any OpenAPI-derived strings placed into literals, identifiers, annotations, or metadata are encoded according to the target language's syntax rules.
  • Introduce strict validation of OpenAPI documents before generation, rejecting or sanitizing fields that contain unexpected control characters, code-like constructs, or invalid tokens.
  • Maintain a trusted-specification model: keep authoritative OpenAPI descriptions in integrity-protected repositories or artifact stores; require signatures, checksums, or similar mechanisms before generation.
  • Design generation workflows so that generated code is reviewed like handwritten code, with linters, static analysis, and code review required before merging into main branches.
  • Limit the privileges and reach of generated clients so that even if a malicious specification influenced past code, the damage is constrained (for example, least-privilege credentials, isolated execution contexts, and minimal side effects).

How teams detect issues like this

Detection involves both code-centric and process-centric controls:

  • Static analysis patterns: Scan generated source code for suspicious constructs in areas typically derived from OpenAPI metadata (e.g., unusual characters in serialization keys, URL templates, or enum names; injected control structures within what should be passive configuration). Rules can flag literals with embedded terminators, interpolations, or unexpected language keywords.
  • Specification linting: Run validation and linting tools on OpenAPI descriptions to detect anomalies such as non-standard characters, code-like payloads, or fields exceeding typical length or format expectations.
  • Code review indicators: During review of generated clients, highlight and inspect any code paths or metadata that appear unusual, especially where names or values contain quotes, backslashes, line breaks, or language keywords.
  • Policy enforcement: Configure CI/CD pipelines so that code generation only uses OpenAPI descriptions from approved, integrity-verified locations, and block automatic regeneration from arbitrary remote URLs or unreviewed inputs.
  • Automated tooling: Integrate security-focused linters or SAST tools into the build process targeting generated code directories, treating them as first-class sources for potential injection flaws.

Sources

SourceLink
github.comgithub.com/microsoft/kiota/security/advisories/GHSA-2hx3-vp6r-mg3f

Frequently asked

questions

Indirectly. An attacker must influence the OpenAPI description used for code generation, which is often stored or retrieved through remote channels (e.g., remote spec endpoints, compromised repositories, or tampered artifacts). Once the malicious spec is in place, the generation process and subsequent use of the client complete the exploit chain.

The attacker needs the ability to provide or alter the OpenAPI document consumed by Kiota. This could be achieved via direct write access to the spec repository, compromising a spec publishing pipeline, or man-in-the-middle attacks on spec downloads if integrity checks are absent.

The safest pattern is to enforce context-appropriate escaping and validation for all values derived from OpenAPI metadata before inserting them into generated code, and to treat specification inputs as untrusted by default. Regenerating clients with hardened generators ensures that previously emitted unsafe constructs are replaced with properly encoded equivalents.

Preventive strategies include designing generators so that untrusted metadata never directly controls code structure, implementing strict input validation and escaping for all generated sinks, securing the provenance and integrity of specification files, and embedding static analysis and code review steps into the generation and integration workflow for client libraries.

Find out what's already
exploitable in your codebase.