Skip to content

Read every literal of a response in IMAP.getReply() - #421

Open
Sahana2524 wants to merge 1 commit into
apache:masterfrom
Sahana2524:imap-multiple-literals
Open

Sahana2524 wants to merge 1 commit into
apache:masterfrom
Sahana2524:imap-multiple-literals

Conversation

@Sahana2524

Copy link
Copy Markdown

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, several BODY[n] parts, or ENVELOPE/BODYSTRUCTURE strings that the server sends as literals (8-bit or CR/LF content). With BODY[TEXT] the second literal is the message body, written by whoever sent the mail. A body that starts with x OK ends 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 of

x OK
* 2 FETCH (BODY[] {27}
Subject: forged by sender
)
y OK FETCH completed

makes a following fetch("2", "BODY[]") return Subject: forged by sender instead 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 with MalformedServerReplyException, 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 IMAPTest case feeds a FETCH response with three literals, the last of which imitates protocol lines. It fails on master with MalformedServerReplyException and passes with this change. mvn (default goal, with -Ddoclint=all as in CI) passes locally on Java 21.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant