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.SetEnumOptionsintoDocumentation.DescriptionTemplateand processed byPythonConventionService.RemoveInvalidDescriptionCharacterswithout 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.

