IMAP custom FETCH breaks when a message contains a line beginning with '+ ' #486

Closed
ehlertjd opened this Issue Oct 13, 2015 · 0 comments

Projects

None yet

3 participants

@ehlertjd
Contributor

When retrieving message contents via CURLOPT_CUSTOM_REQUEST, a message with a line beginning with '+ ' can cause the request to fail with the error "FTP: weird server reply".
Running in verbose, curl prints out "Unexpected continuation response", which is can be found in imap_endofresp.

This appears to be due to custom commands being treated as LIST instead of FETCH in this instance, so curl continues to do extra processing on the body.

Message to reproduce:

To: <test@example.org>
From: <test@example.org>(Example User)
Subject: IMAP Bogus message

+ This line breaks IMAP custom command processing.

Code to reproduce:

#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>

#define USERNAME "username"
#define PASSWORD "password"
#define SERVER_URL "imap://mail.example.org/"

// This is the UID of the message that contains a line beginning with "+ "
#define BAD_MESSAGE_UID "2358"
#define FETCH_COMMAND "UID FETCH " BAD_MESSAGE_UID " (UID BODY.PEEK[1])"

int main(void) {
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();
    if( curl ) {
        curl_easy_setopt(curl, CURLOPT_USERNAME, USERNAME);
        curl_easy_setopt(curl, CURLOPT_PASSWORD, PASSWORD);

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_URL, SERVER_URL "INBOX");

        // Do the request
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, FETCH_COMMAND);
        res = curl_easy_perform(curl);
        if( res != CURLE_OK ) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        }

        curl_easy_cleanup(curl);
    }

    return res;
}

Relevant output:

> A003 SELECT INBOX
< * FLAGS (\Answered \Flagged \Deleted \Seen \Draft $NotJunk $Junk JunkRecorded $Forwarded NotJunk)
< * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft $NotJunk $Junk JunkRecorded $Forwarded NotJunk \*)] Flags permitted.
< * 407 EXISTS
< * 0 RECENT
< * OK [UIDVALIDITY 1418849919] UIDs valid
< * OK [UIDNEXT 2359] Predicted next UID
< * OK [HIGHESTMODSEQ 7503] Highest
< A003 OK [READ-WRITE] Select completed (0.000 secs).
> A004 UID FETCH 2358 (UID BODY.PEEK[1])
< * 407 FETCH (UID 2358 BODY[1] {52}
* 407 FETCH (UID 2358 BODY[1] {52}
< + This line breaks IMAP custom command processing.
* Unexpected continuation response
> A005 LOGOUT
< )
< A004 OK Fetch completed.
< * BYE Logging out
< A005 OK Logout completed.
* Closing connection 0
curl_easy_perform() failed: FTP: weird server reply
@ehlertjd ehlertjd pushed a commit to ehlertjd/curl that referenced this issue Oct 13, 2015
ehlertjd@gmail.com Don't check for continuation if we're executing a CUSTOMREQUEST
References curl/curl#486
04bf853
@ehlertjd ehlertjd pushed a commit to ehlertjd/curl that referenced this issue Oct 13, 2015
ehlertjd@gmail.com Don't check for continuation if we're executing a CUSTOMREQUEST
References curl/curl#486
b246509
@ehlertjd ehlertjd added a commit to ehlertjd/curl that referenced this issue Oct 13, 2015
@ehlertjd ehlertjd Added a test case that demonstrates curl/curl#486 (fails in master, p…
…asses in this branch)
148da49
@bagder bagder added the IMAP label Nov 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment