Repository navigation
feat(notify): Email 通知渠道支持每用户 SMTP (ONE-25) - #135
Merged
Merged
Conversation
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
force-pushed
the
feat/ONE-25-per-user-smtp
branch
from
October 22, 2026 08:52
eccd550 to
e5c1d98
Compare
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. |
Contributor
Author
…-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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
已定:保留本 PR
用户拍板「用 135」(ONE-25 评论),重复的 #133(ONE-21)已关闭、不再合并,ONE-21 单由 Mike 按重复单收口。本 PR 为仅存的 Email 通知渠道每用户 SMTP 实现。
评审 should-fix 已处理(commit
c1c41fc)email-notify.ts):smtp_from || smtp_username现在与to同一套拦截 —— 拒绝[\r\n,;<>]且必须过isValidEmail;不合格在建立连接之前就返回Invalid Email channel SMTP sender address,不再可能把注入串送进MAIL FROM:<…>/From:头。email_configured收紧(me.ts):Email 渠道改为专属判定Boolean(to?.trim() && smtp_host?.trim()),其余渠道沿用原「任意字段非空」启发式;已补单测(to+host / 仅 to / 仅 host / 全空白 / 空对象 / 其他渠道不受影响)。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 负责。