From 931bcd9e16eacc8243c3cd968120180ce6a14a43 Mon Sep 17 00:00:00 2001 From: Sahana Bogar Date: Fri, 6 Nov 2026 19:42:44 +0530 Subject: [PATCH] Read every literal of a response in IMAP.getReply() getReply() only honored the first {n} literal of an untagged response. Once that literal ended, the rest of the line was not checked for another literal, so the bytes of the next literal were parsed as response lines and the first of them could be taken as the tagged completion. With FETCH items such as BODY[HEADER] BODY[TEXT], the second literal is message text chosen by the sender, who can end the FETCH early and have the rest of the body read as the reply to the next command. When a literal ends on the current line, check the remainder of that line for a following literal. Assisted-By: Claude Code (Claude Opus 5.5) --- .../org/apache/commons/net/imap/IMAP.java | 7 +++++- .../org/apache/commons/net/imap/IMAPTest.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/net/imap/IMAP.java b/src/main/java/org/apache/commons/net/imap/IMAP.java index 395be9c9d..bd9ac115a 100644 --- a/src/main/java/org/apache/commons/net/imap/IMAP.java +++ b/src/main/java/org/apache/commons/net/imap/IMAP.java @@ -280,7 +280,12 @@ private void getReply(final boolean wantTag) throws IOException { throw new EOFException("Connection closed without indication."); } replyLines.add(line); - literalCount -= line.length() + 2; // Allow for CRLF + if (literalCount >= line.length() + 2) { + literalCount -= line.length() + 2; // Allow for CRLF + } else { + // The literal ends on this line, and the rest of the line may introduce another literal. + literalCount = literalCount < line.length() ? IMAPReply.literalCount(line.substring(literalCount)) : -1; + } } if (isMultiLine) { final IMAPChunkListener il = chunkListener; diff --git a/src/test/java/org/apache/commons/net/imap/IMAPTest.java b/src/test/java/org/apache/commons/net/imap/IMAPTest.java index 1193ec0a1..52c43b549 100644 --- a/src/test/java/org/apache/commons/net/imap/IMAPTest.java +++ b/src/test/java/org/apache/commons/net/imap/IMAPTest.java @@ -22,8 +22,13 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.StringReader; +import java.io.StringWriter; import java.util.stream.Stream; +import org.apache.commons.net.io.CRLFLineReader; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; @@ -84,6 +89,25 @@ void testConstructDefaultIMAP() { assertEquals(0, imap.getReplyStrings().length); } + @Test + void testGetReplyReadsEveryLiteral() throws IOException { + final String header = "Subject: hi\r\n\r\n"; + // Message text is not protocol data, even when it looks like a tagged reply and a FETCH response. + final String text = "x OK\r\n* 2 FETCH (BODY[] {6}\r\nfake\r\n)\r\ny OK FETCH completed\r\n"; + final String message2 = "Subject: two\r\n"; + final IMAP imap = new IMAP(); + imap._reader = new CRLFLineReader(new StringReader("* 1 FETCH (BODY[HEADER] {" + header.length() + "}\r\n" + header + " BODY[1] {3}\r\nabc BODY[TEXT] {" + + text.length() + "}\r\n" + text + ")\r\nA001 OK FETCH completed\r\n* 2 FETCH (BODY[] {" + message2.length() + "}\r\n" + message2 + + ")\r\nA002 OK FETCH completed\r\n")); + imap.__writer = new BufferedWriter(new StringWriter()); + assertEquals(IMAPReply.OK, imap.sendCommand(IMAPCommand.FETCH, "1 (BODY.PEEK[HEADER] BODY.PEEK[1] BODY.PEEK[TEXT])")); + final String[] reply = imap.getReplyStrings(); + assertEquals(12, reply.length); + assertEquals("A001 OK FETCH completed", reply[reply.length - 1]); + assertEquals(IMAPReply.OK, imap.sendCommand(IMAPCommand.FETCH, "2 BODY[]")); + assertEquals("Subject: two", imap.getReplyStrings()[1]); + } + @Test void testQuoteMailboxNameNullInput() { assertNull(IMAP.quoteMailboxName(null));