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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.

}
}
cookies?.forEach { storage.addCookie(url, it) }
}
Expand Down
18 changes: 18 additions & 0 deletions ktor-client/ktor-client-core/common/test/CookiesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,22 @@ class CookiesTest {

assertNull(builder.headers[HttpHeaders.Cookie])
}

@Test
fun testCapturedHeaderCookiesStoredAsRawPreserveOriginalHeader() = testSuspend {
val feature = HttpCookies(AcceptAllCookiesStorage(), emptyList())
val builder = HttpRequestBuilder()
val defaultEncodingCookie = Cookie("default", "&%?#=$")
val rawEncodingCookie = Cookie("raw", "&%?#=$", encoding = CookieEncoding.RAW)
val base64EncodingCookie = Cookie("base64", "&%?#=$", encoding = CookieEncoding.BASE64_ENCODING)
val dquotesEncodingCookie = Cookie("dquotes", "&%?#=$", encoding = CookieEncoding.DQUOTES)
val cookies = listOf(defaultEncodingCookie, rawEncodingCookie, base64EncodingCookie, dquotesEncodingCookie)
.joinToString("; ", transform = ::renderCookieHeader)

builder.header(HttpHeaders.Cookie, cookies)
feature.captureHeaderCookies(builder)
feature.sendCookiesWith(builder)

assertEquals(cookies, builder.headers[HttpHeaders.Cookie])
}
}