Repository navigation
Read every literal of a response in IMAP.getReply() - #421
Open
Sahana2524 wants to merge 1 commit into
Open
Sahana2524 wants to merge 1 commit into
Sahana2524 wants to merge 1 commit into
Conversation
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
IMAP.getReply()follows a{n}literal at the end of an untagged line, but once that literal ends it never checks the rest of the line for another one. When a FETCH returns more than one literal in the same response, the bytes of the second literal are read as ordinary response lines, and the first of them is taken as the tagged completion.Ordinary requests hit this:
BODY.PEEK[HEADER] BODY.PEEK[TEXT],RFC822.HEADER RFC822.TEXT, severalBODY[n]parts, or ENVELOPE/BODYSTRUCTURE strings that the server sends as literals (8-bit or CR/LF content). WithBODY[TEXT]the second literal is the message body, written by whoever sent the mail. A body that starts withx OKends the FETCH early, and the remaining body lines stay in the stream and come back as the reply to the client's next command. For example, a body ofmakes a following
fetch("2", "BODY[]")returnSubject: forged by senderinstead of the real message 2, and the connection stays out of step after that. A body whose first line does not look like a tagged reply makes the FETCH fail withMalformedServerReplyException, so that message can't be fetched with those items at all.The change keeps the existing byte counting and adds the missing case: when a literal ends on the current line, the remainder of that line is checked for a following literal, so every literal is consumed before the tagged line is looked for. A literal that ends exactly at the end of a line (for example a mailbox name in a LIST response) is handled as before. This is the only place in the code base that parses server literals.
The new
IMAPTestcase feeds a FETCH response with three literals, the last of which imitates protocol lines. It fails on master withMalformedServerReplyExceptionand passes with this change.mvn(default goal, with-Ddoclint=allas in CI) passes locally on Java 21.mvn; that'smvnon the command line by itself.