Use relaxed mode to parse email addresses from message headers

This commit is contained in:
cketti 2023-06-15 19:02:57 +02:00
parent c96d0ce65a
commit 656a9d3862
16 changed files with 110 additions and 48 deletions

View file

@ -1,7 +1,7 @@
package com.fsck.k9.helper
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrNull
interface ContactNameProvider {
fun getNameForAddress(address: String): String?
@ -11,6 +11,8 @@ class RealContactNameProvider(
private val contactRepository: ContactRepository,
) : ContactNameProvider {
override fun getNameForAddress(address: String): String? {
return contactRepository.getContactFor(address.toEmailAddress())?.name
return address.toEmailAddressOrNull()?.let { emailAddress ->
contactRepository.getContactFor(emailAddress)?.name
}
}
}

View file

@ -6,7 +6,7 @@ import android.text.SpannableStringBuilder
import android.text.TextUtils
import android.text.style.ForegroundColorSpan
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrNull
import com.fsck.k9.CoreResourceProvider
import com.fsck.k9.K9.contactNameColor
import com.fsck.k9.K9.isChangeContactNameColor
@ -101,7 +101,7 @@ class MessageHelper(
if (!showCorrespondentNames) {
return address.address
} else if (contactRepository != null) {
val name = contactRepository.getContactFor(address.address.toEmailAddress())?.name
val name = contactRepository.getContactName(address)
if (name != null) {
return if (changeContactNameColor) {
val coloredName = SpannableString(name)
@ -124,6 +124,12 @@ class MessageHelper(
}
}
private fun ContactRepository.getContactName(address: Address): String? {
return address.address.toEmailAddressOrNull()?.let { emailAddress ->
getContactFor(emailAddress)?.name
}
}
private fun isSpoofAddress(displayName: String): Boolean {
return displayName.contains("@") && SPOOF_ADDRESS_PATTERN.matcher(displayName).find()
}

View file

@ -5,7 +5,7 @@ import android.text.SpannableString
import app.k9mail.core.android.common.contact.Contact
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.EmailAddress
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrThrow
import assertk.assertThat
import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
@ -143,6 +143,6 @@ class MessageHelperTest : RobolectricTest() {
}
private companion object {
val EMAIL_ADDRESS = "test@testor.com".toEmailAddress()
val EMAIL_ADDRESS = "test@testor.com".toEmailAddressOrThrow()
}
}

View file

@ -1,7 +1,7 @@
package com.fsck.k9.notification
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrNull
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.mail.Flag
@ -88,7 +88,7 @@ class K9NotificationStrategy(
}
if (account.isNotifyContactsMailOnly &&
!contactRepository.hasAnyContactFor(message.from.asList().mapNotNull { it.address.toEmailAddress() })
!contactRepository.hasAnyContactFor(message.from.asList().mapNotNull { it.address.toEmailAddressOrNull() })
) {
Timber.v("No notification: Message is not from a known contact")
return false

View file

@ -3,8 +3,9 @@ package com.fsck.k9.contacts
import android.content.ContentResolver
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrNull
import timber.log.Timber
internal class ContactPhotoLoader(
@ -12,7 +13,7 @@ internal class ContactPhotoLoader(
private val contactRepository: ContactRepository,
) {
fun loadContactPhoto(emailAddress: String): Bitmap? {
val photoUri = contactRepository.getContactFor(emailAddress.toEmailAddress())?.photoUri ?: return null
val photoUri = getPhotoUri(emailAddress) ?: return null
return try {
contentResolver.openInputStream(photoUri).use { inputStream ->
BitmapFactory.decodeStream(inputStream)
@ -22,4 +23,10 @@ internal class ContactPhotoLoader(
null
}
}
private fun getPhotoUri(email: String): Uri? {
return email.toEmailAddressOrNull()?.let { emailAddress ->
contactRepository.getContactFor(emailAddress)?.photoUri
}
}
}

View file

@ -2,12 +2,13 @@ package com.fsck.k9.ui.messagedetails
import android.app.PendingIntent
import android.content.res.Resources
import android.net.Uri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.k9mail.core.android.common.contact.CachingRepository
import app.k9mail.core.android.common.contact.ContactPermissionResolver
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrNull
import com.fsck.k9.Account
import com.fsck.k9.controller.MessageReference
import com.fsck.k9.helper.ClipboardManager
@ -30,6 +31,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
@Suppress("TooManyFunctions")
internal class MessageDetailsViewModel(
private val resources: Resources,
private val messageRepository: MessageRepository,
@ -130,11 +132,17 @@ internal class MessageDetailsViewModel(
Participant(
displayName = displayName,
emailAddress = emailAddress,
contactLookupUri = contactRepository.getContactFor(emailAddress.toEmailAddress())?.uri,
contactLookupUri = getContactLookupUri(emailAddress),
)
}
}
private fun getContactLookupUri(email: String): Uri? {
return email.toEmailAddressOrNull()?.let { emailAddress ->
contactRepository.getContactFor(emailAddress)?.uri
}
}
private fun Folder.toFolderInfo(): FolderInfoUi {
return FolderInfoUi(
displayName = folderNameFormatter.displayName(this),

View file

@ -18,7 +18,8 @@ import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.TextView
import app.k9mail.core.android.common.contact.ContactRepository
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.EmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrNull
import com.fsck.k9.Account
import com.fsck.k9.Account.ShowPictures
import com.fsck.k9.mail.Message
@ -262,15 +263,15 @@ class MessageTopView(
return false
}
val senderEmailAddress = getSenderEmailAddress(message) ?: return false
return contactRepository.hasContactFor(senderEmailAddress.toEmailAddress())
return contactRepository.hasContactFor(senderEmailAddress)
}
private fun getSenderEmailAddress(message: Message): String? {
private fun getSenderEmailAddress(message: Message): EmailAddress? {
val from = message.from
return if (from == null || from.isEmpty()) {
null
} else {
from[0].address
from[0].address.toEmailAddressOrNull()
}
}

View file

@ -6,7 +6,7 @@ import app.k9mail.autodiscovery.autoconfig.AutoconfigUrlConfig
import app.k9mail.autodiscovery.autoconfig.createIspDbAutoconfigDiscovery
import app.k9mail.autodiscovery.autoconfig.createMxLookupAutoconfigDiscovery
import app.k9mail.autodiscovery.autoconfig.createProviderAutoconfigDiscovery
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toUserEmailAddress
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.options.flag
@ -57,7 +57,7 @@ class AutoDiscoveryCli : CliktCommand(
val mxDiscovery = createMxLookupAutoconfigDiscovery(okHttpClient)
val runnables = listOf(providerDiscovery, ispDbDiscovery, mxDiscovery)
.flatMap { it.initDiscovery(emailAddress.toEmailAddress()) }
.flatMap { it.initDiscovery(emailAddress.toUserEmailAddress()) }
val serialRunner = SerialRunner(runnables)
return runBlocking {

View file

@ -1,12 +1,12 @@
package app.k9mail.core.android.common.contact
import android.net.Uri
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toEmailAddressOrThrow
const val CONTACT_ID = 123L
const val CONTACT_NAME = "user name"
const val CONTACT_LOOKUP_KEY = "0r1-4F314D4F2F294F29"
val CONTACT_EMAIL_ADDRESS = "user@example.com".toEmailAddress()
val CONTACT_EMAIL_ADDRESS = "user@example.com".toEmailAddressOrThrow()
val CONTACT_URI: Uri = Uri.parse("content://com.android.contacts/contacts/lookup/$CONTACT_LOOKUP_KEY/$CONTACT_ID")
val CONTACT_PHOTO_URI: Uri = Uri.parse("content://com.android.contacts/display_photo/$CONTACT_ID")

View file

@ -107,10 +107,32 @@ class EmailAddress internal constructor(
}
companion object {
fun parse(address: String, config: EmailAddressParserConfig = EmailAddressParserConfig.DEFAULT): EmailAddress {
fun parse(address: String, config: EmailAddressParserConfig = EmailAddressParserConfig.RELAXED): EmailAddress {
return EmailAddressParser(address, config).parse()
}
}
}
fun String.toEmailAddress() = EmailAddress.parse(this)
/**
* Converts this string to an [EmailAddress] instance using [EmailAddressParserConfig.RELAXED].
*/
fun String.toEmailAddressOrThrow() = EmailAddress.parse(this, EmailAddressParserConfig.RELAXED)
/**
* Converts this string to an [EmailAddress] instance using [EmailAddressParserConfig.RELAXED].
*/
@Suppress("SwallowedException")
fun String.toEmailAddressOrNull(): EmailAddress? {
return try {
EmailAddress.parse(this, EmailAddressParserConfig.RELAXED)
} catch (e: EmailAddressParserException) {
null
}
}
/**
* Convert this string into an [EmailAddress] instance using [EmailAddressParserConfig.LIMITED].
*
* Use this when validating the email address a user wants to add to an account/identity.
*/
fun String.toUserEmailAddress() = EmailAddress.parse(this, EmailAddressParserConfig.LIMITED)

View file

@ -38,12 +38,28 @@ data class EmailAddressParserConfig(
val isEmptyLocalPartAllowed: Boolean = false,
) {
companion object {
val DEFAULT = EmailAddressParserConfig(
isLocalPartLengthCheckEnabled = true,
isEmailAddressLengthCheckEnabled = true,
/**
* This allows local parts requiring quoted strings and disables length checks for the local part and the
* whole email address.
*/
val RELAXED = EmailAddressParserConfig(
isLocalPartLengthCheckEnabled = false,
isEmailAddressLengthCheckEnabled = false,
isQuotedLocalPartAllowed = true,
isLocalPartRequiringQuotedStringAllowed = true,
isEmptyLocalPartAllowed = false,
)
/**
* This only allows a subset of valid email addresses. Use this when validating the email address a user wants
* to add to an account/identity.
*/
val LIMITED = EmailAddressParserConfig(
isLocalPartLengthCheckEnabled = true,
isEmailAddressLengthCheckEnabled = true,
isQuotedLocalPartAllowed = false,
isLocalPartRequiringQuotedStringAllowed = false,
isEmptyLocalPartAllowed = false,
)
}
}

View file

@ -6,7 +6,7 @@ import app.k9mail.autodiscovery.api.AutoDiscoveryService
import app.k9mail.autodiscovery.api.ConnectionSecurity
import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.SmtpServerSettings
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toUserEmailAddress
import app.k9mail.core.common.net.toHostname
import app.k9mail.core.common.net.toPort
import app.k9mail.feature.account.setup.domain.DomainContract
@ -34,7 +34,7 @@ internal class GetAutoDiscovery(
return provideWithDelay(fakeResult)
}
return service.discover(emailAddress.toEmailAddress())!!
return service.discover(emailAddress.toUserEmailAddress())!!
}
@Suppress("MagicNumber")

View file

@ -2,7 +2,7 @@ package app.k9mail.autodiscovery.autoconfig
import app.k9mail.autodiscovery.autoconfig.MockAutoconfigFetcher.Companion.RESULT_ONE
import app.k9mail.autodiscovery.autoconfig.MockAutoconfigFetcher.Companion.RESULT_TWO
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toUserEmailAddress
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.containsExactly
@ -13,7 +13,7 @@ import kotlin.test.Test
import kotlinx.coroutines.test.runTest
import okhttp3.HttpUrl.Companion.toHttpUrl
private val IRRELEVANT_EMAIL_ADDRESS = "irrelevant@domain.example".toEmailAddress()
private val IRRELEVANT_EMAIL_ADDRESS = "irrelevant@domain.example".toUserEmailAddress()
class AutoconfigDiscoveryTest {
private val urlProvider = MockAutoconfigUrlProvider()
@ -22,7 +22,7 @@ class AutoconfigDiscoveryTest {
@Test
fun `AutoconfigFetcher and AutoconfigParser should only be called when AutoDiscoveryRunnable is run`() = runTest {
val emailAddress = "user@domain.example".toEmailAddress()
val emailAddress = "user@domain.example".toUserEmailAddress()
val autoconfigUrl = "https://autoconfig.domain.invalid/mail/config-v1.1.xml".toHttpUrl()
urlProvider.addResult(listOf(autoconfigUrl))
autoconfigFetcher.addResult(RESULT_ONE)

View file

@ -2,7 +2,7 @@ package app.k9mail.autodiscovery.autoconfig
import app.k9mail.autodiscovery.api.AutoDiscoveryResult.NoUsableSettingsFound
import app.k9mail.autodiscovery.autoconfig.MockAutoconfigFetcher.Companion.RESULT_ONE
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toUserEmailAddress
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.containsExactly
@ -28,7 +28,7 @@ class MxLookupAutoconfigDiscoveryTest {
@Test
fun `AutoconfigUrlProvider should be called with MX base domain`() = runTest {
val emailAddress = "user@company.example".toEmailAddress()
val emailAddress = "user@company.example".toUserEmailAddress()
mxResolver.addResult("mx.emailprovider.example".toDomain())
urlProvider.addResult(listOf("https://ispdb.invalid/emailprovider.example".toHttpUrl()))
autoconfigFetcher.addResult(RESULT_ONE)
@ -50,7 +50,7 @@ class MxLookupAutoconfigDiscoveryTest {
@Test
fun `AutoconfigUrlProvider should be called with MX base domain and subdomain`() = runTest {
val emailAddress = "user@company.example".toEmailAddress()
val emailAddress = "user@company.example".toUserEmailAddress()
mxResolver.addResult("mx.something.emailprovider.example".toDomain())
urlProvider.apply {
addResult(listOf("https://ispdb.invalid/something.emailprovider.example".toHttpUrl()))
@ -74,7 +74,7 @@ class MxLookupAutoconfigDiscoveryTest {
@Test
fun `skip Autoconfig lookup when MX lookup does not return a result`() = runTest {
val emailAddress = "user@company.example".toEmailAddress()
val emailAddress = "user@company.example".toUserEmailAddress()
mxResolver.addResult(emptyList())
val autoDiscoveryRunnables = discovery.initDiscovery(emailAddress)
@ -88,7 +88,7 @@ class MxLookupAutoconfigDiscoveryTest {
@Test
fun `skip Autoconfig lookup when base domain of MX record is email domain`() = runTest {
val emailAddress = "user@company.example".toEmailAddress()
val emailAddress = "user@company.example".toUserEmailAddress()
mxResolver.addResult("mx.company.example".toDomain())
val autoDiscoveryRunnables = discovery.initDiscovery(emailAddress)
@ -102,7 +102,7 @@ class MxLookupAutoconfigDiscoveryTest {
@Test
fun `isTrusted should be false when MxLookupResult_isTrusted is false`() = runTest {
val emailAddress = "user@company.example".toEmailAddress()
val emailAddress = "user@company.example".toUserEmailAddress()
mxResolver.addResult("mx.emailprovider.example".toDomain(), isTrusted = false)
urlProvider.addResult(listOf("https://ispdb.invalid/emailprovider.example".toHttpUrl()))
autoconfigFetcher.addResult(RESULT_ONE.copy(isTrusted = true))
@ -115,7 +115,7 @@ class MxLookupAutoconfigDiscoveryTest {
@Test
fun `isTrusted should be false when AutoDiscoveryResult_isTrusted from AutoconfigFetcher is false`() = runTest {
val emailAddress = "user@company.example".toEmailAddress()
val emailAddress = "user@company.example".toUserEmailAddress()
mxResolver.addResult("mx.emailprovider.example".toDomain(), isTrusted = true)
urlProvider.addResult(listOf("https://ispdb.invalid/emailprovider.example".toHttpUrl()))
autoconfigFetcher.addResult(RESULT_ONE.copy(isTrusted = false))

View file

@ -1,6 +1,6 @@
package app.k9mail.autodiscovery.autoconfig
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toUserEmailAddress
import app.k9mail.core.common.net.toDomain
import assertk.assertThat
import assertk.assertions.containsExactly
@ -8,7 +8,7 @@ import org.junit.Test
class ProviderAutoconfigUrlProviderTest {
private val domain = "domain.example".toDomain()
private val email = "test@domain.example".toEmailAddress()
private val email = "test@domain.example".toUserEmailAddress()
@Test
fun `getAutoconfigUrls with http allowed and email address included`() {

View file

@ -8,7 +8,7 @@ import app.k9mail.autodiscovery.api.ImapServerSettings
import app.k9mail.autodiscovery.api.SmtpServerSettings
import app.k9mail.autodiscovery.autoconfig.AutoconfigParserResult.ParserError
import app.k9mail.autodiscovery.autoconfig.AutoconfigParserResult.Settings
import app.k9mail.core.common.mail.toEmailAddress
import app.k9mail.core.common.mail.toUserEmailAddress
import app.k9mail.core.common.net.toHostname
import app.k9mail.core.common.net.toPort
import assertk.assertThat
@ -55,13 +55,13 @@ class RealAutoconfigParserTest {
</clientConfig>
""".trimIndent()
private val irrelevantEmailAddress = "irrelevant@domain.example".toEmailAddress()
private val irrelevantEmailAddress = "irrelevant@domain.example".toUserEmailAddress()
@Test
fun `minimal data`() {
val inputStream = minimalConfig.byteInputStream()
val result = parser.parseSettings(inputStream, email = "user@domain.example".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "user@domain.example".toUserEmailAddress())
assertThat(result).isNotNull().isEqualTo(
Settings(
@ -87,7 +87,7 @@ class RealAutoconfigParserTest {
fun `real-world data`() {
val inputStream = javaClass.getResourceAsStream("/2022-11-19-googlemail.com.xml")!!
val result = parser.parseSettings(inputStream, email = "test@gmail.com".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "test@gmail.com".toUserEmailAddress())
assertThat(result).isNotNull().isEqualTo(
Settings(
@ -117,7 +117,7 @@ class RealAutoconfigParserTest {
element("outgoingServer > username").text("%EMAILDOMAIN%")
}
val result = parser.parseSettings(inputStream, email = "user@domain.example".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "user@domain.example".toUserEmailAddress())
assertThat(result).isNotNull().isEqualTo(
Settings(
@ -149,7 +149,7 @@ class RealAutoconfigParserTest {
element("incomingServer > username").prepend("<!-- comment -->")
}
val result = parser.parseSettings(inputStream, email = "user@domain.example".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "user@domain.example".toUserEmailAddress())
assertThat(result).isInstanceOf<Settings>()
.prop(Settings::incomingServerSettings).isEqualTo(
@ -169,7 +169,7 @@ class RealAutoconfigParserTest {
element("incomingServer").insertBefore("""<incomingServer type="smtp"/>""")
}
val result = parser.parseSettings(inputStream, email = "user@domain.example".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "user@domain.example".toUserEmailAddress())
assertThat(result).isInstanceOf<Settings>()
.prop(Settings::incomingServerSettings).isEqualTo(
@ -189,7 +189,7 @@ class RealAutoconfigParserTest {
element("outgoingServer").insertBefore("""<outgoingServer type="imap"/>""")
}
val result = parser.parseSettings(inputStream, email = "user@domain.example".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "user@domain.example".toUserEmailAddress())
assertThat(result).isInstanceOf<Settings>()
.prop(Settings::outgoingServerSettings).isEqualTo(
@ -209,7 +209,7 @@ class RealAutoconfigParserTest {
element("incomingServer > authentication").insertBefore("<authentication></authentication>")
}
val result = parser.parseSettings(inputStream, email = "user@domain.example".toEmailAddress())
val result = parser.parseSettings(inputStream, email = "user@domain.example".toUserEmailAddress())
assertThat(result).isInstanceOf<Settings>()
.prop(Settings::incomingServerSettings).isEqualTo(