Skip to content

Commit 69792c3

Browse files
authored
KTOR-4722 renderSetCookieHeader should not encode extensions (#3172)
1 parent d6b9a87 commit 69792c3

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

ktor-http/common/src/io/ktor/http/Cookie.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ public fun renderSetCookieHeader(
157157

158158
cookiePartFlag("Secure", secure),
159159
cookiePartFlag("HttpOnly", httpOnly)
160-
) + extensions.map { cookiePartExt(it.key.assertCookieName(), it.value, encoding) } +
161-
if (includeEncoding) cookiePartExt("\$x-enc", encoding.name, CookieEncoding.RAW) else ""
160+
) + extensions.map { cookiePartExt(it.key.assertCookieName(), it.value) } +
161+
if (includeEncoding) cookiePartExt("\$x-enc", encoding.name) else ""
162162
).filter { it.isNotEmpty() }
163163
.joinToString("; ")
164164

@@ -221,7 +221,7 @@ private inline fun cookiePartFlag(name: String, value: Boolean) =
221221
if (value) name else ""
222222

223223
@Suppress("NOTHING_TO_INLINE")
224-
private inline fun cookiePartExt(name: String, value: String?, encoding: CookieEncoding) =
225-
if (value == null) cookiePartFlag(name, true) else cookiePart(name, value, encoding)
224+
private inline fun cookiePartExt(name: String, value: String?) =
225+
if (value == null) cookiePartFlag(name, true) else cookiePart(name, value, CookieEncoding.RAW)
226226

227227
private fun String.toIntClamping(): Int = toLong().coerceIn(0L, Int.MAX_VALUE.toLong()).toInt()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package io.ktor.tests.http
6+
7+
import io.ktor.http.*
8+
import kotlin.test.*
9+
10+
class RenderSetCookieTest {
11+
12+
@Test
13+
fun renderCookieDoesntEncodeExtensions() {
14+
val cookie = Cookie(
15+
"name",
16+
"value",
17+
encoding = CookieEncoding.BASE64_ENCODING,
18+
extensions = mapOf("foo" to "bar")
19+
)
20+
val rendered = renderSetCookieHeader(cookie)
21+
assertEquals("name=dmFsdWU=; foo=bar; \$x-enc=BASE64_ENCODING", rendered)
22+
}
23+
24+
@Test
25+
fun renderCookieThrowsOnNotEncodedExtensions() {
26+
val cookie = Cookie(
27+
"name",
28+
"value",
29+
encoding = CookieEncoding.BASE64_ENCODING,
30+
extensions = mapOf("foo" to "b,ar")
31+
)
32+
assertFailsWith<IllegalArgumentException> {
33+
renderSetCookieHeader(cookie)
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)