fix(email): decode IMAP folder names from modified UTF-7 (#3514)#3544
Open
maxmilian wants to merge 2 commits into
Open
fix(email): decode IMAP folder names from modified UTF-7 (#3514)#3544maxmilian wants to merge 2 commits into
maxmilian wants to merge 2 commits into
Conversation
maxmilian
marked this pull request as ready for review
June 9, 2026 00:53
This was referenced Jun 11, 2026
Closed
…v#3514) IMAP mailbox names arrive in modified UTF-7 (RFC 3501), so a Cyrillic folder showed up as its raw `&BB4EQgQ,...-` wire form in the folder list. imaplib does no decoding and Python ships no imap4-utf-7 codec. Add a stdlib-only `src/imap_utf7` codec and establish one invariant: folder names are kept decoded everywhere in the Python layer, and `_q()` — the single mailbox-quoting chokepoint for SELECT/COPY/MOVE/APPEND — re-encodes to modified UTF-7 on the way to the server. Decoding happens where names are read from the LIST response (`_folder_name_from_list_line`, the /folders endpoint, and the \Sent/\Drafts/\Junk detectors), so non-ASCII folders both display correctly and stay selectable. ASCII names are unchanged by encode/decode (verified by the existing quoting tests). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Use base64 validate=True so a terminated run of non-Base64 characters raises and falls back to the verbatim slice, instead of b64decode silently stripping it to empty — matching the documented 'left verbatim' behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
maxmilian
force-pushed
the
fix-3514-imap-utf7
branch
from
July 15, 2026 14:20
8a5d1c6 to
880afe5
Compare
Contributor
Author
|
Re-verified on a rebase onto today's
Happy to adjust scope or squash if the maintainers prefer a different shape for this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IMAP mailbox names are encoded in modified UTF-7 (RFC 3501), but nothing decoded them, so folders with non-Latin names rendered as their raw wire form (e.g.
&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-instead of Cyrillic).imaplibdoes no decoding and Python ships noimap4-utf-7codec.This adds a stdlib-only
src/imap_utf7codec and establishes one invariant: folder names are kept decoded everywhere in the Python layer, and_q()— the single mailbox-quoting chokepoint forSELECT/COPY/MOVE/APPEND— re-encodes to modified UTF-7 on the way to the server. Decoding is applied where names are read from theLISTresponse (_folder_name_from_list_line, the/email/foldersendpoint, and the\Sent/\Drafts/\Junkdetectors), so non-ASCII folders both display correctly and stay selectable (no display-only regression). ASCII names are unchanged by encode/decode.Files: new
src/imap_utf7.py;routes/email_routes.py,routes/email_helpers.py,mcp_servers/email_server.py(decode on read, encode in_q).Target branch
dev, notmain.Linked Issue
Closes #3514
Type of Change
Checklist
dev, notmain.How to Test
Backend-only, no UI changes. Run the codec + IMAP quoting tests:
Or check the round-trip directly:
To verify against a real account: add an IMAP account that has a folder with a non-Latin name and open the folder list — it now shows the decoded Unicode name, and selecting/reading that folder still works (the name is re-encoded before it hits the server).
Testing
python -m pytest tests/test_imap_utf7.py tests/test_imap_mailbox_quoting.py→ 17 passed (9 new codec tests + existing quoting tests, which still pass since ASCII names are unchanged).python -m py_compileon all four changed files → clean.