/

CVE-2026-59862

Severity:

HIGH RISK

(7.5)

Code Injection (CWE-94) in Kiota Python Generator

CVE-2026-59862

Deferred

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Arbitrary Code Execution in Generated Clients

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

Kiota's Python client generator previously allowed attacker-controlled enum value descriptions from OpenAPI extensions to be emitted directly into generated source comments without proper newline sanitization.

If an attacker can influence these descriptions, they may inject and execute arbitrary Python code at module import time in applications that consume the generated client.

Affected context

This vulnerability affects environments where Kiota is used to generate Python HTTP clients from OpenAPI specifications that include x-ms-enum.values[].description fields.

It becomes relevant when:

  • OpenAPI specifications are obtained from untrusted or partially trusted sources, or
  • The spec can be modified in transit (e.g., via compromised repositories, CI pipelines, or supply-chain tooling), and
  • Generated Python modules are imported and executed in production or developer environments without additional review.

Exploit preconditions

  • Attack surface: The OpenAPI description input to Kiota, specifically x-ms-enum.values[].description.
  • Required access: Ability to modify or supply the OpenAPI document used in code generation (e.g., via compromised API spec, malicious pull request, or tampered CI artifact).
  • Trigger point: Importing the generated Python module, which causes inline comments containing maliciously crafted newline characters to terminate comments and introduce executable statements.
  • Why exploitation is possible: Enum descriptions are propagated through KiotaBuilder.SetEnumOptions into Documentation.DescriptionTemplate and processed by PythonConventionService.RemoveInvalidDescriptionCharacters without removing or neutralizing newlines, allowing comments to be broken and arbitrary Python code injected at module scope.

Why this happens

The underlying issue is a lack of output encoding and newline sanitization when embedding user-controlled metadata into generated source code.

Kiota treated x-ms-enum.values[].description as harmless documentation, but the Python generator emitted it into inline comments without constraining line breaks or comment terminators.

This maps to CWE-94: Improper Control of Generation of Code ('Code Injection'), where attacker-controlled input influences the structure of generated code and is later executed during normal application behavior (module import).

Potential impact

  • Execution of arbitrary Python code when generated modules are imported.
  • Compromise of application runtime, including theft of secrets accessible to the process.
  • Installation of backdoors or additional malicious logic in generated clients.
  • Lateral movement within CI/CD pipelines that run code generation and import tests.
  • Supply-chain compromise of downstream projects consuming generated Python clients.

General mitigation guidance

Mitigation typically focuses on treating all specification-sourced metadata as untrusted and constraining how it is rendered into source code.

Common defensive patterns include:

  • Normalizing or removing control characters (especially newlines) from documentation fields before emitting them into comments or strings.
  • Applying strict output encoding or escaping when generating code from user-controlled inputs so that they cannot alter code structure.
  • Separating data from code by avoiding direct insertion of free-form text into syntactically sensitive locations such as inline comments or top-level statements.
  • Restricting which OpenAPI documents are trusted as input, and validating schemas and metadata fields as part of the code-generation pipeline.
  • Running generated code with least privilege and additional scrutiny, especially when specs originate from external parties.

How teams detect issues like this

Detection efforts can focus on both code-generation templates and generated artifacts:

  • Static analysis of generators: Search for code-generation templates that embed raw description or comment fields directly into source without sanitizing newlines, line terminators, or comment markers.
  • Review of generated code: Inspect generated Python modules for suspicious comment blocks containing unexpected line breaks or code-like content following comment markers.
  • Policy enforcement: Enforce CI policies that lint generated code for forbidden constructs (e.g., unexpected imports, exec/eval usage, or top-level side-effect code) and reject builds when violations are found.
  • Automated tools (linters, SAST, or supply-chain scanners) can be configured to treat generated sources as first-class code and flag patterns indicative of code injection via comments or documentation fields.

Sources

SourceLink
github.comgithub.com/microsoft/kiota/releases/tag/v1.32.0
github.comgithub.com/microsoft/kiota/security/advisories/GHSA-7f3j-j7jj-r3vr

Frequently asked

questions

It is indirectly exploitable. An attacker must first influence the OpenAPI specification consumed by Kiota; once a malicious spec is used, any environment that generates and imports the resulting Python client can execute the injected code.

The attacker needs the ability to modify or provide the OpenAPI document used for generation, such as committing to a shared repository, tampering with artifacts in a CI pipeline, or hosting a malicious spec that developers or automation trust.

The safest pattern is to enforce strict sanitization and encoding of all untrusted documentation fields before inserting them into generated code, especially removing or escaping newlines and characters that can terminate comments or alter code structure, and to treat all spec-derived text as untrusted input.

Future prevention relies on designing code generators under a strict trust model: never embed unsanitized user-controlled data into syntactically sensitive locations; introduce centralized sanitization utilities; add tests that feed crafted inputs through generation; and enforce automated checks on both generator code and generated artifacts as part of continuous integration.

Find out what's already
exploitable in your codebase.