diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 2657ce439..c2252f0ff 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -30552,3 +30552,20 @@ - 未做:给其他人上传头像(负责人 11-23 定为上线后第一个版本再做)。 - 关联记录:BUG-1081(本人可编辑不可删)、BUG-2057 / BUG-2059(账号头像上传与显示)、BUG-2253(侧栏账号行)。 - 修复版本:分支 `claude/profile-avatar-2270`(待父会话推送)。 + +## BUG-2271 | 验证码邮件只有一行英文标题和裸验证码,通知栏看不到验证码 + +- 状态:fixed-pending-verify(需在 staging 实收一封邮件确认) +- 首次发现 / 最近更新:2026-11-22(负责人看验证码邮件原型,选方案 A 卡片式)/ 2026-11-23 +- 现象:登录、邮箱验证、重置密码、更换邮箱四种验证码邮件只有英文标题(如「Your Jyotisha sign-in code」)、一行加粗验证码和「This code expires in five minutes.」;没有中文,没有品牌样式,手机通知栏不点开看不到验证码。 +- 触发条件:任何一次通过 Resend 发出的邮箱验证码。 +- 根因:`frontend/src/modules/identity/email/resend-email-otp-sender.ts` 里的标题和正文是上线自建登录时的临时写法,一直没有换成正式邮件。 +- 修复: + - 新增纯模板 `frontend/src/modules/identity/email/otp-email-template.ts`:标题验证码在前、中文在前英文在后(四种各一句);正文按原型 A:文字品牌名、中英两行说明、浅色底块居中的大号等宽验证码(数字中间没有空格,可整串复制)、「5 分钟内有效,只能用一次。」、「如果不是你本人操作……」、页脚自动发出说明与客服邮箱。 + - 邮件写法:表格排版、全部行内样式、系统字体、最宽 540px 居中、浅色配色(深色邮箱反色后仍清楚)、`lang="zh-CN"`、`color-scheme` / `supported-color-schemes`、隐藏的预览行(验证码 + 5 分钟内有效);没有外部资源、跟踪和链接。纯文字版内容相同,验证码单独一行。 + - 客服邮箱经 `legalEntityValue('contactEmail')` 读取(BUG-2190 / BUG-2198 的同一取值);为空时「有问题请写信到…」中英两句都不出现。 + - 未改:登录逻辑、验证码有效期(仍是 auth-factory 的 `expiresIn: 300`,没有导出常量,模板里写 5 分钟并用测试与它钉在一起)、发送接口、Resend 请求字段与请求头、出错处理、配置。 +- 验证:`frontend/tests/identity-email-sender.test.ts` 原标题断言改为新标题(注明原值 / 新值 / 原因),新增 7 条:四种邮件各一条(标题以验证码开头、JSON 请求体里中文标题原样往返、html 中验证码恰好两次(正文块 + 预览行)、中英首行都在、无 ` = { + "sign-in": { + subjectZh: "是你的 Jyotisha 登录验证码", + subjectEn: "Your sign-in code", + leadZh: "你的登录验证码", + leadEn: "Your sign-in code", + }, + "email-verification": { + subjectZh: "是你的 Jyotisha 邮箱验证码", + subjectEn: "Your email verification code", + leadZh: "你的邮箱验证码", + leadEn: "Your email verification code", + }, + "forget-password": { + subjectZh: "是你的 Jyotisha 重置密码验证码", + subjectEn: "Your password reset code", + leadZh: "你的重置密码验证码", + leadEn: "Your password reset code", + }, + "change-email": { + subjectZh: "是你的 Jyotisha 新邮箱确认码", + subjectEn: "Your new email confirmation code", + leadZh: "你的新邮箱确认码", + leadEn: "Your new email confirmation code", + }, +}; + +const color = { + outer: "#ecebe6", + card: "#fbfaf7", + line: "#e4e1d9", + ink: "#1d1d1f", + ink2: "#5f5f59", + codeBg: "#f1eee7", +}; + +const sans = + "-apple-system,BlinkMacSystemFont,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','Noto Sans SC','Helvetica Neue',Arial,sans-serif"; +const serif = "'Songti SC','Noto Serif SC',Georgia,'Times New Roman',serif"; +const mono = "'SF Mono',Menlo,Consolas,'Courier New',monospace"; + +export function escapeHtml(value: string): string { + return value.replace(/[&<>"']/g, (character) => { + const entities: Record = { + "&": "&", + "<": "<", + ">": ">", + '"': """, + "'": "'", + }; + return entities[character]; + }); +} + +export interface OtpEmailContent { + subject: string; + html: string; + text: string; +} + +export function buildOtpEmail( + otp: string, + type: EmailOtpType, + supportEmail: string | null, +): OtpEmailContent { + const copy = copyByType[type]; + const minutes = OTP_EMAIL_VALID_MINUTES; + const validZh = `${minutes} 分钟内有效,只能用一次。`; + const validEn = `Valid for ${minutes} minutes. It works once.`; + const ignoreZh = "如果不是你本人操作,忽略这封邮件就行,账号不会有任何变化。"; + const ignoreEn = "If you didn't ask for this, ignore this email. Nothing changes on your account."; + const support = supportEmail?.trim() || null; + const footZh = `这封邮件由系统自动发出,请不要直接回复。${support ? `有问题请写信到 ${support}。` : ""}`; + const footEn = `This is an automated email.${support ? ` For help, write to ${support}.` : ""}`; + + const subject = `${otp} ${copy.subjectZh} | ${copy.subjectEn}`; + + const text = [ + "Jyotisha", + "", + copy.leadZh, + copy.leadEn, + "", + otp, + "", + validZh, + ignoreZh, + "", + validEn, + ignoreEn, + "", + footZh, + footEn, + "", + ].join("\n"); + + const e = escapeHtml; + const code = e(otp); + const preheader = `${code} ${minutes} 分钟内有效 / valid for ${minutes} minutes`; + const small = (value: string, size: number) => + `
${e(value)}`; + + const html = ` + + + + + + +Jyotisha + + +
${preheader}
+ + +
+ + + +
+
Jyotisha
+
 
+
${e(copy.leadZh)}${small(copy.leadEn, 14)}
+ + +
${code}
+
${e(validZh)}${small(validEn, 13)}
+
${e(ignoreZh)}${small(ignoreEn, 13)}
+
${e(footZh)}
${e(footEn)}
+
+ +`; + + return { subject, html, text }; +} diff --git a/frontend/src/modules/identity/email/resend-email-otp-sender.ts b/frontend/src/modules/identity/email/resend-email-otp-sender.ts index 9f74b95f7..c3a27b404 100644 --- a/frontend/src/modules/identity/email/resend-email-otp-sender.ts +++ b/frontend/src/modules/identity/email/resend-email-otp-sender.ts @@ -1,32 +1,13 @@ import type { EmailOtpMessage, EmailOtpSender, - EmailOtpType, } from "../contracts.ts"; +import { legalEntityValue } from "../../../lib/legal-entity.ts"; +import { buildOtpEmail } from "./otp-email-template.ts"; const resendEndpoint = "https://api.resend.com/emails"; const safeDeliveryError = "OTP email delivery failed"; -const subjectByType: Record = { - "sign-in": "Your Jyotisha sign-in code", - "email-verification": "Your Jyotisha verification code", - "forget-password": "Reset your Jyotisha password", - "change-email": "Confirm your new Jyotisha email", -}; - -function escapeHtml(value: string): string { - return value.replace(/[&<>"']/g, (character) => { - const entities: Record = { - "&": "&", - "<": "<", - ">": ">", - '"': """, - "'": "'", - }; - return entities[character]; - }); -} - export interface ResendEmailOtpSenderOptions { apiKey: string; from: string; @@ -45,8 +26,12 @@ export class ResendEmailOtpSender implements EmailOtpSender { } async send(message: EmailOtpMessage): Promise { - const escapedOtp = escapeHtml(message.otp); - const subject = subjectByType[message.type]; + // BUG-2271: bilingual card email; the OTP is escaped inside the template. + const { subject, html, text } = buildOtpEmail( + message.otp, + message.type, + legalEntityValue("contactEmail"), + ); try { const response = await this.fetchImpl(resendEndpoint, { @@ -61,8 +46,8 @@ export class ResendEmailOtpSender implements EmailOtpSender { from: this.from, to: [message.email], subject, - text: `${subject}: ${message.otp}. This code expires in five minutes.`, - html: `

${escapeHtml(subject)}

${escapedOtp}

This code expires in five minutes.

