Skip to content

fix(java,php): neutralize doc-comment delimiters instead of deleting them - #8017

Open
gavinbarron wants to merge 2 commits into
mainfrom
gavinbarron-comment-escaping-fixes
Open

fix(java,php): neutralize doc-comment delimiters instead of deleting them#8017
gavinbarron wants to merge 2 commits into
mainfrom
gavinbarron-comment-escaping-fixes

Conversation

@gavinbarron

Copy link
Copy Markdown
Contributor

Summary

Fixes doc-comment breakout / code-injection in the Java and PHP generators. Both sanitizers deleted the */ terminator, which is unsafe: deleting a two-character sequence lets the surrounding characters rejoin into a new delimiter (**//*/). A hostile OpenAPI description/summary/externalDocs value could therefore close the generated doc comment and inject executable code into the generated client.

This mirrors the already-fixed C# CVE-2026-59860 class and reuses the non-re-forming, replace-based approach already present in TypeScriptConventionService (/*//*, */* /).

Reports addressed

Three internal reports map onto two underlying code sinks:

  • PHP + Java delete-based re-formation (**//*/) — reported twice; same root cause, same fix.
  • Java ordering bug — the */ strip ran before the non-ASCII strip, so *é/ re-formed */ after the non-ASCII char was removed.

A single Java change fixes both Java reports (reorders the passes and switches delete→replace).

Changes

  • New shared StringExtensions.NeutralizeBlockCommentDelimiters() — replaces (never deletes) /* and */.
  • PHP RemoveInvalidDescriptionCharacters: \\/, then neutralize.
  • Java RemoveInvalidDescriptionCharacters: \\/ → non-ASCII strip → neutralize → CleanupXMLString() (neutralization now runs after the non-ASCII strip).
  • Regression tests for **//, **\/, *é/, */, /* asserting exact output and no residual */.
  • Updated 2 pre-existing tests that asserted the old delete output.

Validation

  • 359/359 Java + PHP + TypeScript + common convention-service writer tests pass.

Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com

…them

The Java and PHP doc-comment sanitizers deleted the `*/` terminator via Replace(*/, empty). Deleting a two-character sequence lets the surrounding characters re-form it (e.g. **// collapses to */), allowing a hostile schema description to break out of a generated doc comment and inject code into the generated client.

The Java sanitizer additionally ran the delimiter strip before the non-ASCII strip, so *e/ (with a non-ASCII char between) re-formed */ after the non-ASCII char was removed.

Replace delimiters instead of deleting (/* -> //*, */ -> * /) via a new shared NeutralizeBlockCommentDelimiters helper, matching TypeScriptConventionService, and run the Java neutralization after the non-ASCII strip. Adds regression tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c3d2eefa-5201-4d73-b430-3c0ce09651cd
@gavinbarron
gavinbarron requested a review from a team as a code owner August 3, 2026 18:36
Copilot AI review requested due to automatic review settings August 3, 2026 18:36
@msgraph-bot msgraph-bot Bot added this to Kiota Aug 3, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Hardens Kiota’s Java and PHP doc-comment sanitization to prevent doc-comment breakout/code-injection from schema-derived documentation text by neutralizing (replace-based) block comment delimiters instead of deleting them, eliminating delimiter re-formation cases like **//*/.

Changes:

  • Added a shared StringExtensions.NeutralizeBlockCommentDelimiters() helper that replaces /*//* and */* / (never deletes).
  • Updated Java and PHP RemoveInvalidDescriptionCharacters to use delimiter neutralization (and in Java, to run neutralization after non-ASCII stripping to avoid re-forming */).
  • Added/updated regression tests covering delimiter re-formation and ordering edge cases (**//, **\/, *é/, /*, */) and adjusted expectations accordingly.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/Kiota.Builder.Tests/Writers/Php/PhpWriterTests.cs Adds regression coverage for PHP description sanitization to ensure */ cannot remain or re-form.
tests/Kiota.Builder.Tests/Writers/Php/CodeMethodWriterTests.cs Updates expected emitted PHP doc output to match replace-based neutralization (* / instead of deletion).
tests/Kiota.Builder.Tests/Writers/Java/JavaWriterTests.cs Adds regression coverage for Java sanitization including the non-ASCII-strip ordering case.
tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs Updates expected emitted Java doc output to match replace-based neutralization.
src/Kiota.Builder/Writers/Php/PhpConventionService.cs Switches PHP description sanitization from delete-based to neutralize-based logic.
src/Kiota.Builder/Writers/Java/JavaConventionService.cs Reorders/switches Java description sanitization to neutralize after non-ASCII stripping, preventing re-formation.
src/Kiota.Builder/Extensions/StringExtensions.cs Introduces the shared NeutralizeBlockCommentDelimiters() helper used by generators.

@github-code-quality

github-code-quality Bot commented Aug 3, 2026

Copy link
Copy Markdown

Code Coverage Overview

Languages: C#

C# / code-coverage/dotnet

The overall coverage in commit a71a879 in the gavinbarron-comment-... branch is 72%. Coverage data for the main branch is not yet available.

Show a code coverage summary of the most covered files.
File main gavinbarron-comment-... a71a879 +/-
/home/runner/wo...guageRefiner.cs 98%
/home/runner/wo...criptRefiner.cs 98%
/home/runner/wo...MethodWriter.cs 97%
/home/runner/wo...MethodWriter.cs 96%
/home/runner/wo...MethodWriter.cs 96%
/home/runner/wo...MethodWriter.cs 95%
/home/runner/wo...rs/GoRefiner.cs 94%
/home/runner/wo...KiotaBuilder.cs 90%
/home/runner/wo...ationService.cs 89%
/home/runner/wo...xGenerator.g.cs 76%

Updated August 04, 2026 01:49 UTC

Copilot AI review requested due to automatic review settings August 4, 2026 01:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated no new comments.

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants