Skip to content

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

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

[KOTLIN-SPRING;KOTLIN-KTOR] - Fix some issues similar to CVE-2026-22785#23960
Picazsoo wants to merge 15 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

Hardened Kotlin generators to block OpenAPI-driven code injection in descriptions and examples for kotlin-spring and kotlin-server (ktor/ktor2), addressing the same class as CVE-2026-22785. kotlin-spring now renders descriptions and interface property examples as escaped normal strings; ktor examples in triple-quoted strings are escaped to prevent interpolation and triple-quote breakouts.

  • Bug Fixes
    • AbstractKotlinCodegen: added Mustache lambdas escapeInNormalString (escapes , $, ", newlines) and escapeInTripleQuotedString (escapes $ and triple quotes).
    • kotlin-spring: switched @Operation.description to escaped normal strings; escaped descriptions in @Schema, @ApiModelProperty, and @Parameter across API/model and parameter templates (including declarative HTTP); escaped interface property example values in schema annotations.
    • kotlin-server (ktor/ktor2): applied {{#lambda.escapeInTripleQuotedString}}...{{/lambda.escapeInTripleQuotedString}} around response example values to block interpolation and triple-quote injection.
    • Tests/samples: added CVE-style regression specs (ktor injection and worst-case escaping), updated multi-line description assertions for escaped format, and regenerated samples.

Written for commit 033a41e. 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

Picazsoo and others added 3 commits June 6, 2026 02:16
…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>
@Picazsoo Picazsoo changed the title [KOTLIN-SPRING;KOTLIN-KTOR] - Fix issues similar to CVE-2026-22785 [KOTLIN-SPRING;KOTLIN-KTOR] - Fix some issues similar to CVE-2026-22785 Jun 7, 2026
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.

2 issues found across 51 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_response.mustache">

<violation number="1" location="modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_response.mustache:2">
P1: Incomplete escaping in triple-quoted string: `escapeInTripleQuotedString` does not neutralize `"""`, allowing raw-string breakout from attacker-controlled example content.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@@ -1,5 +1,5 @@
val exampleContentType = "{{{contentType}}}"
val exampleContentString = """{{&example}}"""
val exampleContentString = """{{#lambda.escapeInTripleQuotedString}}{{&example}}{{/lambda.escapeInTripleQuotedString}}"""
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.

P1: Incomplete escaping in triple-quoted string: escapeInTripleQuotedString does not neutralize """, allowing raw-string breakout from attacker-controlled example content.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/resources/kotlin-server/libraries/ktor2/_response.mustache, line 2:

<comment>Incomplete escaping in triple-quoted string: `escapeInTripleQuotedString` does not neutralize `"""`, allowing raw-string breakout from attacker-controlled example content.</comment>

<file context>
@@ -1,5 +1,5 @@
 val exampleContentType = "{{{contentType}}}"
-val exampleContentString = """{{&example}}"""
+val exampleContentString = """{{#lambda.escapeInTripleQuotedString}}{{&example}}{{/lambda.escapeInTripleQuotedString}}"""
 
 when (exampleContentType) {
</file context>

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