The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(email): warn about a missing authserv_id once per account at connect
kshitijk4poorde6013bceffa
diff --git a/plugins/platforms/email/adapter.py b/plugins/platforms/email/adapter.pyindex 475b737de11..c700082ad90 100644--- a/plugins/platforms/email/adapter.py+++ b/plugins/platforms/email/adapter.py@@ -79,9 +79,11 @@ _DROP_HINTS = {_NO_AUTH_RESULTS_REASON: " If your mail server does not stamp Authentication-Results, set ""platforms.email.require_authenticated_sender: false (or EMAIL_TRUST_FROM_HEADER=true) to accept the risk.",_UNTRUSTED_AUTHSERV_REASON: " Check that platforms.email.authserv_id (EMAIL_AUTHSERV_ID) names your mail server.",- _MISSING_AUTHSERV_REASON: " Set EMAIL_AUTHSERV_ID (or platforms.email.authserv_id) to the receiving MTA's exact authserv-id, "- "or set platforms.email.require_authenticated_sender: false (or EMAIL_TRUST_FROM_HEADER=true) to accept the risk.",}+# A missing pin is account config, not one sender's mail, so connect() names its fix once per account.+_MISSING_AUTHSERV_HINT = (" Set EMAIL_AUTHSERV_ID (or platforms.email.authserv_id) to the receiving MTA's exact authserv-id, "+ "or set platforms.email.require_authenticated_sender: false (or EMAIL_TRUST_FROM_HEADER=true) "+ "to accept the risk.")# One token of a clause: a property we read (``header.from=x``; the value may be or contain a quoted-string), or# any other whitespace-delimited token consumed whole, so text inside quotes or other values is never read as a prop._QUOTED = r'"(?:[^"\\]|\\.)*"'@@ -449,7 +451,6 @@ class EmailAdapter(BasePlatformAdapter):"""Email gateway adapter using IMAP (receive) and SMTP (send)."""# One email carries the whole body, so cron delivery hands over the full payload untruncated.splits_long_messages = True- _missing_pin_warned = False # the no-authserv_id drop warning is logged once per process# Per-account seen-UID snapshot surviving adapter recreation: the reconnect watcher builds a FRESH# adapter per retry; without this connect(is_reconnect=True) would re-mark the mailbox seen and skip@@ -608,6 +609,8 @@ class EmailAdapter(BasePlatformAdapter):return self._fail("[Email] %s", message, "email_missing_configuration", message, retryable=False)if not self._probe_imap(is_reconnect) or not self._probe_smtp():return False+ if not is_reconnect and self._require_authenticated_sender and not self._authserv_id:+ logger.warning("[Email] %s: %s.%s", self._address, _MISSING_AUTHSERV_REASON, _MISSING_AUTHSERV_HINT)self._running = Trueself._poll_task = asyncio.create_task(self._poll_loop())print(f"[Email] Connected as {self._address}")@@ -806,10 +809,7 @@ class EmailAdapter(BasePlatformAdapter):if self._require_authenticated_sender and not msg_data.get("sender_authenticated", False):auth_reason = msg_data.get("auth_reason", "no verdict")hint = _DROP_HINTS.get(auth_reason, "")- # A missing pin drops every message, so name the fix once per process rather than once per message.- repeat = auth_reason == _MISSING_AUTHSERV_REASON and EmailAdapter._missing_pin_warned- if (is_listed or (granted and hint)) and not repeat:- EmailAdapter._missing_pin_warned |= auth_reason == _MISSING_AUTHSERV_REASON+ if is_listed or (granted and hint):logger.warning("[Email] Dropping sender with unauthenticated From: %s (%s).%s", sender_addr, auth_reason, hint)else:logger.debug("[Email] Dropping %s sender with unauthenticated From: %s (%s)",diff --git a/tests/gateway/test_email.py b/tests/gateway/test_email.pyindex 85d4f16598a..efa1bf1a9cc 100644--- a/tests/gateway/test_email.py+++ b/tests/gateway/test_email.py@@ -380,6 +380,8 @@ class TestDispatchDefersToGatewayAuthorization(unittest.TestCase):"""A granted sender's mail with no Authentication-Results suggests a server that never stamps it, so the dropwarns with the opt-out hint; no stamp from the pinned authserv-id warns to check authserv_id; a listed sender'sfailing verdict warns without a hint; forged stranger mail under open access stays at debug."""+ import asyncio+ from gateway.config import PlatformConfigfrom plugins.platforms.email.adapter import _MISSING_AUTHSERV_REASON, _UNTRUSTED_AUTHSERV_REASON, EmailAdapteradapter_log = "plugins.platforms.email.adapter"@@ -391,16 +393,29 @@ class TestDispatchDefersToGatewayAuthorization(unittest.TestCase):env={"EMAIL_ALLOWED_USERS": self.STRANGER}), [])self.assertIn("authserv_id", logs.output[0])self.assertNotIn("require_authenticated_sender", logs.output[0])- # No pin drops every message, so the fix (pin, or the explicit opt-out) is logged once per process.- with patch.object(EmailAdapter, "_missing_pin_warned", False), \- self.assertLogs(adapter_log, level="DEBUG") as logs:- for _ in range(2):- self.assertEqual(self._reached_gateway(authenticated=False, auth_reason=_MISSING_AUTHSERV_REASON,- env={"EMAIL_ALLOWED_USERS": self.STRANGER}), [])- warnings = [line for line in logs.output if line.startswith("WARNING")]- self.assertEqual(len(warnings), 1, logs.output)- self.assertIn("EMAIL_AUTHSERV_ID", warnings[0])- self.assertIn("EMAIL_TRUST_FROM_HEADER=true", warnings[0])+ # No pin drops every message of the account, so connect() names the fix (pin, or the explicit opt-out) once+ # per account at startup: each unpinned mailbox warns, a reconnect retry does not, pinned/opted-out stay quiet.+ def connect(address, extra=None, is_reconnect=False):+ with patch.dict(os.environ, {"EMAIL_ADDRESS": address, "EMAIL_PASSWORD": "secret",+ "EMAIL_IMAP_HOST": "imap.test.com", "EMAIL_SMTP_HOST": "smtp.test.com"}):+ adapter = EmailAdapter(PlatformConfig(enabled=True, extra=extra or {}))+ imap = MagicMock()+ imap.uid.return_value = ("OK", [b""])+ with patch("imaplib.IMAP4_SSL", return_value=imap), patch.object(adapter, "_connect_smtp"):+ self.assertTrue(asyncio.run(adapter.connect(is_reconnect=is_reconnect)))++ with self.assertLogs(adapter_log, level="WARNING") as logs:+ for address in ("[email protected]", "[email protected]"):+ connect(address)+ connect("[email protected]", is_reconnect=True)+ connect("[email protected]", {"authserv_id": "mx.ourserver.com"})+ connect("[email protected]", {"require_authenticated_sender": False})+ self.assertEqual([line.split(": ")[0] for line in logs.output],+ ["WARNING:plugins.platforms.email.adapter:[Email] [email protected]",+ "WARNING:plugins.platforms.email.adapter:[Email] [email protected]"], logs.output)+ self.assertIn(_MISSING_AUTHSERV_REASON, logs.output[1])+ self.assertIn("EMAIL_AUTHSERV_ID", logs.output[1])+ self.assertIn("EMAIL_TRUST_FROM_HEADER=true", logs.output[1])with self.assertNoLogs(adapter_log, level="WARNING"):self.assertEqual(self._reached_gateway(authenticated=False, auth_reason="dmarc=fail",env={"EMAIL_ALLOW_ALL_USERS": "true"}), [])