From bb336da1693bb7ba8b6b1f818567a1e4901ca3c2 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Mon, 18 Jun 2018 19:13:30 +0300 Subject: [PATCH] imapc: Don't trust mail stream to have correct CRLFs It's not that expensive to just recalculate them. If they're not correct in the stream, it can result in e.g. truncated emails due to wrong virtual_size being used in body_size calculation. The wrong CRLFs can especially happen if plugins modify the stream in istream_opened() hook. --- src/lib-storage/index/imapc/imapc-mail-fetch.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib-storage/index/imapc/imapc-mail-fetch.c b/src/lib-storage/index/imapc/imapc-mail-fetch.c index b543a92a0e..42b32d2f39 100644 --- a/src/lib-storage/index/imapc/imapc-mail-fetch.c +++ b/src/lib-storage/index/imapc/imapc-mail-fetch.c @@ -589,12 +589,15 @@ void imapc_mail_init_stream(struct imapc_mail *mail) smaller than the fetched message header. In this case change the size as well, otherwise reading via istream-mail will fail. */ if (mail->body_fetched || imail->data.physical_size < size) { - if (mail->body_fetched) + if (mail->body_fetched) { imail->data.inexact_total_sizes = FALSE; + /* Don't trust any existing virtual_size. Also don't + set it to size, because there's no guarantees about + the content having proper CRLF newlines, especially + not if istream_opened() has changed the stream. */ + imail->data.virtual_size = (uoff_t)-1; + } imail->data.physical_size = size; - /* we'll assume that the remote server is working properly and - sending CRLF linefeeds */ - imail->data.virtual_size = size; } imail->data.stream_has_only_header = !mail->body_fetched; -- 2.47.3