The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(email): authorize before fetching MIME bodies
FroRaut81706c8b30b1
diff --git a/plugins/platforms/email/adapter.py b/plugins/platforms/email/adapter.pyindex 5a214cc87c4..15fc25f7036 100644--- a/plugins/platforms/email/adapter.py+++ b/plugins/platforms/email/adapter.py@@ -13,13 +13,15 @@ import socketimport sslimport uuidfrom email.header import decode_header+from email.message import Messagefrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom email.mime.base import MIMEBase+from email.parser import BytesHeaderParserfrom email.utils import formatdate, parseaddrfrom email import encodersfrom pathlib import Path-from typing import Any, Dict, List, Optional, Tuple+from typing import Any, Callable, Dict, List, Optional, Tuplefrom agent.i18n import tfrom gateway.platforms.base import (@@ -60,6 +62,13 @@ _COMMENT_RE = re.compile(r"\([^()]*\)")# Longest From: value we parse. parseaddr is pure Python and superlinear on hostile input (~1s at 100KB, GIL held);# a real mailbox plus display name stays far below this (RFC 5322 caps a line at 998 chars)._MAX_FROM_LEN = 2048+# Only these headers are needed to reject a sender. Request one byte past the+# limit so a truncated preflight fails closed without downloading the MIME body.+_MAX_PREAUTH_HEADER_BYTES = 64 * 1024+_PREAUTH_FETCH = (+ "(BODY.PEEK[HEADER.FIELDS (FROM AUTHENTICATION-RESULTS AUTO-SUBMITTED PRECEDENCE "+ f"X-AUTO-RESPONSE-SUPPRESS LIST-UNSUBSCRIBE)]<0.{_MAX_PREAUTH_HEADER_BYTES + 1}>)"+)# Authentication-Results clause head (``dmarc=pass``), matched only at the start of a clause._AUTH_METHOD_RE = re.compile(r"\s*(dmarc|dkim|spf)\s*=\s*([a-z]+)", re.IGNORECASE)_NO_AUTH_RESULTS_REASON = "no Authentication-Results header"@@ -343,8 +352,8 @@ def _domains_aligned(a: str, b: str) -> bool:def _verify_sender_authentication(msg: email_lib.message.Message, from_addr: str, *, authserv_id: str = "") -> Tuple[bool, str]:"""Verify the ``From:`` domain is authenticated; returns ``(authenticated, reason)``.``From:`` is attacker-controlled (GHSA-rxqh-5572-8m77); the only trustworthy signal is the- ``Authentication-Results`` header stamped by the *receiving* server. It prepends, so the FIRST- instance is trusted and an injected copy sorts below it; pinned to *authserv_id* when given.+ ``Authentication-Results`` header stamped by the *receiving* server. It prepends, so only the+ FIRST instance is authoritative; when *authserv_id* is set, that instance must match it exactly.True on DMARC pass, aligned SPF pass, or aligned DKIM (``header.d``) pass. No header → fail-closed(opt out via ``EmailAdapter._require_authenticated_sender``)."""from_domain = _domain_of(from_addr)@@ -352,15 +361,15 @@ def _verify_sender_authentication(msg: email_lib.message.Message, from_addr: strreturn False, "missing From domain"if not (headers := msg.get_all("Authentication-Results")):return False, _NO_AUTH_RESULTS_REASON- values = (" ".join(str(raw).split()) for raw in headers) # authserv-id precedes the first ';'- trusted = next((v for v in values if not authserv_id or (serv := v.split(";", 1)[0].strip().lower()) == authserv_id.lower()- or _domains_aligned(serv, authserv_id)), None)- if trusted is None:+ trusted = " ".join(str(headers[0]).split())+ # _ar_clauses removes RFC 8601 CFWS comments and ignores semicolons inside them, so supported+ # receiver variants remain valid without allowing a lower field or a related domain to satisfy the pin.+ if (clauses := _ar_clauses(trusted)) is None:+ return False, "unbalanced quote or comment in Authentication-Results"+ if authserv_id and clauses[0].strip().lower() != authserv_id.strip().lower():return False, _UNTRUSTED_AUTHSERV_REASON# Each verdict comes from the head of its own clause (split outside quotes/comments) and its domains only from that# clause: a quoted local part or comment can otherwise smuggle ``spf=pass``/``header.d=`` (GHSA-rxqh-5572-8m77).- if (clauses := _ar_clauses(trusted)) is None:- return False, "unbalanced quote or comment in Authentication-Results"results: Dict[str, List[Tuple[str, List[Tuple[str, str]]]]] = {"dmarc": [], "spf": [], "dkim": []}for clause in clauses:if m := _AUTH_METHOD_RE.match(clause):@@ -607,7 +616,17 @@ class EmailAdapter(BasePlatformAdapter):async def _check_inbox(self) -> None:"""Check INBOX for unseen messages and dispatch them."""- messages = await asyncio.get_running_loop().run_in_executor(None, self._fetch_new_messages)+ loop = asyncio.get_running_loop()++ async def authorize_on_loop(msg_data: Dict[str, Any]) -> bool:+ return self._sender_accepted(msg_data["sender_addr"], msg_data)++ def gate(candidate: Dict[str, Any]) -> bool:+ # Authorization reads profile-scoped policy and pairing state on the adapter's event loop;+ # the blocking IMAP worker waits for that verdict before asking the server for RFC822.+ return asyncio.run_coroutine_threadsafe(authorize_on_loop(candidate), loop).result()++ messages = await asyncio.to_thread(self._fetch_new_messages, gate)# Dispatch partial results BEFORE escalating a failure — a mid-batch exception returns what was fetched (already marked seen).for msg_data in messages:await self._dispatch_message(msg_data)@@ -620,8 +639,18 @@ class EmailAdapter(BasePlatformAdapter):self._set_fatal_error("email_imap_fetch_failed", self._last_fetch_error or "IMAP fetch failed", retryable=True)await self._notify_fatal_error()- def _fetch_new_messages(self) -> List[Dict[str, Any]]:- """Fetch new (unseen) messages from IMAP. Runs in executor thread."""+ def _mark_uid_consumed(self, imap: "imaplib.IMAP4", uid: Any) -> None:+ """Remember a rejected UID and mark it seen without fetching its MIME body."""+ self._seen_uids.add(uid)+ self._trim_seen_uids()+ status, _ = imap.uid("store", uid, "+FLAGS.SILENT", r"(\Seen)")+ if status != "OK":+ logger.warning("[Email] Could not mark rejected UID %s seen", uid)++ def _fetch_new_messages(+ self, preauthorize: Optional[Callable[[Dict[str, Any]], bool]] = None,+ ) -> List[Dict[str, Any]]:+ """Fetch unseen messages. When *preauthorize* is set, bounded headers are gated before RFC822."""results = []try:with self._inbox() as imap:@@ -629,6 +658,34 @@ class EmailAdapter(BasePlatformAdapter):for uid in (data[0].split() if status == "OK" and data and data[0] else []):if uid in self._seen_uids:continue+ if preauthorize is not None:+ header_status, header_data = imap.uid("fetch", uid, _PREAUTH_FETCH)+ if header_status != "OK":+ continue+ try:+ raw_headers = header_data[0][1]+ except (IndexError, TypeError):+ logger.warning("[Email] Unexpected IMAP header response for UID %s, skipping", uid)+ self._mark_uid_consumed(imap, uid)+ continue+ if not isinstance(raw_headers, (bytes, bytearray)):+ logger.warning("[Email] Non-bytes IMAP header payload for UID %s, skipping", uid)+ self._mark_uid_consumed(imap, uid)+ continue+ if len(raw_headers) > _MAX_PREAUTH_HEADER_BYTES:+ logger.warning("[Email] Pre-authorization headers exceed %d bytes for UID %s, skipping",+ _MAX_PREAUTH_HEADER_BYTES, uid)+ self._mark_uid_consumed(imap, uid)+ continue+ try:+ candidate = self._parse_fetched_headers(uid, raw_headers)+ accepted = candidate is not None and preauthorize(candidate)+ except Exception as auth_exc:+ logger.error("[Email] Failed to authorize message UID %s, skipping: %s", uid, auth_exc)+ accepted = False+ if not accepted:+ self._mark_uid_consumed(imap, uid)+ continuestatus, msg_data = imap.uid("fetch", uid, "(RFC822)")if status != "OK":continue # transient per-UID refusal: leave unseen so the next poll retries@@ -664,9 +721,8 @@ class EmailAdapter(BasePlatformAdapter):self._seen_uids_snapshot[self._address] = set(self._seen_uids)return results- def _parse_fetched_message(self, uid: bytes, raw_email: "bytes | bytearray") -> Optional[Dict[str, Any]]:- """Parse one RFC822 payload into a dispatchable dict; ``None`` for automated senders. Raises on pathological input (caller logs + continues)."""- msg = email_lib.message_from_bytes(raw_email)+ def _message_metadata(self, uid: bytes, msg: Message) -> Optional[Dict[str, Any]]:+ """Parse sender-facing headers and authentication without touching the MIME body."""if not (sender_addr := _extract_email_address(msg.get("From", ""))): # never dispatch an empty identitylogger.debug("[Email] Dropping message with no parseable From address: %r", msg.get("From", ""))return None@@ -677,14 +733,24 @@ class EmailAdapter(BasePlatformAdapter):if _is_automated_sender(sender_addr, dict(msg.items())):logger.debug("[Email] Skipping automated sender: %s", sender_addr)return None- # Verify From: while the trusted Authentication-Results header is in scope; the verdict is consumed at dispatch (GHSA-rxqh-5572-8m77).sender_authenticated, auth_reason = _verify_sender_authentication(msg, sender_addr, authserv_id=self._authserv_id)return {"uid": uid, "sender_addr": sender_addr, "sender_name": sender_name, "subject": subject,"message_id": msg.get("Message-ID", ""), "in_reply_to": msg.get("In-Reply-To", ""),- "body": _extract_text_body(msg),- "attachments": _extract_attachments(msg, skip_attachments=self._skip_attachments),"date": msg.get("Date", ""), "sender_authenticated": sender_authenticated, "auth_reason": auth_reason}+ def _parse_fetched_headers(self, uid: bytes, raw_headers: "bytes | bytearray") -> Optional[Dict[str, Any]]:+ """Parse the bounded IMAP header preflight without constructing a MIME tree."""+ return self._message_metadata(uid, BytesHeaderParser().parsebytes(bytes(raw_headers)))++ def _parse_fetched_message(self, uid: bytes, raw_email: "bytes | bytearray") -> Optional[Dict[str, Any]]:+ """Parse an authorized RFC822 payload into a dispatchable dict."""+ msg = email_lib.message_from_bytes(raw_email)+ if (metadata := self._message_metadata(uid, msg)) is None:+ return None+ metadata.update(body=_extract_text_body(msg),+ attachments=_extract_attachments(msg, skip_attachments=self._skip_attachments))+ return metadata+@staticmethoddef _allow_all_senders() -> bool:"""True when the operator opted into any sender (EMAIL_ or GATEWAY_ALLOW_ALL_USERS).diff --git a/tests/gateway/test_email.py b/tests/gateway/test_email.pyindex e07b1bc6499..01217af6864 100644--- a/tests/gateway/test_email.py+++ b/tests/gateway/test_email.py@@ -673,6 +673,10 @@ class TestPollLoop(unittest.TestCase):}):from plugins.platforms.email.adapter import EmailAdapteradapter = EmailAdapter(PlatformConfig(enabled=True))+ # _check_inbox gates every UID on _sender_accepted before the RFC822 fetch; authorize the+ # unauthenticated test senders so these tests exercise the fetch/dispatch loop itself.+ adapter._is_sender_authorized = lambda *a, **k: True+ adapter._require_authenticated_sender = Falsereturn adapterdef test_check_inbox_dispatches_messages(self):@@ -708,6 +712,84 @@ class TestPollLoop(unittest.TestCase):self.assertEqual(len(dispatched), 1)self.assertEqual(dispatched[0]["subject"], "Inbox Test")+ def test_rejected_sender_is_not_full_fetched_before_authorization(self):+ """A header-only, bounded preflight rejects a forged lower auth result+ before IMAP returns the MIME body or an attachment is decoded."""+ import asyncio+ from plugins.platforms.email.adapter import _MAX_PREAUTH_HEADER_BYTES++ adapter = self._make_adapter()+ adapter._require_authenticated_sender = True+ adapter._authserv_id = "mx.trusted.test"+ dispatched, fetch_specs = [], []++ async def capture(msg_data):+ dispatched.append(msg_data)++ adapter._dispatch_message = capture+ forged_headers = (+ b"From: Owner <[email protected]>\r\n"+ b"Subject: forged lower result\r\n"+ b"Message-ID: <[email protected]>\r\n"+ b"Authentication-Results: edge.receiver.test; dmarc=fail header.from=example.com\r\n"+ b"Authentication-Results: mx.trusted.test; dmarc=pass header.from=example.com\r\n"+ b"Content-Type: multipart/mixed; boundary=x\r\n\r\n"+ )+ trusted_headers = (+ b"From: Owner <[email protected]>\r\n"+ b"Subject: trusted result\r\n"+ b"Message-ID: <[email protected]>\r\n"+ b"Authentication-Results: mx.trusted.test; dmarc=pass header.from=example.com\r\n"+ b"Content-Type: text/plain\r\n\r\n"+ )+ oversized_headers = (+ b"From: Owner <[email protected]>\r\nAuthentication-Results: mx.trusted.test; "+ + b"x" * _MAX_PREAUTH_HEADER_BYTES+ )+ headers = {b"9": forged_headers, b"10": trusted_headers, b"11": oversized_headers}+ full_messages = {+ b"9": forged_headers + (+ b"--x\r\nContent-Disposition: attachment; filename=payload.bin\r\n\r\n"+ + b"x" * 200_000 + b"\r\n--x--\r\n"+ ),+ b"10": trusted_headers + b"authorized body",+ b"11": oversized_headers + b"unauthorized body",+ }+ mock_imap = MagicMock()++ def uid_handler(command, *args):+ if command == "search":+ return ("OK", [b"9 10 11"])+ if command == "fetch":+ uid, spec = args+ fetch_specs.append((uid, spec))+ payload = headers[uid] if "BODY.PEEK[HEADER.FIELDS" in spec else full_messages[uid]+ return ("OK", [(uid, payload)])+ if command == "store":+ return ("OK", [b""])+ return ("NO", [])++ mock_imap.uid.side_effect = uid_handler+ with patch.dict(os.environ, {+ "EMAIL_ALLOWED_USERS": "[email protected]",+ "EMAIL_ALLOW_ALL_USERS": "",+ "GATEWAY_ALLOWED_USERS": "",+ "GATEWAY_ALLOW_ALL_USERS": "",+ }, clear=False), patch("imaplib.IMAP4_SSL", return_value=mock_imap), patch(+ "plugins.platforms.email.adapter._extract_attachments", return_value=[],+ ) as extract_attachments:+ asyncio.run(adapter._check_inbox())++ self.assertEqual([message["message_id"] for message in dispatched], ["<[email protected]>"])+ self.assertNotIn((b"9", "(RFC822)"), fetch_specs)+ self.assertNotIn((b"11", "(RFC822)"), fetch_specs)+ self.assertIn((b"10", "(RFC822)"), fetch_specs)+ header_specs = [spec for _, spec in fetch_specs if "BODY.PEEK[HEADER.FIELDS" in spec]+ self.assertEqual(len(header_specs), 3)+ self.assertTrue(all(f"<0.{_MAX_PREAUTH_HEADER_BYTES + 1}>" in spec for spec in header_specs))+ extract_attachments.assert_called_once()+ self.assertTrue({b"9", b"10", b"11"}.issubset(adapter._seen_uids))+def test_check_inbox_notifies_fatal_error_on_fetch_failure(self):"""A failed IMAP check must surface through the fatal-error hook sothe gateway's reconnect/backoff machinery learns email is unhealthy@@ -763,7 +845,7 @@ class TestPollLoop(unittest.TestCase):return ("OK", [b"1 2"])if command == "fetch":fetches.append(args)- if len(fetches) == 1:+ if args[0] == b"1":return ("OK", [(b"1", raw_email.as_bytes())])raise OSError("connection dropped mid-batch")return ("NO", [])@@ -1284,21 +1366,30 @@ class TestSenderAuthentication(unittest.TestCase):self.assertFalse(ok, reason)- def test_injected_header_below_trusted_does_not_authenticate(self):- """An attacker-injected Authentication-Results sorts BELOW the receiving- server's. With authserv-id pinning, only the trusted (first) header is- consulted, so a forged 'dmarc=pass' lower in the stack is ignored."""+ def test_only_topmost_exact_authserv_id_is_trusted(self):+ """Never search below the authoritative field or relax an authserv-id pin."""+ forged_lower = "mx.ourserver.com; dmarc=pass header.from=example.com"+ for topmost in (+ "mx.ourserver.com; dmarc=fail header.from=example.com",+ "edge.receiver.test; dmarc=fail header.from=example.com",+ "child.mx.ourserver.com; dmarc=fail header.from=example.com",+ "(mx.ourserver.com) edge.receiver.test; dmarc=fail header.from=example.com",+ ):+ with self.subTest(topmost=topmost):+ ok, reason = self._verify(+ "[email protected]", [topmost, forged_lower],+ authserv_id="mx.ourserver.com",+ )+ self.assertFalse(ok, reason)++ # Matching remains case-insensitive and accepts RFC 8601 CFWS comments,+ # including a semicolon inside a nested comment.ok, reason = self._verify(- [- # Trusted: stamped by our server, real verdict = fail- "mx.ourserver.com; dmarc=fail header.from=example.com",- # Forged by attacker, claims pass- "mx.ourserver.com; dmarc=pass header.from=example.com",- ],+ ["MX.OURSERVER.COM (receiver (a;b)); dmarc=pass header.from=example.com"],authserv_id="mx.ourserver.com",)- self.assertFalse(ok, reason)+ self.assertTrue(ok, reason)def test_oversized_cron_output_is_delivered_as_one_whole_email():