Skip to content

KTOR-9423 CannotTransformContentToTypeException leaks internal class#5492

Open
zibet27 wants to merge 1 commit intorelease/3.xfrom
zibet27/fix-internal-class-message-leak
Open

KTOR-9423 CannotTransformContentToTypeException leaks internal class#5492
zibet27 wants to merge 1 commit intorelease/3.xfrom
zibet27/fix-internal-class-message-leak

Conversation

@zibet27
Copy link
Copy Markdown
Collaborator

@zibet27 zibet27 commented Mar 26, 2026

Subsystem
Server

Motivation
KTOR-9423 CannotTransformContentToTypeException leaks internal class names in the response body

Solution

  • Move CannotTransformContentToTypeException catching, allowing StatusPages to process it
  • Don't send the error message in prod mode

@zibet27 zibet27 requested review from bjhham and osipxd March 26, 2026 17:06
@zibet27 zibet27 self-assigned this Mar 26, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 26, 2026

📝 Walkthrough

Walkthrough

The PR centralizes content transformation exception handling by removing default receive-side error handling from BaseApplicationEngine and consolidating exception mapping in DefaultEnginePipeline. Both UnsupportedMediaTypeException and CannotTransformContentToTypeException are now mapped to UnsupportedMediaType status, with logging severity adjusted accordingly. A test case validates the new exception handling path.

Changes

Cohort / File(s) Summary
Core engine error handling
ktor-server/ktor-server-core/common/src/io/ktor/server/engine/BaseApplicationEngine.kt
Removed receive-side transformation error interception that caught CannotTransformContentToTypeException and returned 415; error handling now relies solely on post-render send pipeline phase.
Default pipeline exception mapping
ktor-server/ktor-server-core/common/src/io/ktor/server/engine/DefaultEnginePipeline.kt
Expanded defaultExceptionStatusCode to map CannotTransformContentToTypeException to UnsupportedMediaType; refined tryRespondError to consider developmentMode when deciding whether to include error message; adjusted logFailure to treat both transformation exceptions as debug-level.
Test coverage
ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt
Added test case testCannotTransformContentToTypeException verifying custom StatusPages handler for the exception type with correct status and response body.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing a leak of internal class names in the CannotTransformContentToTypeException response.
Description check ✅ Passed The description follows the required template with all sections completed: Subsystem (Server), Motivation (references KTOR-9423 issue), and Solution (specific changes outlined).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch zibet27/fix-internal-class-message-leak

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt (1)

631-647: Please cover the default no-leak path too.

Because this handler replaces the body, the test doesn't verify the actual leak fix in the default engine response. A companion case that triggers CannotTransformContentToTypeException through the receive path without StatusPages would lock in the production 415/no-message behavior as well.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt`
around lines 631 - 647, Add a companion test that exercises the default
(no-StatusPages) path for CannotTransformContentToTypeException to ensure the
engine returns the production 415/no-message behavior; specifically, create a
setup that does not install StatusPages, has a routing POST that triggers throw
CannotTransformContentToTypeException(typeOf<String>()), issues client.post("/")
and asserts response.status is HttpStatusCode.UnsupportedMediaType and
response.bodyAsText() is empty (or no message) so the leak fix is verified in
the default receive path.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt`:
- Line 630: Rename the test function testCannotTransformContentToTypeException
to a descriptive backtick-style Kotlin test name (e.g., `fun \`cannot transform
content to target type returns appropriate status\`()`) to follow the
repository's **/test/**/*.kt naming convention; update the function declaration
(testCannotTransformContentToTypeException) and any references to it so the test
framework runs the new backtick name without changing behavior.

---

Nitpick comments:
In
`@ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt`:
- Around line 631-647: Add a companion test that exercises the default
(no-StatusPages) path for CannotTransformContentToTypeException to ensure the
engine returns the production 415/no-message behavior; specifically, create a
setup that does not install StatusPages, has a routing POST that triggers throw
CannotTransformContentToTypeException(typeOf<String>()), issues client.post("/")
and asserts response.status is HttpStatusCode.UnsupportedMediaType and
response.bodyAsText() is empty (or no message) so the leak fix is verified in
the default receive path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ae4f0fc2-8550-4a20-bafa-7e36299097e3

📥 Commits

Reviewing files that changed from the base of the PR and between 12de7fb and 0a59138.

📒 Files selected for processing (3)
  • ktor-server/ktor-server-core/common/src/io/ktor/server/engine/BaseApplicationEngine.kt
  • ktor-server/ktor-server-core/common/src/io/ktor/server/engine/DefaultEnginePipeline.kt
  • ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt

}

@Test
fun testCannotTransformContentToTypeException() = testApplication {
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.

⚠️ Potential issue | 🟡 Minor

Use a descriptive backtick test name.

This new case should follow the repository's **/test/**/*.kt naming convention.

✏️ Rename suggestion
-    fun testCannotTransformContentToTypeException() = testApplication {
+    fun `handles CannotTransformContentToTypeException via StatusPages`() = testApplication {
As per coding guidelines `**/test/**/*.kt`: Use descriptive test names in backticks following the pattern: `describe what is being tested`.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fun testCannotTransformContentToTypeException() = testApplication {
fun `handles CannotTransformContentToTypeException via StatusPages`() = testApplication {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt`
at line 630, Rename the test function testCannotTransformContentToTypeException
to a descriptive backtick-style Kotlin test name (e.g., `fun \`cannot transform
content to target type returns appropriate status\`()`) to follow the
repository's **/test/**/*.kt naming convention; update the function declaration
(testCannotTransformContentToTypeException) and any references to it so the test
framework runs the new backtick name without changing behavior.

Copy link
Copy Markdown
Member

@osipxd osipxd left a comment

Choose a reason for hiding this comment

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

Please check failing tests

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.

3 participants