email: non-ASCII mailboxes are UTF-7-encoded on the wire
- Commit
66c815f1d1d3cf337c4345f8fc98917cbebce934- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 11:40 UTC (4 days ago)
- Parents
2f97545
LIST decoded RFC 3501 modified UTF-7 (wire Caf&AOk- stored and displayed as Café), but SELECT/EXAMINE/LIST never re-encoded before sending, and the connection writes US_ASCII — so é went over the wire as '?': SELECT "Caf?" drew a server NO and the folder was silently skipped on every sync (caught and logged only). A&B needed A&-B the same way. The encoder (encodeModifiedUtf7) existed and was unit-tested but had zero production callers. The wire layer now encodes mailbox names (select, examine, list ref + pattern); ASCII names like INBOX and * are byte-identical, so no other account behaviour changes. Wire- level regression tests against the fake server assert the encoded commands (and no Caf?), plus an end-to-end fetch of a Café folder — both failed pre-fix. Sync read-status, attachment decoding and notification actions were audited and cleared. Verified: :email:testDevUnitTest 594 tests pass; lint errors are pre-existing on untouched files.
Diff
email/src/main/java/com/vayunmathur/email/network/imap/RawImapConnection.kt
diff --git a/email/src/main/java/com/vayunmathur/email/network/imap/RawImapConnection.kt b/email/src/main/java/com/vayunmathur/email/network/imap/RawImapConnection.ktindex d8963cebab..92a5fd6418 100644--- a/email/src/main/java/com/vayunmathur/email/network/imap/RawImapConnection.kt+++ b/email/src/main/java/com/vayunmathur/email/network/imap/RawImapConnection.kt@@ -218,7 +218,7 @@ class RawImapConnection( fun select(mailbox: String): ImapSelectResult { val tag = nextTag()- sendLine("$tag SELECT ${escapeString(mailbox)}")+ sendLine("$tag SELECT ${escapeString(ImapParser.encodeModifiedUtf7(mailbox))}") return mailboxOps.collectSelect(tag, mailbox, "SELECT") } @@ -241,7 +241,10 @@ class RawImapConnection( ) { fun list(ref: String, pattern: String): List<ImapListEntry> { val tag = nextTag()- sendLine("$tag LIST ${escapeString(ref)} ${escapeString(pattern)}")+ sendLine(+ "$tag LIST ${escapeString(ImapParser.encodeModifiedUtf7(ref))} " ++ escapeString(ImapParser.encodeModifiedUtf7(pattern)),+ ) return collectList(tag) } @@ -294,7 +297,7 @@ class RawImapConnection( fun examine(mailbox: String): ImapSelectResult { val tag = nextTag()- sendLine("$tag EXAMINE ${escapeString(mailbox)}")+ sendLine("$tag EXAMINE ${escapeString(ImapParser.encodeModifiedUtf7(mailbox))}") var exists = 0 var finalLine = "" var done = false
email/src/test/java/com/vayunmathur/email/imap/ImapMailboxUtf7WireTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/imap/ImapMailboxUtf7WireTest.kt b/email/src/test/java/com/vayunmathur/email/imap/ImapMailboxUtf7WireTest.ktnew file mode 100644index 0000000000..17c505f493--- /dev/null+++ b/email/src/test/java/com/vayunmathur/email/imap/ImapMailboxUtf7WireTest.kt@@ -0,0 +1,69 @@+package com.vayunmathur.email.imap++import com.vayunmathur.email.network.imap.ImapClient+import com.vayunmathur.email.network.imap.RawImapConnection+import com.vayunmathur.email.platform.EmailManager+import com.vayunmathur.email.platform.ServerConfig+import kotlin.test.AfterTest+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import kotlinx.coroutines.runBlocking+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * Regression: LIST decodes modified UTF-7 mailbox names (RFC 3501 §5.1.3),+ * e.g. wire `Caf&AOk-` is stored/displayed as `Café`. SELECT/EXAMINE must+ * re-encode that stored name on the wire; sending it raw through the+ * US-ASCII wire writer turned `é` into `?` (`SELECT "Caf?"`), so every+ * sync of a non-ASCII folder failed with NO on real servers.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class ImapMailboxUtf7WireTest {++ private lateinit var server: FakeImapServer++ @BeforeTest+ fun startServer() {+ server = FakeImapServer()+ server.start()+ }++ @AfterTest+ fun stopServer() = server.close()++ private fun config() = ServerConfig("127.0.0.1", server.port, useSsl = false)+ private val auth = EmailManager.AuthType.Password("secret")++ @Test+ fun `select and examine re-encode non-ascii mailbox as modified utf7`() {+ RawImapConnection(config()).use { conn ->+ conn.connect()+ conn.login("[email protected]", "secret")+ conn.select("Café")+ conn.examine("Café")+ conn.select("A&B")+ conn.select("INBOX")+ }+ val commands = synchronized(server.receivedCommands) { server.receivedCommands.toList() }+ assertTrue(commands.any { it.contains("SELECT \"Caf&AOk-\"") }, commands.toString())+ assertTrue(commands.any { it.contains("EXAMINE \"Caf&AOk-\"") }, commands.toString())+ assertTrue(commands.any { it.contains("SELECT \"A&-B\"") }, commands.toString())+ // ASCII names are sent untouched.+ assertTrue(commands.any { it.contains("SELECT \"INBOX\"") }, commands.toString())+ assertTrue(commands.none { it.contains("Caf?") }, commands.toString())+ }++ @Test+ fun `client sync of a utf7 folder selects the encoded name`() = runBlocking {+ val (messages, _) = ImapClient.fetchMessages(config(), "[email protected]", auth, "Café", limit = 10)+ assertEquals(listOf(102L, 101L), messages.map { it.id })+ assertTrue(messages.all { it.folderName == "Café" })+ val commands = synchronized(server.receivedCommands) { server.receivedCommands.toList() }+ assertTrue(commands.any { it.contains("SELECT \"Caf&AOk-\"") }, commands.toString())+ }+}