fix(identity): bilingual card OTP email with the code first in the subject (BUG-2271)
Owner picked prototype A. Table layout, inline styles, no links or external resources; plain-text part kept; support address via legalEntityValue. Sign-in logic, OTP lifetime, Resend request shape and errors unchanged. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
This commit is contained in:
@@ -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 中验证码恰好两次(正文块 + 预览行)、中英首行都在、无 `<script`、无 http 地址、无 class / style 标签 / 链接 / 图片、纯文字版存在且验证码单独一行)、恶意验证码字符串被转义、无客服邮箱时帮助句缺席、邮件的 5 分钟等于 `expiresIn`。tsc、eslint、identity 相关前端测试通过(`identity-auth-integration` 因本机无 docker 失败,与改动前相同)。600px / 390px 浅色与强制反色截图看过。
|
||||
- 防复发:模板是纯函数,测试逐种钉住标题、正文、转义和有效期一致。
|
||||
- 关联记录:BUG-1349(测试验证码通道只在非生产)、BUG-2190 / BUG-2198(客服邮箱只经 legalEntityValue 显示)。此前没有关于验证码邮件内容的记录。
|
||||
- 修复版本:分支 `claude/otp-email`(待父会话推送)。
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import type { EmailOtpType } from "../contracts.ts";
|
||||
|
||||
/**
|
||||
* The OTP email (BUG-2271, owner picked prototype A): bilingual card, Chinese line
|
||||
* then English line, code first in the subject so the notification preview shows it.
|
||||
*
|
||||
* Mail-client safe: table layout, inline styles only, system font stacks, no
|
||||
* external resources, no links. The only interpolated values are the OTP and the
|
||||
* support address from config; both are escaped.
|
||||
*
|
||||
* The "5 分钟" copy mirrors `expiresIn: 300` in createEmailOtpOptions
|
||||
* (auth-factory.ts), which is not exported as a constant; a test pins the two together.
|
||||
*/
|
||||
export const OTP_EMAIL_VALID_MINUTES = 5;
|
||||
|
||||
interface OtpEmailCopy {
|
||||
subjectZh: string;
|
||||
subjectEn: string;
|
||||
leadZh: string;
|
||||
leadEn: string;
|
||||
}
|
||||
|
||||
const copyByType: Record<EmailOtpType, OtpEmailCopy> = {
|
||||
"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<string, string> = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
};
|
||||
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) =>
|
||||
`<br><span style="font-size:${size}px;font-weight:400;color:${color.ink2};">${e(value)}</span>`;
|
||||
|
||||
const html = `<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<meta name="supported-color-schemes" content="light dark">
|
||||
<title>Jyotisha</title>
|
||||
</head>
|
||||
<body style="margin:0;padding:0;background-color:${color.outer};">
|
||||
<div style="display:none;max-height:0;overflow:hidden;opacity:0;mso-hide:all;font-size:1px;line-height:1px;color:${color.outer};">${preheader}</div>
|
||||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="background-color:${color.outer};">
|
||||
<tr><td align="center" style="padding:22px 12px;">
|
||||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="max-width:540px;width:100%;">
|
||||
<tr><td style="background-color:${color.card};border:1px solid ${color.line};border-radius:12px;padding:28px 24px;font-family:${sans};color:${color.ink};">
|
||||
<div style="font-family:${serif};font-size:20px;line-height:1.3;color:${color.ink};">Jyotisha</div>
|
||||
<div style="height:1px;line-height:1px;font-size:1px;background-color:${color.line};margin:18px 0 22px;"> </div>
|
||||
<div style="font-size:17px;font-weight:600;line-height:1.45;color:${color.ink};">${e(copy.leadZh)}${small(copy.leadEn, 14)}</div>
|
||||
<table role="presentation" width="100%" cellpadding="0" cellspacing="0" border="0" style="margin:20px 0 18px;">
|
||||
<tr><td align="center" style="background-color:${color.codeBg};border-radius:10px;padding:18px 12px;font-family:${mono};font-size:32px;font-weight:600;letter-spacing:6px;line-height:1.2;color:${color.ink};">${code}</td></tr>
|
||||
</table>
|
||||
<div style="font-size:14px;line-height:1.6;color:${color.ink};">${e(validZh)}${small(validEn, 13)}</div>
|
||||
<div style="font-size:14px;line-height:1.6;color:${color.ink};margin-top:12px;">${e(ignoreZh)}${small(ignoreEn, 13)}</div>
|
||||
</td></tr>
|
||||
<tr><td style="padding:14px 6px 0;font-family:${sans};font-size:12px;line-height:1.6;color:${color.ink2};">${e(footZh)}<br>${e(footEn)}</td></tr>
|
||||
</table>
|
||||
</td></tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
return { subject, html, text };
|
||||
}
|
||||
@@ -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<EmailOtpType, string> = {
|
||||
"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<string, string> = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
};
|
||||
return entities[character];
|
||||
});
|
||||
}
|
||||
|
||||
export interface ResendEmailOtpSenderOptions {
|
||||
apiKey: string;
|
||||
from: string;
|
||||
@@ -45,8 +26,12 @@ export class ResendEmailOtpSender implements EmailOtpSender {
|
||||
}
|
||||
|
||||
async send(message: EmailOtpMessage): Promise<void> {
|
||||
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: `<p>${escapeHtml(subject)}</p><p><strong>${escapedOtp}</strong></p><p>This code expires in five minutes.</p>`,
|
||||
text,
|
||||
html,
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -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 <login@staging.jyotisha.chat>");
|
||||
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 <login@staging.jyotisha.chat>",
|
||||
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.ok(body.html.includes(leadZh) && body.html.includes(leadEn));
|
||||
assert.ok(body.html.includes("5 分钟内有效,只能用一次。"));
|
||||
assert.ok(body.html.includes("Valid for 5 minutes. It works once."));
|
||||
assert.match(body.html, /<html lang="zh-CN">/);
|
||||
assert.match(body.html, /<meta name="color-scheme" content="light dark">/);
|
||||
assert.match(body.html, /<meta name="supported-color-schemes" content="light dark">/);
|
||||
assert.doesNotMatch(body.html, /<script/i);
|
||||
assert.doesNotMatch(body.html, /https?:/i);
|
||||
assert.doesNotMatch(body.html, /<a\b|<img\b|<link\b|<style\b|class=/i);
|
||||
|
||||
// Plain-text part is present, code on its own line, both languages.
|
||||
assert.ok(body.text.length > 0);
|
||||
assert.match(body.text, /^482913$/m);
|
||||
assert.ok(body.text.includes(leadZh) && body.text.includes(leadEn));
|
||||
assert.ok(body.text.indexOf("5 分钟内有效") < body.text.indexOf("Valid for 5 minutes"));
|
||||
});
|
||||
}
|
||||
|
||||
test("BUG-2271: a hostile OTP string is escaped in the html", () => {
|
||||
const { html, text } = buildOtpEmail('<script>alert("x")</script>&', "sign-in", null);
|
||||
assert.doesNotMatch(html, /<script/i);
|
||||
assert.ok(html.includes("<script>alert("x")</script>&"));
|
||||
assert.match(text, /^<script>alert\("x"\)<\/script>&$/m);
|
||||
});
|
||||
|
||||
test("BUG-2271: the support sentences follow the configured support address", () => {
|
||||
const withSupport = buildOtpEmail("482913", "sign-in", "help@example.test");
|
||||
assert.ok(withSupport.html.includes("有问题请写信到 help@example.test。"));
|
||||
assert.ok(withSupport.html.includes("For help, write to help@example.test."));
|
||||
assert.ok(withSupport.text.includes("有问题请写信到 help@example.test。"));
|
||||
|
||||
const without = buildOtpEmail("482913", "sign-in", null);
|
||||
for (const part of [without.html, without.text]) {
|
||||
assert.doesNotMatch(part, /有问题请写信到|For help/);
|
||||
assert.ok(part.includes("这封邮件由系统自动发出,请不要直接回复。"));
|
||||
assert.ok(part.includes("This is an automated email."));
|
||||
}
|
||||
});
|
||||
|
||||
test("BUG-2271: the email's 5 minutes matches the OTP lifetime", () => {
|
||||
const options = createEmailOtpOptions(new FakeEmailOtpSender(), "secret", true);
|
||||
assert.equal(options.expiresIn, OTP_EMAIL_VALID_MINUTES * 60);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user