Skip to content

The agent that grows with you

fix(email): require exact authserv-id before trusting results

MoonsvnLyn4f39bd4f3560
diff --git a/plugins/platforms/email/adapter.py b/plugins/platforms/email/adapter.py
index 3237d531ead..1f2360357b2 100644
--- a/plugins/platforms/email/adapter.py
+++ b/plugins/platforms/email/adapter.py
@@ -73,11 +73,14 @@ _PREAUTH_FETCH = (
_AUTH_METHOD_RE = re.compile(r"\s*(dmarc|dkim|spf)\s*=\s*([a-z]+)", re.IGNORECASE)
_NO_AUTH_RESULTS_REASON = "no Authentication-Results header"
_UNTRUSTED_AUTHSERV_REASON = "no Authentication-Results from trusted authserv-id"
+_MISSING_AUTHSERV_REASON = "authserv-id is not configured; refusing to trust Authentication-Results"
# 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.",
+ _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.",
}
# 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.
@@ -362,8 +365,9 @@ def _verify_sender_authentication(msg: email_lib.message.Message, from_addr: 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 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
+ FIRST instance is authoritative, and it must match the required *authserv_id* exactly. A matching id is a
+ pin, not proof of provenance: the receiving MTA must strip inbound results claiming its id (RFC 8601).
+ True on DMARC pass, aligned SPF pass, or aligned DKIM (``header.d``) pass. No header or no pin → fail-closed
(opt out via ``EmailAdapter._require_authenticated_sender``)."""
from_domain = _domain_of(from_addr)
if not from_domain:
@@ -375,7 +379,9 @@ def _verify_sender_authentication(msg: email_lib.message.Message, from_addr: str
# 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():
+ if not authserv_id.strip(): # without a pin the topmost header may be one the sender wrote
+ return False, _MISSING_AUTHSERV_REASON
+ if 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).
@@ -474,7 +480,9 @@ class EmailAdapter(BasePlatformAdapter):
self._require_authenticated_sender = bool(extra["require_authenticated_sender"])
else:
self._require_authenticated_sender = not _esecret_bool("EMAIL_TRUST_FROM_HEADER", False)
- # Optional authserv-id pinning Authentication-Results to the operator's own server (defeats an injected header sorting first).
+ # Pin Authentication-Results to the operator's receiving MTA. When an
+ # allowlist grants access, an absent pin fails closed rather than
+ # accepting a sender-supplied result header.
self._authserv_id = (extra.get("authserv_id", "") or _get_secret("EMAIL_AUTHSERV_ID", "")).strip().lower()
self._seen_uids: set = set()
self._seen_uids_max: int = 2000 # cap to prevent unbounded memory growth
diff --git a/plugins/platforms/email/plugin.yaml b/plugins/platforms/email/plugin.yaml
index 8e9ca3d877b..5cf233df5c6 100644
--- a/plugins/platforms/email/plugin.yaml
+++ b/plugins/platforms/email/plugin.yaml
@@ -37,3 +37,7 @@ optional_env:
description: "Default address for cron / notification delivery"
prompt: "Home address"
password: false
+ - name: EMAIL_AUTHSERV_ID
+ description: "Exact authserv-id stamped by the receiving MTA (required with an allowlist unless sender auth is disabled)"
+ prompt: "Receiving MTA authserv-id"
+ password: false
diff --git a/website/docs/reference/environment-variables.md b/website/docs/reference/environment-variables.md
index caf1cd5e372..0fa0f320a3f 100644
--- a/website/docs/reference/environment-variables.md
+++ b/website/docs/reference/environment-variables.md
@@ -437,6 +437,7 @@ These are set automatically by the Docker terminal backend when `proxy.enabled:
| `EMAIL_SMTP_HOST` | SMTP hostname for the email adapter |
| `EMAIL_SMTP_PORT` | SMTP port |
| `EMAIL_ALLOWED_USERS` | Comma-separated email addresses allowed to message the bot |
+| `EMAIL_AUTHSERV_ID` | Exact authserv-id that stamps the receiving MTA's topmost `Authentication-Results`; required with an allowlist unless authenticated-sender verification is disabled |
| `EMAIL_HOME_ADDRESS` | Default recipient for proactive email delivery |
| `EMAIL_HOME_ADDRESS_NAME` | Display name for the email home target |
| `EMAIL_POLL_INTERVAL` | Email polling interval in seconds |
diff --git a/website/docs/user-guide/messaging/email.md b/website/docs/user-guide/messaging/email.md
index bc0da296406..0858d448919 100644
--- a/website/docs/user-guide/messaging/email.md
+++ b/website/docs/user-guide/messaging/email.md
@@ -98,6 +98,10 @@ EMAIL_SMTP_HOST=smtp.gmail.com
# Security (recommended)
+# Exact authserv-id on the receiving MTA's topmost Authentication-Results header.
+# With an allowlist, leave sender authentication enabled only after confirming
+# that MTA strips inbound headers claiming this id before adding its own stamp.
+EMAIL_AUTHSERV_ID=mx.google.com
# Optional
EMAIL_IMAP_PORT=993 # Default: 993 (IMAP SSL)
@@ -179,6 +183,8 @@ Allowlist entries match whole addresses. A bare entry such as `alice` (a chat us
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 topmost `Authentication-Results` header (the one your server prepends; lower ones are never consulted) does not name exactly the pinned `authserv_id`. `authserv_id` must equal the exact host your server writes; if that host varies per message, leave `authserv_id` unset. 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.
+When `EMAIL_ALLOWED_USERS` grants access, sender authentication is enabled by default. Set `EMAIL_AUTHSERV_ID` to the exact id stamped on the **topmost** `Authentication-Results` header by your receiving MTA. Hermes fails closed without that pin, or when the top header does not match it. Confirm that your MTA strips inbound `Authentication-Results` headers claiming its id before it prepends its own; a copied id alone does not prove provenance. If your provider cannot meet this requirement, you may explicitly accept the risk with `platforms.email.require_authenticated_sender: false` or `EMAIL_TRUST_FROM_HEADER=true`.
+
:::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.
:::
@@ -191,7 +197,7 @@ Hermes acts on a message only when the `Authentication-Results` header stamped b
|---------|----------|
| **"IMAP connection failed"** at startup | Verify `EMAIL_IMAP_HOST` and `EMAIL_IMAP_PORT`. Ensure IMAP is enabled on the account. For Gmail, enable it in Settings → Forwarding and POP/IMAP. |
| **"SMTP connection failed"** at startup | Verify `EMAIL_SMTP_HOST` and `EMAIL_SMTP_PORT`. Check that your password is correct (use App Password for Gmail). |
-| **Messages not received** | Check `EMAIL_ALLOWED_USERS` includes the sender's email. Check spam folder — some providers flag automated replies. |
+| **Messages not received** | Check `EMAIL_ALLOWED_USERS` includes the sender's email. With an allowlist, set `EMAIL_AUTHSERV_ID` to the exact receiving-MTA stamp and verify it is the topmost `Authentication-Results` header. Check spam folder — some providers flag automated replies. |
| **"Authentication failed"** | For Gmail, you must use an App Password, not your regular password. Ensure 2FA is enabled first. |
| **Duplicate replies** | Ensure only one gateway instance is running. Check `hermes gateway status`. |
| **Slow response** | The default poll interval is 15 seconds. Reduce with `EMAIL_POLL_INTERVAL=5` for faster response (but more IMAP connections). |
@@ -207,6 +213,7 @@ Hermes acts on a message only when the `Authentication-Results` header stamped b
- Use **App Passwords** instead of your main password (required for Gmail with 2FA)
- Set `EMAIL_ALLOWED_USERS` to restrict who can interact with the agent
+- With an allowlist, set `EMAIL_AUTHSERV_ID` to the exact id of the receiving MTA's topmost `Authentication-Results` stamp, and verify that MTA strips inbound headers claiming that id
- The password is stored in `~/.hermes/.env` — protect this file (`chmod 600`)
- IMAP uses SSL (port 993) and SMTP uses STARTTLS (port 587) by default — connections are encrypted
@@ -224,5 +231,6 @@ Hermes acts on a message only when the `Authentication-Results` header stamped b
| `EMAIL_SMTP_PORT` | No | `587` | SMTP server port |
| `EMAIL_POLL_INTERVAL` | No | `15` | Seconds between inbox checks |
| `EMAIL_ALLOWED_USERS` | No | — | Comma-separated allowed sender addresses |
+| `EMAIL_AUTHSERV_ID` | No | — | Exact authserv-id on the receiving MTA's topmost `Authentication-Results`; required when using an allowlist unless sender authentication is disabled |
| `EMAIL_HOME_ADDRESS` | No | — | Default delivery target for cron jobs |
| `EMAIL_ALLOW_ALL_USERS` | No | `false` | Allow all senders (not recommended) |
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/environment-variables.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/environment-variables.md
index 12b6f5e8c74..ed9afa70a80 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/environment-variables.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/reference/environment-variables.md
@@ -322,6 +322,7 @@ description: "Hermes Agent 使用的所有环境变量完整参考"
| `EMAIL_SMTP_HOST` | 邮件适配器的 SMTP 主机名 |
| `EMAIL_SMTP_PORT` | SMTP 端口 |
| `EMAIL_ALLOWED_USERS` | 允许向 bot 发送消息的逗号分隔邮箱地址 |
+| `EMAIL_AUTHSERV_ID` | 接收 MTA 在最上层 `Authentication-Results` 中写入的精确 authserv-id;使用白名单时必须设置,除非已禁用发件人认证校验 |
| `EMAIL_HOME_ADDRESS` | 主动邮件投递的默认收件人 |
| `EMAIL_HOME_ADDRESS_NAME` | 邮件主目标的显示名称 |
| `EMAIL_POLL_INTERVAL` | 邮件轮询间隔(秒) |
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/messaging/email.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/messaging/email.md
index c4433d6787e..c4a13105c0a 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/messaging/email.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/messaging/email.md
@@ -66,6 +66,9 @@ EMAIL_SMTP_HOST=smtp.gmail.com
# 安全设置(推荐)
+# 接收 MTA 在最上层 Authentication-Results 中写入的精确 authserv-id。
+# 使用白名单时,请先确认该 MTA 会移除伪造此 id 的入站头部,再写入自己的结果。
+EMAIL_AUTHSERV_ID=mx.google.com
# 可选
EMAIL_IMAP_PORT=993 # 默认:993(IMAP SSL)
@@ -141,6 +144,8 @@ platforms:
2. **未设置白名单** → 未知发件人会收到配对码
3. **`EMAIL_ALLOW_ALL_USERS=true`** → 接受任意发件人(请谨慎使用)
+当 `EMAIL_ALLOWED_USERS` 授予访问权限时,发件人认证默认开启。请将 `EMAIL_AUTHSERV_ID` 设置为接收 MTA 在**最上层** `Authentication-Results` 头中写入的精确 id。未设置该 pin 或顶部头部不匹配时,Hermes 会拒绝邮件。还须确认 MTA 会先移除声称该 id 的入站 `Authentication-Results` 头,再添加自己的头部,因为仅复制 id 不能证明头部来自该 MTA。若服务商无法满足此要求,可显式设置 `platforms.email.require_authenticated_sender: false` 或 `EMAIL_TRUST_FROM_HEADER=true` 来接受风险。
+
:::warning
**请务必配置 `EMAIL_ALLOWED_USERS`。** 若不配置,任何知道 Agent 邮箱地址的人都可以发送命令。Agent 默认具有终端访问权限。
:::
@@ -153,7 +158,7 @@ platforms:
|---------|----------|
| 启动时出现 **"IMAP connection failed"** | 检查 `EMAIL_IMAP_HOST` 和 `EMAIL_IMAP_PORT`。确保账户已启用 IMAP。对于 Gmail,在设置 → 转发和 POP/IMAP 中启用。 |
| 启动时出现 **"SMTP connection failed"** | 检查 `EMAIL_SMTP_HOST` 和 `EMAIL_SMTP_PORT`。确认密码正确(Gmail 请使用应用专用密码)。 |
-| **未收到邮件** | 检查 `EMAIL_ALLOWED_USERS` 是否包含发件人邮箱。检查垃圾邮件文件夹——部分服务商会将自动回复标记为垃圾邮件。 |
+| **未收到邮件** | 检查 `EMAIL_ALLOWED_USERS` 是否包含发件人邮箱。使用白名单时,将 `EMAIL_AUTHSERV_ID` 设为接收 MTA 写入的精确 id,并确认它位于最上层 `Authentication-Results` 头。检查垃圾邮件文件夹——部分服务商会将自动回复标记为垃圾邮件。 |
| **"Authentication failed"** | 对于 Gmail,必须使用应用专用密码,而非常规密码。请先确保已启用双重验证。 |
| **重复回复** | 确保只有一个 gateway 实例在运行。检查 `hermes gateway status`。 |
| **响应缓慢** | 默认轮询间隔为 15 秒。设置 `EMAIL_POLL_INTERVAL=5` 可加快响应速度(但会增加 IMAP 连接次数)。 |
@@ -169,6 +174,7 @@ platforms:
- 使用**应用专用密码**代替主密码(Gmail 开启双重验证后必须如此)
- 设置 `EMAIL_ALLOWED_USERS` 以限制可与 Agent 交互的用户
+- 使用白名单时,将 `EMAIL_AUTHSERV_ID` 设为接收 MTA 最上层 `Authentication-Results` 头的精确 id,并确认该 MTA 会移除伪造此 id 的入站头部
- 密码存储在 `~/.hermes/.env` 中——请保护此文件(`chmod 600`)
- IMAP 默认使用 SSL(端口 993),SMTP 默认使用 STARTTLS(端口 587)——连接已加密
@@ -186,5 +192,6 @@ platforms:
| `EMAIL_SMTP_PORT` | 否 | `587` | SMTP 服务器端口 |
| `EMAIL_POLL_INTERVAL` | 否 | `15` | 收件箱检查间隔(秒) |
| `EMAIL_ALLOWED_USERS` | 否 | — | 允许的发件人地址,逗号分隔 |
+| `EMAIL_AUTHSERV_ID` | 否 | — | 接收 MTA 最上层 `Authentication-Results` 中的精确 authserv-id;使用白名单时必须设置,除非已禁用发件人认证校验 |
| `EMAIL_HOME_ADDRESS` | 否 | — | cron 任务的默认投递目标 |
| `EMAIL_ALLOW_ALL_USERS` | 否 | `false` | 允许所有发件人(不推荐) |
\ No newline at end of file