fix(auth): Crash if the username contains characters that cannot be used in a Uri path segment.

This commit is contained in:
Mateusz Armatys
2025-02-13 17:27:44 +01:00
committed by MargeBot
parent 04a1332457
commit 790516b0ae
4 changed files with 39 additions and 3 deletions
@@ -29,6 +29,7 @@ import kotlinx.coroutines.flow.SharedFlow
import me.proton.core.auth.domain.entity.AuthInfo
import me.proton.core.auth.domain.usecase.UserCheckAction
import me.proton.core.domain.entity.UserId
import me.proton.core.util.kotlin.toEncodedUriPathSegment
public object LoginRoutes {
@@ -41,12 +42,12 @@ public object LoginRoutes {
public object Route {
public object Login {
public const val Deeplink: String = "auth/{${Arg.KEY_USERNAME}}/login"
public fun get(username: String): String = "auth/$username/login"
public fun get(username: String): String = "auth/${username.toEncodedUriPathSegment()}/login"
}
public object Srp {
public const val Deeplink: String = "auth/{${Arg.KEY_USERNAME}}/login/srp"
public fun get(username: String): String = "auth/$username/login/srp"
public fun get(username: String): String = "auth/${username.toEncodedUriPathSegment()}/login/srp"
}
}
+4 -1
View File
@@ -41,7 +41,10 @@ dependencies {
serialization("core")
)
implementation(`serialization-json`)
implementation(
okhttp,
`serialization-json`
)
testImplementation(
junit,
@@ -2,6 +2,7 @@
package me.proton.core.util.kotlin
import okhttp3.HttpUrl
import kotlin.math.absoluteValue
/** An empty [String] `""` */
@@ -263,4 +264,18 @@ fun String.Companion.random(
charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
): String =
(1..length).map { charPool.random() }.joinToString("")
/**
* Encodes a [String] to be a valid URI path segment.
* Note: this is different than [java.net.URLEncoder.encode]. For example:
* `URLEncoder.encode("foo +bar", "UTF-8") == "foo+%2Bbar"`
* however, this method would return `"foo%20+bar"`.
*/
fun String.toEncodedUriPathSegment(): String = HttpUrl.Builder()
.scheme("https").host("proton.me") // required only to be able to build the Url
.addPathSegment(this)
.build()
.encodedPathSegments
.first()
// endregion
@@ -0,0 +1,17 @@
package me.proton.core.util.kotlin
import kotlin.test.Test
import kotlin.test.assertEquals
class StringUtilsKtTest {
@Test
fun `encoding URI path segments`() {
assertEquals("", "".toEncodedUriPathSegment())
assertEquals("%2F", "/".toEncodedUriPathSegment())
assertEquals("test", "test".toEncodedUriPathSegment())
assertEquals("test%201", "test 1".toEncodedUriPathSegment())
assertEquals("test%2F", "test/".toEncodedUriPathSegment())
assertEquals("test%0A", "test\n".toEncodedUriPathSegment())
assertEquals("foo%20+bar", "foo +bar".toEncodedUriPathSegment())
}
}