Repository navigation
gh-49680: Don't rewrite CR/LF in imaplib APPEND literals - #151557
Closed
harjothkhara wants to merge 2 commits into
Closed
harjothkhara wants to merge 2 commits into
harjothkhara wants to merge 2 commits into
Conversation
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.
IMAP4.append()rewrites every bare CR or LF in the message to CRLF (viaMapCRLF.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\nunder 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 realEmailMessage.Note for reviewers: the module-level
MapCRLFregex 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.