The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(email): warn when a listed sender's mail fails authentication
kshitijk4poorc4343445c431
diff --git a/plugins/platforms/email/adapter.py b/plugins/platforms/email/adapter.pyindex 653b5ada421..50db9e343d3 100644--- a/plugins/platforms/email/adapter.py+++ b/plugins/platforms/email/adapter.py@@ -66,6 +66,12 @@ _AUTH_METHOD_RE = re.compile(r"\s*(dmarc|dkim|spf)\s*=\s*([a-z]+)", re.IGNORECAS# server which never stamps the header (the opt-out case), rather than at the message itself._NO_AUTH_RESULTS_REASON = "no Authentication-Results header"_UNTRUSTED_AUTHSERV_REASON = "no Authentication-Results from trusted authserv-id"+# Operator-fixable reasons a granted sender's mail fails authentication, and the fix each log line names.+_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.",+}# 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'"(?:[^"\\]|\\.)*"'@@ -728,20 +734,14 @@ class EmailAdapter(BasePlatformAdapter):return False# Reject spoofed senders (GHSA-rxqh-5572-8m77): every grant keys on the attacker-controlled From:, and a pairing# code or decline is mailed back to it; fail-closed. Open access is no exception: the session and every reply- # key on From:, so a forged one lands in that address's conversation and makes the agent mail it. Only a granted- # sender's mail with no Authentication-Results at all warns, since that suggests a server that never stamps it and- # the opt-out hint fits; so does one whose only stamp is from another authserv-id (a mis-pinned authserv_id). An explicit failing verdict (dmarc=fail, misaligned SPF/DKIM, ...) is routine forgery,- # even when From: is an allowlisted address or open access grants everyone; the hint would be wrong advice.+ # key on From:, so a forged one lands in that address's conversation and makes the agent mail it.+ # Warn only where the operator can act: a known misconfiguration, or a listed contact's own mail failing.+ # Forged stranger mail (open access grants everyone) stays at debug.if self._require_authenticated_sender and not msg_data.get("sender_authenticated", False):auth_reason = msg_data.get("auth_reason", "no verdict")- if granted and auth_reason == _NO_AUTH_RESULTS_REASON:- logger.warning("[Email] Dropping sender with unauthenticated From: %s (%s). 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.", sender_addr, auth_reason)- elif granted and auth_reason == _UNTRUSTED_AUTHSERV_REASON:- logger.warning("[Email] Dropping sender with unauthenticated From: %s (%s). Check that "- "platforms.email.authserv_id (EMAIL_AUTHSERV_ID) names your mail server.",- sender_addr, auth_reason)+ hint = _DROP_HINTS.get(auth_reason, "")+ if granted and (hint or sender_addr.lower() in listed):+ 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)","authorized" if granted else "unknown", sender_addr, auth_reason)diff --git a/tests/gateway/test_email.py b/tests/gateway/test_email.pyindex 5070920210f..e07b1bc6499 100644--- a/tests/gateway/test_email.py+++ b/tests/gateway/test_email.py@@ -378,8 +378,8 @@ class TestDispatchDefersToGatewayAuthorization(unittest.TestCase):def test_only_a_missing_auth_results_header_warns_with_the_opt_out_hint(self):"""A granted sender's mail with no Authentication-Results suggests a server that never stamps it, so the drop- warns with the opt-out hint; a stamp only from another authserv-id warns to check authserv_id; an explicit- failing verdict is routine forgery and stays at debug."""+ warns with the opt-out hint; no stamp from the pinned authserv-id warns to check authserv_id; a listed sender's+ failing verdict warns without a hint; forged stranger mail under open access stays at debug."""from plugins.platforms.email.adapter import _UNTRUSTED_AUTHSERV_REASONadapter_log = "plugins.platforms.email.adapter"@@ -391,10 +391,15 @@ 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])- for label, env in {"open access": {"EMAIL_ALLOW_ALL_USERS": "true"},- "listed sender": {"EMAIL_ALLOWED_USERS": self.STRANGER}}.items():- with self.subTest(label), self.assertNoLogs(adapter_log, level="WARNING"):- self.assertEqual(self._reached_gateway(authenticated=False, auth_reason="dmarc=fail", env=env), [])+ with self.assertNoLogs(adapter_log, level="WARNING"):+ self.assertEqual(self._reached_gateway(authenticated=False, auth_reason="dmarc=fail",+ env={"EMAIL_ALLOW_ALL_USERS": "true"}), [])+ # A listed contact's failing mail (broken DKIM, a forwarder) is worth seeing, but the opt-out is wrong advice.+ with self.assertLogs(adapter_log, level="WARNING") as logs:+ self.assertEqual(self._reached_gateway(authenticated=False, auth_reason="dmarc=fail",+ env={"EMAIL_ALLOWED_USERS": self.STRANGER}), [])+ self.assertIn("dmarc=fail", logs.output[0])+ self.assertNotIn("require_authenticated_sender", logs.output[0])def test_mail_the_gateway_admits_or_answers_reaches_it(self):cases = {diff --git a/website/docs/user-guide/messaging/email.md b/website/docs/user-guide/messaging/email.mdindex c9ffb197014..6f61a7f733e 100644--- a/website/docs/user-guide/messaging/email.md+++ b/website/docs/user-guide/messaging/email.md@@ -177,7 +177,7 @@ Email access is stricter by default than chat-style platforms:Allowlist entries match whole addresses. A bare entry such as `alice` (a chat username in `GATEWAY_ALLOWED_USERS`, say) never admits `alice@` at any domain, and mail from such an address is dropped rather than paired or declined.-Hermes acts on a message only when the `Authentication-Results` header stamped by your receiving server authenticates its `From:` domain (DMARC, or aligned SPF/DKIM). This applies with open access (`EMAIL_ALLOW_ALL_USERS` / `GATEWAY_ALLOW_ALL_USERS`) too: open access admits any sender, but each conversation and every reply is keyed on `From:`, so a forged `From:` would put the attacker's text into that address's session and make Hermes mail it. Pairing codes and declines likewise need an authenticated `From:`, so neither is mailed to a forged address. If your mail server does not stamp that header, set `platforms.email.require_authenticated_sender: false` (or `EMAIL_TRUST_FROM_HEADER=true`) to accept the risk; otherwise mail from an admitted sender that carries no such header is dropped with a `Dropping sender with unauthenticated From` warning in the gateway log. The same warning, pointing at `platforms.email.authserv_id` (`EMAIL_AUTHSERV_ID`) instead, fires when the only stamp comes from a server other than the pinned `authserv_id`. Mail whose header records a failing verdict (a forged `From:`, say) is dropped quietly, logged at debug level only.+Hermes acts on a message only when the `Authentication-Results` header stamped by your receiving server authenticates its `From:` domain (DMARC, or aligned SPF/DKIM). This applies with open access (`EMAIL_ALLOW_ALL_USERS` / `GATEWAY_ALLOW_ALL_USERS`) too: open access admits any sender, but each conversation and every reply is keyed on `From:`, so a forged `From:` would put the attacker's text into that address's session and make Hermes mail it. Pairing codes and declines likewise need an authenticated `From:`, so neither is mailed to a forged address. If your mail server does not stamp that header, set `platforms.email.require_authenticated_sender: false` (or `EMAIL_TRUST_FROM_HEADER=true`) to accept the risk; otherwise mail from an admitted sender that carries no such header is dropped with a `Dropping sender with unauthenticated From` warning in the gateway log. The same warning, pointing at `platforms.email.authserv_id` (`EMAIL_AUTHSERV_ID`) instead, fires when no stamp comes from the pinned `authserv_id`. Mail from an address listed in `EMAIL_ALLOWED_USERS` / `GATEWAY_ALLOWED_USERS` that fails authentication otherwise (for example a sender whose DKIM is broken, or a forwarder that breaks alignment) is dropped with the same warning and its verdict, with no opt-out hint. Other senders' failing mail, such as forged `From:` under open access, is dropped quietly, logged at debug level only.:::warning**Use a dedicated inbox and configure `EMAIL_ALLOWED_USERS` for normal operation.** Email pairing is opt-in because shared inboxes often contain unrelated unread messages, and Hermes should not reply to those contacts by default.