Skip to content

Trim trailing whitespace in cookie value: JavaNetCookieJar to avoid crash#9374

Open
vansh1sh wants to merge 1 commit intosquare:masterfrom
vansh1sh:fix-cookiejar-trimmed-value-9373
Open

Trim trailing whitespace in cookie value: JavaNetCookieJar to avoid crash#9374
vansh1sh wants to merge 1 commit intosquare:masterfrom
vansh1sh:fix-cookiejar-trimmed-value-9373

Conversation

@vansh1sh
Copy link

@vansh1sh vansh1sh commented Mar 13, 2026

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:

// Minimal normalisation so Cookie.Builder doesn't crash on values like "abc123 ".
value = value.trim()

It also adds a regression test to okhttp/src/jvmTest/kotlin/okhttp3/CookiesTest.kt

Fixes #9373.

@vansh1sh vansh1sh force-pushed the fix-cookiejar-trimmed-value-9373 branch from e3f2511 to b0e8187 Compare March 13, 2026 19:08
@vansh1sh vansh1sh force-pushed the fix-cookiejar-trimmed-value-9373 branch from b0e8187 to da15e0e Compare March 13, 2026 19:14
@vansh1sh vansh1sh changed the title Trim trailing whitespace in cookie value: JavaNetCookieJar to avoid crash (#9373) Trim trailing whitespace in cookie value: JavaNetCookieJar to avoid crash Mar 13, 2026
Copy link
Collaborator

@yschimke yschimke left a comment

Choose a reason for hiding this comment

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

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() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

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")
  }

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.

JavaNetCookieJar: "value is not trimmed" crash for Cookie header token="abc123 "

2 participants