Skip to content

IMAP: custom FETCH with multiple body sections silently drops every literal after the first #23010

Closed

Description

@sean-rowe

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 completed

curl 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

Activity

  1. xhon-pelushi commented on Sep 28, 2026

    @xhon-pelushi
    Contributor

    I dug into this and can confirm the diagnosis, including the routing detail. Posting the analysis plus a working prototype, because the fix that actually works has an architectural tradeoff that is the maintainers' call rather than mine.

    Confirming the routing

    UID FETCH 1 (...) does fall to the transfer-setup branch, and the reason is narrower than "custom fetch". imap_parse_custom_request() splits on the first space, so custom = "UID" and custom_params = " FETCH 1 (BODY.PEEK[HEADER] BODY.PEEK[TEXT])". is_custom_fetch_listing_match() then walks " 1 (...": it consumes the digit and requires : or , next, finds a space, and returns FALSE. So it only recognises a range or list (1:*, 1,2,3). A single message number is not a "listing", so the repro takes the else if(imapcode == '*') branch.

    Where the bytes go

    Two places, and the already-buffered case loses them one step earlier than the streaming case:

    • size += len; ... data->req.maxdownload = size; Curl_xfer_setup_recv(...) then imap_state(data, imapc, IMAP_STOP) — the transfer is sized for header line + first literal only, and the DO phase ends, so the response parser never runs again on this request and never sees the second {68}.
    • If the whole response is already buffered, if(chunk > (size_t)size) chunk = (size_t)size; clamps the write to the first literal and parks the rest in pp->overflow. The only code that drains overflow as body is in imap_state_fetch_resp() (the IMAP_FETCH state), so on the IMAP_LIST path it is never emitted. That is the silent part.

    Note on #22951

    I have an open PR (#22951) that adds literal framing for custom FETCH listings. It does not fix this. Its new branch is guarded by is_custom_fetch_listing(), which is FALSE here, so literal_bytes stays 0 and the new code is inert for this repro. Worth stating so nobody assumes that PR covers it.

    Prototype, and the tradeoff

    The approach that actually works is to drain the literal in Curl_pp_readresp() before the line splitter, via an optional cliteral() callback on struct pingpong, so literal content never becomes a "line". That also removes the 64 KB DYN_PINGPPONG_CMD ceiling for literals and stops content leaking into the CLIENTWRITE_INFO header stream.

    I have this building warning-free locally with a test that fails before and passes after (both literals delivered), and the full IMAP suite (800-899) plus 1694-1697 green at 80/80.

    But I do not want to just throw it at you, because:

    1. It touches lib/pingpong.c/.h for an IMAP-only bug, and grows struct pingpong by 16 bytes for every FTP/POP3/SMTP connection.
    2. It supersedes imap: honour literal framing for custom FETCH listings #22951 rather than complementing it — it removes that PR's literal_bytes mechanism and enforces the same property one layer lower. If you take this direction, imap: honour literal framing for custom FETCH listings #22951 should be closed.
    3. There is a real regression to disclose: on the -X 'UID FETCH ...' path the body would be written from the DO phase instead of the transfer loop, so --limit-rate / CURLOPT_MAX_RECV_SPEED_LARGE would no longer throttle that path. imap_state_fetch_resp() is untouched, so the ordinary imap://host/;UID=1 download keeps Curl_xfer_setup_recv() and both properties. Progress and --max-filesize still work.

    I have not yet completed a full FTP/POP3/SMTP regression run, which I would obviously do before proposing it seriously.

    Happy to open it as a PR if that direction is acceptable, or to rework it as something narrower if you would rather not have IMAP reach into the pingpong layer. Equally happy for someone to take the analysis and do it differently.

  2. xhon-pelushi commented on Sep 28, 2026

    @xhon-pelushi
    Contributor

    Follow-up on the regression question I left open above — I have now run it.

    With the prototype applied, against my local build:

    • 800-899 (IMAP/POP3/SMTP): 76/76 OK
    • 100-145 (FTP): 46/46 OK
    • the new multi-literal test plus 1694-1697: pass, and the new one fails without the fix

    I have still not run the complete 1-250 / 600-999 ranges, so I am not claiming a clean full suite — just that the shared-pingpong protocols are not obviously disturbed by draining the literal before the line splitter.

    One correction worth recording, in case anyone tries to reproduce my numbers: I briefly saw all of 100-115 fail and thought the change had broken FTP. That was my own error — I had two runtests instances running at once and they were fighting over the test server ports. On a clean sequential run the same range is 46/46 both with and without the patch. Mentioning it so the earlier scary-looking result is not taken at face value if it shows up anywhere.

    The architectural questions in my previous comment still stand and are the part I would want a maintainer's steer on before opening a PR.

  3. added a commit that references this issue on Oct 22, 2026
    f40c351
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions