IMAP: custom FETCH with multiple body sections silently drops every literal after the first
I did this
Requested two body sections of one message in a single custom FETCH, against a stock Dovecot server (message 1 in INBOX has a 210-byte header and a 68-byte text part):
curl "imap://127.0.0.1:14300/INBOX" -u user:pass \
-X "UID FETCH 1 (BODY.PEEK[HEADER] BODY.PEEK[TEXT])"The server's response to that command, captured from a manual session with the same server, contains both literals:
* 1 FETCH (UID 1 BODY[HEADER] {210}
...210 bytes...
BODY[TEXT] {68}
...68 bytes...
)
b OK Fetch completedcurl outputs 247 bytes: the * 1 FETCH ... header line plus the first literal. The BODY[TEXT] {68} continuation and its 68 bytes are silently discarded — %{size_download} is byte-identical to running the command with only BODY.PEEK[HEADER].
I expected the following
Either the whole response (all literals) or a visible error. The silent truncation is the painful part: multi-section FETCH is legal IMAP4rev1 and the natural way to fetch header + MIME structure + parts in one round trip, and an application gets no signal that most of what it asked for was dropped. I found this because a mail client built on libcurl passed all its tests and then silently lost attachment parts against a real server.
Where this happens: a custom single-message FETCH is routed to imap_state_listsearch_resp(), whose literal handling (added in #19246 for #18847) sets up the transfer for exactly one {size}. When that literal completes, the remainder of the same FETCH response goes back through the line parser, and the BODY[TEXT] {68} continuation line is not recognized as carrying another literal. The URL-path handler (imap_state_fetch_resp()) has the same one-literal shape, but URLs can only request one SECTION, so only the custom-request path can hit this.
Related but distinct from #22865: that one is about the listing classification skipping literal framing entirely; this is the non-listing custom FETCH path dropping the second and later literals.
I am happy to work on a patch. The design question I would like guidance on first: should curl (a) deliver every literal of the response, interleaved with the response text the way the first literal already is today, or (b) detect the additional literal and fail the transfer (e.g. CURLE_WEIRD_SERVER_REPLY) rather than truncate silently?
curl/libcurl version
curl 8.18.0 (x86_64-pc-linux-gnu) libcurl/8.18.0 — also confirmed present in current master by reading lib/imap.c.
operating system
Ubuntu 25.10, x86_64
Source: curl/curl