Skip to content

[KOTLIN-SPRING;KOTLIN-KTOR] - Fix issues similar to CVE-2026-22785#23960

Draft
Picazsoo wants to merge 4 commits into
OpenAPITools:masterfrom
Picazsoo:feature/fix-kotlin-spring-injection
Draft

[KOTLIN-SPRING;KOTLIN-KTOR] - Fix issues similar to CVE-2026-22785#23960
Picazsoo wants to merge 4 commits into
OpenAPITools:masterfrom
Picazsoo:feature/fix-kotlin-spring-injection

Conversation

@Picazsoo
Copy link
Copy Markdown
Contributor

@Picazsoo Picazsoo commented Jun 5, 2026

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Prevents code injection in generated Kotlin by replacing triple-quoted descriptions in kotlin-spring and neutralizing interpolation in kotlin-server (ktor/ktor2) response examples, addressing the same class of issues as CVE-2026-22785. Adds shared Mustache lambdas to safely embed untrusted OpenAPI values in Kotlin strings.

  • Bug Fixes
    • Added escapeInNormalString (escapes , $, ", newlines) and escapeInTripleQuotedString (escapes $) lambdas in AbstractKotlinCodegen.
    • kotlin-spring: switched @Operation(description = "...") in api.mustache and apiInterface.mustache to {{#lambda.escapeInNormalString}}...{{/lambda.escapeInNormalString}}.
    • kotlin-server ktor/ktor2: _response.mustache now wraps example with {{#lambda.escapeInTripleQuotedString}}...{{/lambda.escapeInTripleQuotedString}} inside the triple-quoted string.
    • Added regression tests for description/example injection (including ktor/ktor2) and worst-case escaping; updated multi-line description assertions; regenerated samples.

Written for commit 43cea0a. Summary will update on new commits.

Review in cubic

Picazsoo and others added 3 commits June 5, 2026 21:46
…erators (CVE-2026-22785)

Any triple-quoted Kotlin string rendered from an untrusted OpenAPI value is a
code-injection sink: a description or example containing """ closes the
string, allowing attacker-controlled Kotlin declarations in the generated code.

Add a new �scapeInNormalString mustache lambda to AbstractKotlinCodegen that
escapes backslashes, dollar signs, double-quotes, and newlines, making values
safe to embed in regular double-quoted Kotlin strings. Since AbstractKotlinCodegen
is the parent of all Kotlin generators, the lambda is available everywhere.

Fix all identified sinks:

kotlin-spring (CVE-2026-22785):
  api.mustache, apiInterface.mustache:
    description = """{{{unescapedNotes}}}"""
    -> description = "{{#lambda.escapeInNormalString}}{{{unescapedNotes}}}{{/lambda.escapeInNormalString}}"

kotlin-server / ktor, ktor2:
  libraries/ktor/_response.mustache, libraries/ktor2/_response.mustache:
    val exampleContentString = """{{&example}}"""
    -> val exampleContentString = "{{#lambda.escapeInNormalString}}{{&example}}{{/lambda.escapeInNormalString}}"

Tests:
  - Add regression test tripleQuoteInjectionInDescriptionIsBlocked (CVE-2026-22785)
  - Add commentEndingInDescriptionIsSanitized (KDoc */ injection)
  - Update multiLineOperationDescription assertion for new escaped format
  - Add test fixtures cve-description-injection.yaml and
    issue20502-kotlin-string-escaping.yaml

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

Add tests that verify exampleContentString in _response.mustache is rendered
as a normal double-quoted string (not triple-quoted), blocking triple-quote
injection via response example values.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Picazsoo Picazsoo changed the title [KOTLIN-SPRING;KOTLIN-KTOR] - Fix issue similar to CVE-2026-22785 [KOTLIN-SPRING;KOTLIN-KTOR] - Fix issues similar to CVE-2026-22785 Jun 5, 2026
@Picazsoo Picazsoo marked this pull request as ready for review June 5, 2026 22:06
@Picazsoo Picazsoo marked this pull request as draft June 5, 2026 22:06
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

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

No issues found across 52 files

Re-trigger cubic

…or triple-quoted strings

A Kotlin triple-quoted string still supports dollar-sign string interpolation
(\). An untrusted OpenAPI example value containing \ would
therefore be evaluated at runtime inside �xampleContentString.

Add �scapeInTripleQuotedString lambda to AbstractKotlinCodegen that replaces
every \$ with \, preventing interpolation without switching away
from triple-quoted strings. Apply it to both ktor and ktor2 _response.mustache.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant