Skip to content

KTOR-8343 Fix: Store client header cookies with RAW encoding to preve… #4873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025

Conversation

rururux
Copy link
Contributor

@rururux rururux commented May 21, 2025

…nt value alteration

Subsystem
Client, HttpCookies

Motivation
KTOR-8343 HttpCookies: Encoding of request cookies is not preserved in CookiesStorage
Currently, when HttpCookies captures cookies from the Cookie request header, it doesn't explicitly specify an encoding for storage. This can lead to unintended alteration of cookie values if the CookiesStorage later retrieves them assuming a different encoding (e.g., URI_ENCODING by default).
This is problematic because the original cookie value, as sent by the client, might not be preserved, potentially leading to unexpected behavior in applications that rely on exact cookie values.

Solution
This PR modifies HttpCookies to explicitly store cookies captured from the Cookie request header with CookieEncoding.RAW.
By doing so, we ensure that the cookie value is preserved in its original form as received in the header, regardless of any default encoding assumptions by the CookiesStorage.

A new test case, testCapturedHeaderCookiesStoredAsRawPreserveOriginalHeader, has been added to CookiesTest.kt to verify that cookies captured from the header are indeed stored and retrieved with their original values intact, even when they contain characters that might be altered by other encodings.

Copy link

coderabbitai bot commented May 21, 2025

Walkthrough

The changes update how cookies parsed from the "Cookie" request header are instantiated in the HTTP client core, ensuring they are created with explicit raw encoding. Additionally, a new test is added to verify that cookies captured from headers retain their original raw format and that the header remains unchanged after processing.

Changes

File(s) Change Summary
ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cookies/HttpCookies.kt Modified cookie instantiation from the "Cookie" header to use explicit CookieEncoding.RAW when creating Cookie objects.
ktor-client/ktor-client-core/common/test/CookiesTest.kt Added a new test function to ensure cookies captured from headers are stored with raw encoding and the original header is preserved after processing.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a7d3c2e and 05e86b3.

📒 Files selected for processing (2)
  • ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cookies/HttpCookies.kt (1 hunks)
  • ktor-client/ktor-client-core/common/test/CookiesTest.kt (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • ktor-client/ktor-client-core/common/test/CookiesTest.kt
  • ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cookies/HttpCookies.kt
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@e5l e5l requested review from Copilot and osipxd May 26, 2025 11:06
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures that cookies captured from the request header are stored using RAW encoding to preserve their original values, addressing the risk of unintended value alteration.

  • Updates HttpCookies.kt to map header cookies with CookieEncoding.RAW.
  • Adds a new test in CookiesTest.kt to verify that the original cookie header values are preserved.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ktor-client/ktor-client-core/common/test/CookiesTest.kt Adds a test case to ensure that header cookies remain unchanged when stored with RAW encoding.
ktor-client/ktor-client-core/common/src/io/ktor/client/plugins/cookies/HttpCookies.kt Updates the cookie parsing logic to force RAW encoding for header cookies.
Comments suppressed due to low confidence (1)

ktor-client/ktor-client-core/common/test/CookiesTest.kt:115

  • Consider expanding the test to also assert that each Cookie object added to the storage has its 'encoding' property set to CookieEncoding.RAW for added verification of the desired behavior.
assertEquals(cookies, builder.headers[HttpHeaders.Cookie])

@@ -53,7 +53,9 @@ public class HttpCookies internal constructor(
val url = builder.url.clone().build()
val cookies = headers[HttpHeaders.Cookie]?.let { cookieHeader ->
LOGGER.trace("Saving cookie $cookieHeader for ${builder.url}")
parseClientCookiesHeader(cookieHeader).map { (name, encodedValue) -> Cookie(name, encodedValue) }
parseClientCookiesHeader(cookieHeader).map { (name, encodedValue) ->
Cookie(name, encodedValue, encoding = CookieEncoding.RAW)
Copy link
Preview

Copilot AI May 26, 2025

Choose a reason for hiding this comment

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

Consider adding a short inline comment explaining why the cookies captured from the header are explicitly set to RAW encoding. This will help maintain the clarity of this change for future maintainers.

Copilot uses AI. Check for mistakes.

Copy link
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.

Thank you for the contribution! 🎉
The solution looks good to me. It will be merged soon

@osipxd osipxd enabled auto-merge (squash) May 27, 2025 10:31
@osipxd osipxd merged commit a17d917 into ktorio:main May 27, 2025
15 of 17 checks passed
@rururux
Copy link
Contributor Author

rururux commented May 27, 2025

Thank you for the review and merge! 🤩
I appreciate your time and effort.

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.

2 participants