C12.1: ely-mail-engine connects over IMAP, POP3 and SMTP

TLS or STARTTLS with the system trust store; password and XOAUTH2
logins keep the server's words on refusal. IMAP and POP3 are written
in the engine (imap-proto parses IMAP replies): async-imap dropped
NO/BAD on LIST and FETCH and lost BYE text, async-pop kept dot
stuffing and logged passwords. SMTP runs its own transaction with
RFC 5321 mailbox checks, full dot transparency, SMTPUTF8 and RSET.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-19 16:53:53 -04:00
1 parent d7de5619a7
commit 9f47b94714
21 files changed
+2848 -20

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
FAIL
1. `crates/ely-mail-engine/src/smtp.rs:70` — [P1] SMTP envelope addresses can inject additional commands. `from` and every `to` string reach mail-send's command formatting without validation. Passing one recipient, `bo@example.com>\r\nRCPT TO:<hidden@example.com`, transmits two separate `RCPT TO` commands. A scripted TLS server accepted both recipients and DATA, and `send` returned `Ok(())`, silently delivering to an extra envelope recipient. Reject invalid envelope mailbox syntax, including CR/LF, for the sender and all recipients before transmitting the transaction; add regression coverage for both inputs. Reproduced on Windows and Linux: `smtp_envelope_cannot_inject_an_additional_recipient`, `.work/c12.1-r1-probe.log` and `.work/c12.1-r1-linux-probe.log`.
2. `crates/ely-mail-engine/Cargo.toml:10` — [P1] The selected async-imap and async-pop dependencies emit credentials and complete fetched messages through the shared `log` facade at trace level. Neither the dependency configuration nor the engine suppresses or redacts those records. With trace enabled, successful TLS login and fetch recorded the literal IMAP password, POP3 `PASS` password, and both message bodies. The project's logger enables these targets with `ELY_LOG=trace`, violating agents.md's prohibition on credentials and content in logs. Remove or redact sensitive protocol tracing, or reliably exclude those records from logging, and test actual dependency logs rather than only the engine's own messages. Reproduced on both platforms: `trace_logs_exclude_password_token_and_message`, the two probe logs above.
3. `crates/ely-mail-engine/src/smtp.rs:62` — [P2] SMTP XOAUTH2 does not complete an authentication error challenge correctly. After the initial token, a server replying `334 <base64 JSON error>` receives the token again from mail-send's generic authentication loop. The required response is an empty line, after which the server supplies its final `535` refusal ([Gmail protocol](https://developers.google.com/workspace/gmail/imap/xoauth2-protocol#error_response_3)). The scripted server expecting that exchange instead observed the repeated token; `connect` returned a connection error and lost the final refusal. Implement the stateful XOAUTH2 error exchange for SMTP and preserve the final server reply. Existing SMTP tests cover password refusal and token success only. Reproduced on both platforms: `smtp_refused_oauth_answers_the_error_challenge_empty`, the two probe logs above.
4. `crates/ely-mail-engine/src/pop.rs:106` — [P2] POP3 XOAUTH2 stops handling challenges immediately after sending the token. If the server then returns a base64 error challenge, async-pop receives a challenge where it expects final success and returns `Protocol("Did not received the expected auith response")`; the engine never sends the empty response and never reads the server's final `-ERR` explanation. This loses the required credential refusal and server wording for the documented [XOAUTH2 error exchange](https://developers.google.com/workspace/gmail/imap/xoauth2-protocol#error_response). Complete the challenge/final-response exchange, preserving both successful authentication and the final rejection; add a token-refusal test. Reproduced on both platforms: `pop_refused_oauth_answers_the_error_challenge_empty`, the two probe logs above.
5. `crates/ely-mail-engine/src/pop.rs:32` — [P2] POP3 login can report success after swallowing a connection failure. Both async-pop authentication paths call `update_capabilities`, which discards every CAPA error. A TLS server that answers USER/PASS successfully and then closes after receiving the post-login CAPA causes `Pop::connect` to return `Ok(Pop)` and emit the successful-login log despite the failed operation and unusable connection. Surface transport and malformed-response errors from capability discovery; distinguish an explicitly handled unsupported CAPA reply from other failures. Cover the same behavior for password and OAuth authentication. Reproduced on both platforms: `pop_connection_loss_during_capabilities_is_an_error`, the two probe logs above.
6. `crates/ely-mail-engine/src/imap.rs:101` — [P2] The IMAP greeting is discarded without checking its response type or status. When a TLS server sends `* BYE [UNAVAILABLE] Maintenance in progress` and closes, the client treats the greeting as successful and attempts authentication. The returned error is an OS connection failure (Windows 10053 / Linux broken pipe), with the already-received maintenance explanation lost. This breaks C12.1's requirement to retain server wording on failure. Interpret the greeting, return BYE's reason immediately, and avoid authentication after a refusing greeting; add a greeting-refusal regression. Reproduced on both platforms: `imap_greeting_refusal_keeps_the_server_words`, the two probe logs above.
+9
View File
@@ -0,0 +1,9 @@
FAIL
1. `crates/ely-mail-engine/src/imap.rs:60` — [P2] IMAP command refusals become successful empty results. async-imap's LIST and FETCH streams discard the tagged completion without checking its status. A server replying to LIST with `NO [UNAVAILABLE] Mailbox listing temporarily unavailable` makes `mailboxes()` return `Ok([])`; replying to UID FETCH with `NO [UNAVAILABLE] Message store temporarily unavailable` makes `fetch()` return `Ok(None)`, falsely reporting a missing UID. Both lose the server's explanation and swallow an operation failure. Ensure the tagged completion is checked for NO/BAD before returning collected results, and cover refusals for both operations. Reproduced on Windows and Linux: `imap_list_refusal_is_an_error`, `imap_fetch_refusal_is_an_error`; `.work/c12.1-r2-probe.log`, `.work/c12.1-r2-linux-probe.log`.
2. `crates/ely-mail-engine/src/imap.rs:44` — [P2] BYE explanations are still discarded during authentication. The round 1 greeting scenario is fixed, but after a normal greeting, a server answering LOGIN with `* BYE [UNAVAILABLE] Too many simultaneous connections` and closing produces only `cannot connect: peer closed connection without sending TLS close_notify`. async-imap ignores the untagged BYE while waiting for a tagged completion. This still violates C12.1's requirement to preserve server wording on failure. Propagate BYE and its explanation through authentication and command response handling, rather than handling it only as the initial greeting; add regression coverage after authentication starts. Reproduced on both platforms: `imap_login_bye_keeps_server_words`, the two probe logs above.
3. `crates/ely-mail-engine/src/smtp.rs:131` — [P2] Envelope validation rejects valid quoted mailbox addresses. For example, sending to `"bo smith"@example.com` returns `Error::Invalid` before any transaction, so that recipient cannot receive mail through this engine. Spaces inside a quoted local part are permitted by [RFC 5321 §4.1.2](https://www.rfc-editor.org/rfc/rfc5321.html#section-4.1.2). Validate SMTP mailbox grammar with quoting awareness while retaining command-injection protection, and test valid quoted senders and recipients alongside the injection cases. Reproduced on both platforms: `smtp_accepts_a_quoted_local_part`, the two probe logs above.
4. `crates/ely-mail-engine/src/smtp.rs:82` — [P2] SMTP DATA omits dot stuffing on the first line. A valid message beginning with the optional header `.X-Trace: yes` is transmitted with that single initial period, whereas subsequent dot-prefixed lines are escaped. A conforming receiver removes the period and changes the header name: [RFC 5321 §4.5.2](https://www.rfc-editor.org/rfc/rfc5321.html#section-4.5.2) requires escaping each dot-prefixed line, and [RFC 5322 §3.6.8](https://www.rfc-editor.org/rfc/rfc5322.html#section-3.6.8) permits this header name. The selected mail-send writer only escapes dots following CR/LF. Correct the transparency handling at the first byte as well, and test the exact DATA bytes of a message starting with a period. Reproduced on both platforms: `smtp_stuffs_a_dot_in_the_first_data_line`, the two probe logs above.
+5
View File
@@ -0,0 +1,5 @@
FAIL
1. `crates/ely-mail-engine/src/imap.rs:68` — [P2] Quoted LIST mailbox names retain their wire escapes. imap-proto's quoted-string parser returns the escaped interior, so a server listing the folder `a"b` as `"a\"b"` makes `mailboxes()` return a name containing an extra backslash. Passing that returned name to `fetch()` escapes it again in EXAMINE and addresses a different folder, preventing retrieval from the listed folder. Backslashes are similarly doubled. Decode quoted-string escapes at the response boundary, preserving literal mailbox contents unchanged, and cover LIST-to-fetch round trips for both representations ([RFC 3501 string syntax](https://www.rfc-editor.org/rfc/rfc3501.html#section-9)). Reproduced on Windows and Linux: `imap_list_decodes_quoted_mailbox_names` and `imap_fetch_from_a_listed_quoted_mailbox_uses_its_name`; literal-name control passes. Evidence: `.work/c12.1-r3-windows-probe.log`, `.work/c12.1-r3-linux-probe.log`.
2. `crates/ely-mail-engine/src/smtp.rs:85` — [P2] SMTPUTF8 is checked only for envelope addresses, ignoring internationalized message headers. With ASCII sender and recipients but `Subject: 您好`, the engine omits SMTPUTF8 from MAIL even when advertised. When the server does not advertise it, the engine still transmits the UTF-8 header and can return `Ok(())`. This violates [RFC 6531 §3.2 and §3.4](https://www.rfc-editor.org/rfc/rfc6531.html#section-3.2) and can make otherwise supported internationalized messages fail submission or subsequent relay. Include the message's SMTPUTF8 requirement in the send contract and capability check, reject unsupported messages before MAIL, and add the parameter when required; test ASCII envelopes with UTF-8 headers against both capability responses. Reproduced on Windows and Linux: `smtp_utf8_header_requests_smtputf8_with_ascii_envelope` and `smtp_utf8_header_refused_without_smtputf8`; the two probe logs above.