Skip to content

fix(writers/python): prevent code injection via x-ms-enum description#7735

Open
MIchaelMainer wants to merge 4 commits into
mainfrom
fix/python-enum-description-injection
Open

fix(writers/python): prevent code injection via x-ms-enum description#7735
MIchaelMainer wants to merge 4 commits into
mainfrom
fix/python-enum-description-injection

Conversation

@MIchaelMainer
Copy link
Copy Markdown
Member

Fixes a code injection vulnerability in the Python writer where malicious x-ms-enum description fields containing newline characters could break out of
inline comments (# ...) or docstrings and inject arbitrary Python code into generated output.

Changes

  • PythonConventionService.RemoveInvalidDescriptionCharacters: Added newline stripping (\r\n, \r, \n → space) before the existing backslash and
    triple-quote replacements.
  • CodeEnumWriterTests: Added two regression tests verifying hostile payloads with newlines and triple-quotes are properly neutralized.

Root Cause

The RemoveInvalidDescriptionCharacters method only handled backslash and triple-quote escaping but did not strip newline characters. Since Python
inline comments are single-line (# ...), an embedded newline would terminate the comment and allow subsequent text to execute as Python code.

Security Impact

A malicious OpenAPI specification could inject arbitrary Python code into generated SDK clients via the x-ms-enum extension's description field.

… newlines

The RemoveInvalidDescriptionCharacters method in PythonConventionService
did not strip newline characters (\r\n, \r, \n). A malicious OpenAPI spec
could inject arbitrary Python code via x-ms-enum description fields by
embedding newlines that break out of inline comments (# ...) or
docstrings.

This fix replaces newlines with spaces, keeping all description content
on a single line where it remains safely within comments/docstrings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@MIchaelMainer MIchaelMainer requested a review from a team as a code owner May 26, 2026 19:57
@msgraph-bot msgraph-bot Bot added this to Kiota May 26, 2026
… newlines

The RemoveInvalidDescriptionCharacters method in PythonConventionService
did not strip newline characters (\r\n, \r, \n). A malicious OpenAPI spec
could inject arbitrary Python code via x-ms-enum description fields by
embedding newlines that break out of inline comments (# ...) or
docstrings.

This fix replaces newlines with spaces, keeping all description content
on a single line where it remains safely within comments/docstrings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
baywet
baywet previously approved these changes May 26, 2026
@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota May 26, 2026
@baywet
Copy link
Copy Markdown
Member

baywet commented May 26, 2026

@MIchaelMainer some unit tests are failing. Could be transient.
Also do we have exposure in other languages?
Also, it'd be good to update the agent /copilot instructions to be on the look out for these kind of instances, I'm not sure why it didn't flag it in the initial fixes.

- "**"
---

# Pre-Commit Testing Requirements
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, what I meant was to amend this one.

https://github.com/microsoft/kiota/blob/main/.github/instructions/writer-literal-security.instructions.md

that one

https://github.com/microsoft/kiota/blob/main/.github/skills/codegen-literal-security-scan/SKILL.md

this one

https://github.com/microsoft/kiota/blob/main/.github/agents/codegen-security-guardian.agent.md

And I think the reason why they didn't get looped in is because we don't have .github/copilot-instructions.md on this repository. (contrary to other repositories I work on)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress 🚧

Development

Successfully merging this pull request may close these issues.

2 participants