emersion / go-imap

📥 An IMAP library for clients and servers
MIT License
2.02k stars 288 forks source link

v2: fails to parse imap server response #612

Open quzhi1 opened 2 months ago

quzhi1 commented 2 months ago

I have seen this error when I try to connect to an imap server:

in response-data: imapwire: expected number, got "]"
exit status 1

I suspect there is some error when parsing the server response. This is my test script that can reproduce this error: https://github.com/quzhi1/ImapPlayground/blob/main/benchmark/idle_v2/main.go#L56. I am not able to select an folder because the client is not able to parse the server response.

I configured DebugWriter: os.Stdout. This is the full response:

✗ go run benchmark/idle_v2/main.go
T1 LOGIN "<redacted>" "<redacted>"
* OK imap server ready for requests
*T2 CAPABILITY
 CAPABILITY IMAP4rev1 MOVE IDLE UTF8=ACCEPT ID ENABLE UIDPLUS
T1 OK LOGIN successfully logged in
* CAPABILITY IMAP4rev1 MOVE IDLE UTF8=ACCEPT ID ENABLE UIDPLUS
T2 OK CAPAT3 CAPABILITY
T4 EXAMINE INBOX
BILITY Completed
* CAPABILITY IMAP4rev1 MOVE IDLE UTF8=ACCEPT ID ENABLE UIDPLUS
T3 OK CAPABILITY Completed
* FLAGS (\DELETED \RECENT \DRAFT \FLAGGED \ANSWERED \SEEN)
* OK [PERMANENTFLAGS (\DELETED \RECENT \DRAFT \FLAGGED \ANSWERED \SEEN)] flags permitted
* OK [UIDVALIDITY 1713632002544]2024/04/22 10:51:57 failed to select Archive: in response-data: imapwire: expected number, got "]"

The email hosting service I am using is https://www.dynadot.com/. I tried to directly use open_ssl to connect to their IMAP server, and their response seems to comply with the IMAP standard. This is their server's response when I select a folder:

a SELECT INBOX
* FLAGS (\DELETED \RECENT \DRAFT \FLAGGED \ANSWERED \SEEN)
* OK [PERMANENTFLAGS (\DELETED \RECENT \DRAFT \FLAGGED \ANSWERED \SEEN)] flags permitted
* OK [UIDVALIDITY 1713632002544] UIDs valid
* 1 EXISTS
* 0 RECENT
* OK [UIDNEXT 2] Predicted next UID
a OK [READ-WRITE] INBOX selected

Is there something wrong this line? https://github.com/emersion/go-imap/blob/v2/imapclient/client.go#L831

Thank you for taking a look 🙏

emersion commented 2 months ago

Hm, it seems like they are sending 1713632002544 as UIDVALIDITY, which exceeds the maximum value that a uint32 can store (4294967295).

Our error message is far from ideal here, but not sure how to handle these cases…

quzhi1 commented 2 months ago

Thank you for taking a look! This makes sense.