Skip to content

feat(notify): Email 通知渠道支持每用户 SMTP (ONE-25) - #135

Merged
mapxn merged 2 commits into
mainfrom
feat/ONE-25-per-user-smtp
Oct 22, 2026
Merged

mapxn merged 2 commits into
mainfrom
feat/ONE-25-per-user-smtp

Conversation

@mapxn

@mapxn mapxn commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

已定:保留本 PR

用户拍板「用 135」(ONE-25 评论),重复的 #133(ONE-21)已关闭、不再合并,ONE-21 单由 Mike 按重复单收口。本 PR 为仅存的 Email 通知渠道每用户 SMTP 实现。

评审 should-fix 已处理(commit c1c41fc)

  1. 发件人校验(email-notify.ts):smtp_from || smtp_username 现在与 to 同一套拦截 —— 拒绝 [\r\n,;<>] 且必须过 isValidEmail;不合格在建立连接之前就返回 Invalid Email channel SMTP sender address,不再可能把注入串送进 MAIL FROM:<…> / From: 头。
  2. email_configured 收紧(me.ts):Email 渠道改为专属判定 Boolean(to?.trim() && smtp_host?.trim()),其余渠道沿用原「任意字段非空」启发式;已补单测(to+host / 仅 to / 仅 host / 全空白 / 空对象 / 其他渠道不受影响)。
  3. 可选 nit 一并做掉:smtp_host 增加 hostname / IPv4 / 带括号 IPv6 形态校验(拒绝空白、userinfo、CRLF、路径与分号),不合格返回 Invalid Email channel SMTP host;clean() 改为 String(value ?? "").trim(),非字符串 JSON 值不再抛 TypeError。

me.ts 只动了这 8 行判定,没顺手跑 biome 的 organizeImports(该文件 import 排序问题是既有状态,避免无关 diff)。

背景

Email 通知渠道原来走 settings 表的全局邮件配置(email_provider / smtp_config / resend_config),与系统邮件共用一套发送账号。本 PR 把通知邮件改为每用户自配 SMTP,系统邮件路径行为不变。

改动

  • worker/src/services/notify/email-notify.ts:改为读取 notification_configs.email_config 的 smtp_host / smtp_port / smtp_username / smtp_password / smtp_from(+ 可选 from_name)发送;smtp_port 字符串转数字;smtp_host 为空直接返回错误(由 notify/index.ts 记入通知历史),不读全局设置、不回落系统邮件。
  • worker/src/services/smtp-transport.ts(新增):把 email.ts 里的 SMTP 客户端(含 AUTH LOGIN、465 隐式 TLS、其余 STARTTLS、不支持 STARTTLS 时拒绝明文发凭据)抽成共享模块,两条邮件路径共用一份实现。
  • worker/src/services/email.ts:sendEmail() 签名与行为不变,sendViaSMTP() 改为调用共享模块(去掉重复实现),并导出 isValidEmail 供通知渠道复用同一套收件人校验。
  • worker/src/routes/me.ts:仅 email_configured 一处收紧(见上)。
  • 测试:worker/test/email-notify.test.ts(内存假 SMTP 服务器)、worker/test/me-notifications.test.ts;worker/test/stubs/sockets.ts 支持安装假 socket。

自测(should-fix 后重跑)

  • vitest run:8 文件 / 78 测试全部通过(原 67 + 本次 11 条新增)
  • tsc --noEmit:通过
  • biome check(本 PR 改动的源码与测试文件):通过

契约

email_config 字段:to、from_name、smtp_host、smtp_port(string)、smtp_username、smtp_password、smtp_from。

前端待对接

web/src/pages/channels/ChannelPage.tsx 的 email 渠道目前只有 to 一个字段,需补 5 个 SMTP 字段(密码用 password 类型,回填 ••••••)——由 ONE-26 负责。

The Email notification channel used the global settings-table mail
configuration (email_provider / smtp_config / resend_config), sharing one
sender account with system mail. It now sends through each user's own SMTP
server taken from notification_configs.email_config:

  smtp_host, smtp_port (string), smtp_username, smtp_password, smtp_from,
  from_name

- no global-settings lookup in the notification path, no fallback to system
  mail: an empty smtp_host returns an error which the caller records in
  notification history
- port 465 = implicit TLS, anything else = STARTTLS; credentials are never
  sent in plaintext when the server does not upgrade
- the SMTP client itself is extracted from services/email.ts into
  services/smtp-transport.ts and shared by both paths, so system mail
  (email verification, password reset, 2FA codes) keeps its exact behaviour
- smtp_password keeps using the existing me.ts masking / keep-old-value logic
  (it matches the `password` secret-field rule)

Tests: fake in-memory SMTP server covering implicit TLS, STARTTLS negotiation,
plaintext-credential refusal, missing host/port and history recording, plus a
me.ts round-trip test for the password mask.
@mapxn

mapxn commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #133 (ONE-21). ONE-25 cancelled as duplicate; the e2e SMTP tests here (in-memory fake SMTP server + me.ts roundtrip) are noted in review ONE-23, branch feat/ONE-25-per-user-smtp preserved.

@mapxn mapxn closed this Oct 22, 2026
@mapxn mapxn reopened this Oct 22, 2026
@mapxn

mapxn commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

纠正:本 PR 是定案保留的实现。用户已定「用 135」(ONE-25),#133(ONE-21)已关闭;本 PR 在 09:04 被一次按旧结论(Set A 胜)的误操作关闭,现重新打开。评审走 ONE-27、验证走 ONE-28,通过后另行请用户批合并。此前的「Superseded by #133」结论作废。

…-25)

Review should-fix items:

1. email-notify: the sender address (`smtp_from || smtp_username`) now goes
   through the same guards as the recipient — reject `[\r\n,;<>]` and require
   `isValidEmail` — since it is interpolated into `MAIL FROM:<…>` and the
   `From:` header. Malformed/injecting senders fail before any connection is
   opened.
2. me.ts: `email_configured` for the Email channel is now
   `Boolean(to?.trim() && smtp_host?.trim())` instead of "any field is filled";
   other channels keep the existing heuristic.

Nits taken: `smtp_host` is validated as a plain hostname / IPv4 / bracketed
IPv6 (no spaces, no userinfo, no CRLF), and `clean()` coerces with
`String(value ?? "")` so non-string JSON values can no longer throw.

Tests: email-notify 12 -> 18 cases (host/sender injection + format, username
fallback as sender, non-string config values), me-notifications 3 -> 8 cases
(email_configured matrix, other channels unaffected). 78 tests total.
@mapxn
mapxn merged commit f829987 into main Oct 22, 2026
1 check passed
@mapxn
mapxn deleted the feat/ONE-25-per-user-smtp branch October 22, 2026 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant