Skip to content

fix(gateway): harden email adapter against malformed IMAP responses#2794

Open
CharmingGroot wants to merge 1 commit into
NousResearch:mainfrom
CharmingGroot:fix/email-imap-response-robustness
Open

fix(gateway): harden email adapter against malformed IMAP responses#2794
CharmingGroot wants to merge 1 commit into
NousResearch:mainfrom
CharmingGroot:fix/email-imap-response-robustness

Conversation

@CharmingGroot

Copy link
Copy Markdown

What changed and why

The email platform adapter (gateway/platforms/email.py) had two crash-prone patterns:

  1. Unguarded IMAP response indexingmsg_data[0][1] assumes the IMAP FETCH response is always a list of tuples. Some IMAP servers return unexpected structures (e.g. None, empty list, plain bytes), causing IndexError or TypeError that crashes the entire polling loop and stops all email processing.

  2. Unsafe split('@')[1]Message-ID generation used self._address.split('@')[1] in two places. If EMAIL_ADDRESS is misconfigured without an @ sign, this raises IndexError and prevents sending any replies.

Changes

  • Wrap msg_data[0][1] in try/except (IndexError, TypeError) with a logger.warning and continue, so one malformed response skips gracefully instead of killing the poll loop.
  • Replace split('@')[1] with rsplit("@", 1)[-1] guarded by an "@" in check, 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:

  • Normal IMAP response (passes through correctly)
  • Malformed responses: None element, empty list, plain bytes (all skipped gracefully)
  • Domain extraction: normal address, missing @, empty string

Platforms tested

  • macOS (darwin aarch64, Python 3.11)

- 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
@CharmingGroot CharmingGroot force-pushed the fix/email-imap-response-robustness branch from 9ec62fc to 47abc62 Compare March 24, 2026 13:58
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/gateway Gateway runner, session dispatch, delivery platform/email Email (IMAP/SMTP) adapter labels May 2, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12498 and #18146 — same fix: guard IMAP FETCH response against non-standard payloads.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #12498 and #18146

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

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/email Email (IMAP/SMTP) adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants