email: cap IMAP literal allocation and harden SMTP reply parsing
- Commit
aeefa070b7e51edecb28b257016b31099d075a74- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 02:07 UTC (17 days ago)
- Parents
700e7b7
Two server-controlled crash paths in the raw mail protocols:
ImapWireIo read the IMAP literal length {n} straight from the
server and allocated ByteArray(n) unchecked. A broken or hostile
IMAP server (or a corrupted stream) advertising a gigabyte-scale
literal made the fetch allocate until OutOfMemoryError, an Error
that the surrounding Exception handlers cannot catch, killing the
sync process. Literals over 64 MiB now fail the fetch with an
IOException instead; normal mail is unaffected.
RawSmtpConnection.ehlo mapped every EHLO reply line through
substring(3). A reply line shorter than the 3-char reply code
(an empty or one-word greeting from a broken server) threw
StringIndexOutOfBoundsException. drop() is length-safe and yields
the same capability set for well-formed replies.
Verified: ./gradlew :email:testDevUnitTest (pass, incl. a new
ImapWireIo regression test refusing a 2 GiB literal),
./check email (pass).Diff
email/src/main/java/com/vayunmathur/email/network/imap/ImapWireIo.kt
diff --git a/email/src/main/java/com/vayunmathur/email/network/imap/ImapWireIo.kt b/email/src/main/java/com/vayunmathur/email/network/imap/ImapWireIo.ktindex bd842b1ef3..a11617acdc 100644--- a/email/src/main/java/com/vayunmathur/email/network/imap/ImapWireIo.kt+++ b/email/src/main/java/com/vayunmathur/email/network/imap/ImapWireIo.kt@@ -26,6 +26,11 @@ internal class ImapWireIo( val litMatch = Regex("""\{(\d+)(\+)?\}$""").find(lineStr) if (litMatch != null) { val size = litMatch.groupValues[1].toIntOrNull() ?: 0+ if (size > MAX_LITERAL_BYTES) {+ // The literal length is server-controlled: allocating it+ // unchecked lets a broken or hostile server OOM the process.+ throw IOException("IMAP literal too large: $size bytes")+ } if (size > 0) { val litBytes = ByteArray(size) var read = 0@@ -94,5 +99,8 @@ internal class ImapWireIo( private const val TAG = "RawImap" private const val LOG_LINE_PREVIEW_LEN = 120 private const val LOG_SNIFF_LEN = 600++ /** Largest literal body buffered in memory; bigger mail fails the fetch instead of the process. */+ private const val MAX_LITERAL_BYTES = 64 * 1024 * 1024 } }
email/src/main/java/com/vayunmathur/email/network/smtp/RawSmtpConnection.kt
diff --git a/email/src/main/java/com/vayunmathur/email/network/smtp/RawSmtpConnection.kt b/email/src/main/java/com/vayunmathur/email/network/smtp/RawSmtpConnection.ktindex f524dbec04..09ca81c789 100644--- a/email/src/main/java/com/vayunmathur/email/network/smtp/RawSmtpConnection.kt+++ b/email/src/main/java/com/vayunmathur/email/network/smtp/RawSmtpConnection.kt@@ -63,7 +63,7 @@ class RawSmtpConnection( sendCommand("EHLO $hostname") val resp = readMultilineResponse() ehloResponse = resp- caps = resp.map { it.substring(SMTP_REPLY_CODE_LEN).trim().uppercase() }.toSet()+ caps = resp.map { it.drop(SMTP_REPLY_CODE_LEN).trim().uppercase() }.toSet() if (!isPositive(resp)) throw IOException("EHLO failed: $resp") return resp }
email/src/test/java/com/vayunmathur/email/imap/ImapWireIoTest.kt
diff --git a/email/src/test/java/com/vayunmathur/email/imap/ImapWireIoTest.kt b/email/src/test/java/com/vayunmathur/email/imap/ImapWireIoTest.ktindex 72206d0b1b..f4abc0235f 100644--- a/email/src/test/java/com/vayunmathur/email/imap/ImapWireIoTest.kt+++ b/email/src/test/java/com/vayunmathur/email/imap/ImapWireIoTest.kt@@ -59,6 +59,12 @@ class ImapWireIoTest { assertEquals(0, literal.size) } + @Test+ fun `oversized literal is refused instead of allocated`() {+ val (io, _) = wire("* 1 FETCH (BODY[] {2147483647}\r\n".toByteArray())+ kotlin.test.assertFailsWith<java.io.IOException> { io.readLineWithLiteral() }+ }+ @Test fun `exhausted stream returns null`() { val (io, _) = wire(ByteArray(0))