Don't allow username or password to contain line break

This commit is contained in:
cketti 2024-03-04 18:34:29 +01:00
parent c07e1582d4
commit d2296b3955
2 changed files with 113 additions and 0 deletions

View file

@ -22,6 +22,8 @@ data class ServerSettings @JvmOverloads constructor(
init {
require(type == type.lowercase()) { "type must be all lower case" }
require(username.contains(LINE_BREAK).not()) { "username must not contain line break" }
require(password?.contains(LINE_BREAK) != true) { "password must not contain line break" }
}
fun newPassword(newPassword: String?): ServerSettings {
@ -31,4 +33,8 @@ data class ServerSettings @JvmOverloads constructor(
fun newAuthenticationType(authType: AuthType): ServerSettings {
return this.copy(authenticationType = authType)
}
companion object {
private val LINE_BREAK = "[\\r\\n]".toRegex()
}
}

View file

@ -0,0 +1,107 @@
package com.fsck.k9.mail
import assertk.assertFailure
import assertk.assertions.hasMessage
import assertk.assertions.isInstanceOf
import kotlin.test.Test
class ServerSettingsTest {
@Test
fun `creating typical ServerSettings should not throw`() {
ServerSettings(
type = "imap",
host = "imap.domain.example",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "123456",
clientCertificateAlias = null,
)
}
@Test
fun `type that is not all lower case should throw`() {
assertFailure {
ServerSettings(
type = "IMAP",
host = "imap.domain.example",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "123456",
clientCertificateAlias = null,
)
}.isInstanceOf<IllegalArgumentException>()
.hasMessage("type must be all lower case")
}
@Test
fun `username containing LF should throw`() {
assertFailure {
ServerSettings(
type = "imap",
host = "imap.domain.example",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user\nname",
password = "123456",
clientCertificateAlias = null,
)
}.isInstanceOf<IllegalArgumentException>()
.hasMessage("username must not contain line break")
}
@Test
fun `username containing CR should throw`() {
assertFailure {
ServerSettings(
type = "imap",
host = "imap.domain.example",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user\rname",
password = "123456",
clientCertificateAlias = null,
)
}.isInstanceOf<IllegalArgumentException>()
.hasMessage("username must not contain line break")
}
@Test
fun `password containing LF should throw`() {
assertFailure {
ServerSettings(
type = "imap",
host = "imap.domain.example",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "123456\n",
clientCertificateAlias = null,
)
}.isInstanceOf<IllegalArgumentException>()
.hasMessage("password must not contain line break")
}
@Test
fun `password containing CR should throw`() {
assertFailure {
ServerSettings(
type = "imap",
host = "imap.domain.example",
port = 993,
connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED,
authenticationType = AuthType.PLAIN,
username = "user",
password = "123456\r",
clientCertificateAlias = null,
)
}.isInstanceOf<IllegalArgumentException>()
.hasMessage("password must not contain line break")
}
}