`, + text, + html, }), }); diff --git a/frontend/tests/identity-email-sender.test.ts b/frontend/tests/identity-email-sender.test.ts index 0a44f0e75..b09cd2a2c 100644 --- a/frontend/tests/identity-email-sender.test.ts +++ b/frontend/tests/identity-email-sender.test.ts @@ -3,7 +3,12 @@ import test from "node:test"; import { FakeEmailOtpSender } from "../src/modules/identity/email/fake-email-otp-sender.ts"; import { ResendEmailOtpSender } from "../src/modules/identity/email/resend-email-otp-sender.ts"; -import type { EmailOtpMessage } from "../src/modules/identity/contracts.ts"; +import type { EmailOtpMessage, EmailOtpType } from "../src/modules/identity/contracts.ts"; +import { createEmailOtpOptions } from "../src/modules/identity/auth-factory.ts"; +import { + buildOtpEmail, + OTP_EMAIL_VALID_MINUTES, +} from "../src/modules/identity/email/otp-email-template.ts"; const message: EmailOtpMessage = { email: "person@example.com", @@ -50,9 +55,13 @@ test("Resend OTP sender emits an idempotent authenticated request", async () => >; assert.equal(body.from, "Jyotisha "); assert.deepEqual(body.to, [message.email]); - assert.equal(body.subject, "Your Jyotisha sign-in code"); - assert.match(String(body.text), /123456/); + // 原值:subject "Your Jyotisha sign-in code" + // 新值:code first, Chinese then English (BUG-2271) + // 原因:owner picked prototype A; the notification preview shows the code + assert.equal(body.subject, "123456 是你的 Jyotisha 登录验证码 | Your sign-in code"); + assert.match(String(body.text), /^123456$/m); assert.match(String(body.html), /123456/); + assert.deepEqual(Object.keys(body).sort(), ["from", "html", "subject", "text", "to"]); }); test("Resend OTP sender escapes template values", async () => { @@ -111,3 +120,87 @@ test("network errors use the same safe delivery error", async () => { new Error("OTP email delivery failed"), ); }); + +// BUG-2271: bilingual card email (owner picked prototype A). +const otpTypes: Array<{ + type: EmailOtpType; + subject: string; + leadZh: string; + leadEn: string; +}> = [ + { type: "sign-in", subject: "是你的 Jyotisha 登录验证码 | Your sign-in code", leadZh: "你的登录验证码", leadEn: "Your sign-in code" }, + { type: "email-verification", subject: "是你的 Jyotisha 邮箱验证码 | Your email verification code", leadZh: "你的邮箱验证码", leadEn: "Your email verification code" }, + { type: "forget-password", subject: "是你的 Jyotisha 重置密码验证码 | Your password reset code", leadZh: "你的重置密码验证码", leadEn: "Your password reset code" }, + { type: "change-email", subject: "是你的 Jyotisha 新邮箱确认码 | Your new email confirmation code", leadZh: "你的新邮箱确认码", leadEn: "Your new email confirmation code" }, +]; + +function occurrences(haystack: string, needle: string): number { + return haystack.split(needle).length - 1; +} + +for (const { type, subject, leadZh, leadEn } of otpTypes) { + test(`BUG-2271: ${type} email is the bilingual card with the code first in the subject`, async () => { + let raw = ""; + const sender = new ResendEmailOtpSender({ + apiKey: "re_test_secret_value", + from: "Jyotisha ", + fetchImpl: async (_input, init) => { + raw = String(init?.body); + assert.equal(new Headers(init?.headers).get("content-type"), "application/json"); + return Response.json({ id: "email_123" }); + }, + }); + await sender.send({ ...message, otp: "482913", type }); + + // The JSON body round-trips the Chinese subject unchanged. + const body = JSON.parse(raw) as { subject: string; html: string; text: string }; + assert.equal(body.subject, `482913 ${subject}`); + assert.ok(body.subject.startsWith("482913 ")); + + // Code: once in the visible block, once in the hidden preheader. + assert.equal(occurrences(body.html, "482913"), 2); + assert.match(body.html, />482913<\/td>/); + assert.match(body.html, /display:none[^>]*>482913 5 分钟内有效 \/ valid for 5 minutes/); + assert.match(body.html, //); + assert.match(body.html, //); + assert.doesNotMatch(body.html, /&', "sign-in", null); + assert.doesNotMatch(html, /