Skip to content

fix(imap): encode mailbox names unless UTF8=ACCEPT is enabled - #300

Merged
robert-virkus merged 2 commits into
Enough-Software:mainfrom
brlumen:fix/imap-select-mailbox-by-path
Oct 2, 2026
Merged

robert-virkus merged 2 commits into
Enough-Software:mainfrom
brlumen:fix/imap-select-mailbox-by-path

Conversation

@brlumen

@brlumen brlumen commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #299

Builds on #298 and includes its commit: without it, sending modified UTF-7 on a server that advertises UTF8=ACCEPT would bring the double encoding of #297 to a plain ImapClient on Gmail, for a Mailbox target of appendMessage*(), copy() or move(). Only the last commit belongs to this PR.

Change

  • selectMailboxByPath() encodes the given path and name instead of using them as encodedPath and encodedName directly. The whole path is encoded at once, since modified UTF-7 leaves the ASCII path separator as it is.
  • Mailbox names are sent in UTF-8 only once UTF8=ACCEPT is enabled, checked with serverInfo.isEnabled() instead of supportsUtf8. MailClient enables it when it connects, so it behaves as before.
  • The line break check of _encodeMailboxPath() and _quoteMailboxPath(), now also needed by selectMailboxByPath(), moved to _checkMailboxName().

Code that passes an already encoded Mailbox.encodedPath as a plain targetMailboxPath worked only with a plain ImapClient on a server that advertises UTF8=ACCEPT. It now gets the same result there as on other servers; passing the Mailbox itself works with #298.

Tests

In test/imap/imap_quoting_test.dart:

  • selectMailboxByPath() sends SELECT "Entw&APw-rfe/M&APw-ll" for Entwürfe/Müll and SELECT "R&-D" for R&D. Without the fix: SELECT "Entwürfe/Müll" in UTF-8 and SELECT "R&D".
  • With UTF8=ACCEPT advertised but not enabled, selectMailboxByPath() and appendMessageText(targetMailboxPath: ...) send modified UTF-7. Without the fix both send UTF-8.
  • After ENABLE UTF8=ACCEPT both send UTF-8, as before.

dart format and dart analyze show no new findings, dart test: 598 passed.

APPEND, COPY and MOVE passed Mailbox.encodedPath, which is already
modified UTF-7, through Mailbox.encode() once more on servers without
UTF8=ACCEPT. The '&' that starts every non-ASCII run became '&-', so
the server rejected the command, e.g. with NO [TRYCREATE].

The encodedPath of a target Mailbox is now quoted with
_quoteMailboxPath(), as SELECT and the other mailbox commands do. A
plain targetMailboxPath is still encoded.
selectMailboxByPath() used the decoded path as Mailbox.encodedPath, so
SELECT sent it unencoded: a non-ASCII name went out as raw UTF-8 and a
name such as "R&D" without the "&-" escape. The path is now encoded in
modified UTF-7 as a whole, which leaves the ASCII path separator as it
is.

Mailbox names given as strings were sent in UTF-8 as soon as the server
advertised UTF8=ACCEPT. RFC 6855 requires ENABLE UTF8=ACCEPT first:
until then the server lists modified UTF-7 names, and Gmail answers a
UTF-8 name with "BAD Could not parse command". UTF-8 is now only used
once UTF8=ACCEPT is enabled, as MailClient does when it connects.
@robert-virkus

Copy link
Copy Markdown
Member

Thanks, lgtm!

@robert-virkus
robert-virkus merged commit 2f8a9a4 into Enough-Software:main Oct 2, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

selectMailboxByPath() does not encode the path, and UTF-8 mailbox names are sent before ENABLE UTF8=ACCEPT

2 participants