Skip to content

gh-49680: Don't rewrite CR/LF in imaplib APPEND literals - #151557

Closed
harjothkhara wants to merge 2 commits into
python:mainfrom
harjothkhara:codex/49680-imaplib-append-literal
Closed

harjothkhara wants to merge 2 commits into
python:mainfrom
harjothkhara:codex/49680-imaplib-append-literal

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

IMAP4.append() rewrites every bare CR or LF in the message to CRLF (via MapCRLF.sub) before sending it. But an IMAP literal is a counted, opaque octet sequence ({N} + exactly N bytes), so the rewrite corrupts payloads that legitimately contain bare CR/LF — e.g. encoded headers or an already-serialized message — and desyncs the byte count from what the caller meant to send.

This patch makes append() send the message verbatim; _command() still owns the protocol framing (the {N} literal, the continuation handshake, the trailing CRLF).

Behavior change: callers that relied on the old normalization — notably EmailMessage.as_bytes(), which emits bare \n under the default policy — would now send those bytes unchanged and should ensure CRLF themselves (e.g. email.policy.SMTP). Whether treating the literal as opaque is the right fix, or another approach is preferred, is a maintainer call — see my comment below. The new tests read the exact {N} literal octets and assert byte-exact round-trips for a payload mixing \n, \r, and \r\n, and for a real EmailMessage.

Note for reviewers: the module-level MapCRLF regex is now unused. It's not in __all__ and isn't documented; I left it in place to keep the diff focused, but happy to remove it (with its own NEWS line).

Disclosure: I used AI assistance writing this; I've reviewed the change and can explain it.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants