Trim trailing whitespace in cookie value: JavaNetCookieJar to avoid crash#9374
Open
vansh1sh wants to merge 1 commit intosquare:masterfrom
Open
Trim trailing whitespace in cookie value: JavaNetCookieJar to avoid crash#9374vansh1sh wants to merge 1 commit intosquare:masterfrom
vansh1sh wants to merge 1 commit intosquare:masterfrom
Conversation
e3f2511 to
b0e8187
Compare
b0e8187 to
da15e0e
Compare
yschimke
reviewed
Mar 14, 2026
Collaborator
yschimke
left a comment
There was a problem hiding this comment.
Looks safe enough, but would like to confirm both paths, via a Response with Set-Cookie and the path here via injecting directly into CookieHandler.
Is there a particular CookieHandler that has this issue?
We should fix anyway, but I'm curious if this is just a theoretical bug.
| } | ||
|
|
||
| @Test | ||
| fun cookieHandlerWithQuotedValueAndTrailingSpace() { |
Collaborator
There was a problem hiding this comment.
This is testing injecting a Cookie via CookieHandler. I can't reproduce via an actual request/response stored to the cookieJar, so I'm assuming this is only a problem with a shared or custom CookieHandler?
Can we add a test to confirm, something like
@Test
fun receiveAndSendUntrimmedCookie() {
server.enqueue(
MockResponse
.Builder()
.addHeader("Set-Cookie", "a=\"android \"")
.build(),
)
server.enqueue(MockResponse())
val cookieManager = CookieManager(null, CookiePolicy.ACCEPT_ORIGINAL_SERVER)
client =
client
.newBuilder()
.cookieJar(JavaNetCookieJar(cookieManager))
.build()
get(urlWithIpAddress(server, "/"))
val request1 = server.takeRequest()
assertThat(request1.headers["Cookie"]).isNull()
get(urlWithIpAddress(server, "/"))
val request2 = server.takeRequest()
assertThat(request2.headers["Cookie"]).isEqualTo("a=android")
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When JavaNetCookieJar receives a Cookie header from a CookieHandler with a quoted value that has trailing whitespace, for example:
Cookie: token="abc123 "
the value is unquoted to abc123 (with a trailing space), and Cookie.Builder.value currently throws IllegalArgumentException("value is not trimmed").
This PR makes decodeHeaderAsJavaNetCookies minimally tolerant of that case by trimming the value after unquoting:
It also adds a regression test to okhttp/src/jvmTest/kotlin/okhttp3/CookiesTest.kt
Fixes #9373.