fix(gateway): harden email adapter against malformed IMAP responses#2794
Open
CharmingGroot wants to merge 1 commit into
Open
fix(gateway): harden email adapter against malformed IMAP responses#2794CharmingGroot wants to merge 1 commit into
CharmingGroot wants to merge 1 commit into
Conversation
- Guard msg_data[0][1] access with try/except so one unexpected IMAP
response structure does not crash the entire polling loop
- Replace split('@')[1] with safe rsplit + fallback for Message-ID
domain extraction when EMAIL_ADDRESS is misconfigured
- Add unit tests covering malformed IMAP responses (None, empty list,
plain bytes) and missing @ in address
9ec62fc to
47abc62
Compare
Collaborator
Collaborator
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.
What changed and why
The email platform adapter (
gateway/platforms/email.py) had two crash-prone patterns:Unguarded IMAP response indexing —
msg_data[0][1]assumes the IMAPFETCHresponse is always a list of tuples. Some IMAP servers return unexpected structures (e.g.None, empty list, plainbytes), causingIndexErrororTypeErrorthat crashes the entire polling loop and stops all email processing.Unsafe
split('@')[1]—Message-IDgeneration usedself._address.split('@')[1]in two places. IfEMAIL_ADDRESSis misconfigured without an@sign, this raisesIndexErrorand prevents sending any replies.Changes
msg_data[0][1]intry/except (IndexError, TypeError)with alogger.warningandcontinue, so one malformed response skips gracefully instead of killing the poll loop.split('@')[1]withrsplit("@", 1)[-1]guarded by an"@" incheck, falling back to"localhost"for the Message-ID domain.How to test
pytest tests/gateway/test_email_robustness.py -v -o "addopts="7 unit tests cover:
Noneelement, empty list, plain bytes (all skipped gracefully)@, empty stringPlatforms tested