Skip to content
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 @@ -112,6 +112,9 @@ class JavaNetCookieJar(
value = value.substring(1, value.length - 1)
}

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

result.add(
Cookie
.Builder()
Expand Down
56 changes: 56 additions & 0 deletions okhttp/src/jvmTest/kotlin/okhttp3/CookiesTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,62 @@ class CookiesTest {
assertThat(request.headers["Quux"]).isNull()
}

@Test
fun cookieHandlerWithQuotedValueAndTrailingSpace() {
Copy link
Copy Markdown
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")
  }

Copy link
Copy Markdown
Contributor Author

@vansh1sh vansh1sh Mar 17, 2026

Choose a reason for hiding this comment

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

added a commit for this test. It succeeds in both the flows for me, so probably happening because of shared Cookie Handler.

server.enqueue(MockResponse())
val serverUrl = urlWithIpAddress(server, "/")
val androidCookieHandler: CookieHandler =
object : CookieHandler() {
override fun get(
uri: URI,
map: Map<String, List<String>>,
) = mapOf(
"Cookie" to
listOf(
"a=\"android \"",
),
)

override fun put(
uri: URI,
map: Map<String, List<String>>,
) {
}
}
client =
client
.newBuilder()
.cookieJar(JavaNetCookieJar(androidCookieHandler))
.build()
get(serverUrl)
val request = server.takeRequest()
assertThat(request.headers["Cookie"]).isEqualTo("a=android")
assertThat(request.headers["Quux"]).isNull()
}

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

private fun urlWithIpAddress(
server: MockWebServer,
path: String,
Expand Down
Loading