-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-917 KTOR-993 Prevent URI Encoding for Safe Chars (#3179)
* KTOR-993 Add test to verify URI encoding * KTOR-917 Keep some characters in cookie URI encoding according to RFC 3986 * Fix codestyle warnings
- Loading branch information
Showing
12 changed files
with
391 additions
and
208 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
110 changes: 110 additions & 0 deletions
110
ktor-http/common/test/io/ktor/tests/http/UrlEncodeTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.http | ||
|
||
import io.ktor.http.* | ||
import kotlin.test.* | ||
|
||
class UrlEncodeTest { | ||
|
||
@Test | ||
fun testUrlEncodePathKeepDigits() { | ||
assertEquals("0123456789", "0123456789".encodeURLPath()) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodeQueryComponentKeepDigits() { | ||
assertEquals("0123456789", "0123456789".encodeURLQueryComponent()) | ||
} | ||
|
||
@Test | ||
fun testUrlKeepDigitsInPath() { | ||
assertEquals("/0123456789/", Url("http://x.com/0123456789/").encodedPath) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodePathKeepLetters() { | ||
assertEquals( | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".encodeURLPath() | ||
) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodeQueryComponentKeepLetters() { | ||
assertEquals( | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", | ||
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".encodeURLQueryComponent() | ||
) | ||
} | ||
|
||
@Test | ||
fun testUrlKeepLettersInPath() { | ||
assertEquals( | ||
"/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/", | ||
Url("http://x.com/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/").encodedPath | ||
) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodePathKeepHyphen() { | ||
assertEquals("-", "-".encodeURLPath()) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodeQueryComponentKeepHyphen() { | ||
assertEquals("-", "-".encodeURLQueryComponent()) | ||
} | ||
|
||
@Test | ||
fun testUrlKeepHyphenInPath() { | ||
assertEquals("/-/", Url("http://x.com/-/").encodedPath) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodePathKeepPeriod() { | ||
assertEquals(".", ".".encodeURLPath()) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodeQueryComponentKeepPeriod() { | ||
assertEquals(".", ".".encodeURLQueryComponent()) | ||
} | ||
|
||
@Test | ||
fun testUrlKeepPeriodInPath() { | ||
assertEquals("/./", Url("http://x.com/./").encodedPath) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodePathKeepUnderscore() { | ||
assertEquals("_", "_".encodeURLPath()) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodeQueryComponentKeepUnderscore() { | ||
assertEquals("_", "_".encodeURLQueryComponent()) | ||
} | ||
|
||
@Test | ||
fun testUrlKeepUnderscoreInPath() { | ||
assertEquals("/_/", Url("http://x.com/_/").encodedPath) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodePathKeepTilde() { | ||
assertEquals("~", "~".encodeURLPath()) | ||
} | ||
|
||
@Test | ||
fun testUrlEncodeQueryComponentKeepTilde() { | ||
assertEquals("~", "~".encodeURLQueryComponent()) | ||
} | ||
|
||
@Test | ||
fun testUrlKeepTildeInPath() { | ||
assertEquals("/~/", Url("http://x.com/~/").encodedPath) | ||
} | ||
} |
This file contains 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
32 changes: 32 additions & 0 deletions
32
...tor-server-tests/jvmAndNix/test/io/ktor/tests/server/plugins/Base64EncodingCookiesTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.server.plugins | ||
|
||
import io.ktor.http.* | ||
import kotlin.test.* | ||
|
||
class Base64EncodingCookiesTest { | ||
@Test | ||
fun `no bad characters`() { | ||
testEncode("YWJj", "abc") | ||
} | ||
|
||
@Test | ||
fun `space inside`() { | ||
testEncode("YWJjIDEyMw==", "abc 123") | ||
} | ||
|
||
@Test | ||
fun `equals inside`() { | ||
testEncode("YWJjPTEyMw==", "abc=123") | ||
} | ||
|
||
private fun testEncode(expected: String, value: String): String { | ||
val encoded = encodeCookieValue(value, CookieEncoding.BASE64_ENCODING) | ||
assertEquals(expected, encoded, "Encode failed") | ||
assertEquals(value, decodeCookieValue(encoded, CookieEncoding.BASE64_ENCODING), "Decode failed") | ||
return encoded | ||
} | ||
} |
Oops, something went wrong.