Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(login): recognize Chinese email verification codes

Afloatfb51c03c9c32
diff --git a/server/email-code.test.ts b/server/email-code.test.ts
index 234cc22c25..e16622a89a 100644
--- a/server/email-code.test.ts
+++ b/server/email-code.test.ts
@@ -28,6 +28,12 @@ describe("extractCode", () => {
["Code", "Enter this code: 123-456", "123456"],
["Verify", "Your security code: A1B2C3", "A1B2C3"],
["Código", "Seu código de verificação é 553211", "553211"],
+ ["登录验证码", "您的验证码为:483920,5 分钟内有效。", "483920"],
+ ["登入驗證碼", "您的驗證碼是 483920,有效時間為 5 分鐘。", "483920"],
+ ["登录通知", "您的验证码是483920,请勿分享。", "483920"],
+ ["登入通知", "您的驗證碼為A1B2C3,請勿分享。", "A1B2C3"],
+ ["", "<p>您的验证码是 <b>483920</b>,5 分钟内有效</p>", "483920"],
+ ["483920是您的验证码", "", "483920"],
["Code", "Your code is 900100. Again: 900100.", "900100"],
])("takes the one code named as one in %s", (subject, body, code) => {
expect(extractCode(subject, body)).toEqual({ kind: "code", code });
@@ -39,12 +45,16 @@ describe("extractCode", () => {
["a zip code", "Hello", "Delivered to zip code 94103."],
["a code inside a link", "Welcome", "Your code: https://acme.test/v?c=482913 was generated."],
["a year in the footer", "Your code", "Use the app. © 2026 Acme. Your code is below."],
+ ["a Chinese postal address", "送货通知", "邮政编码为 100000。"],
+ ["a Chinese label beside a link", "验证码", "请访问 https://acme.test/v?c=483920 获取验证码。"],
+ ["a number inside a longer Latin identifier", "验证码", "abc483920 和 483920abc 都不是验证码。"],
])("finds none in %s", (_name, subject, body) => {
expect(extractCode(subject, body).kind).toBe("none");
});
it("calls two different codes ambiguous rather than guessing", () => {
expect(extractCode("Code", "Your code is 111222 or use code 333444")).toEqual({ kind: "ambiguous" });
+ expect(extractCode("验证码", "验证码为111222,或使用验证码333444。")).toEqual({ kind: "ambiguous" });
});
it("keeps only codes that fit the boxes on the page", () => {
@@ -93,6 +103,12 @@ describe("waitForEmailCode", () => {
expect(await waitForEmailCode([mailbox([mail({ from: "[email protected]" })])], options(clocked()))).toEqual({ kind: "miss", why: "wrong-sender" });
});
+ it("does not read a Chinese verification email from another site", async () => {
+ const box = mailbox([mail({ from: "[email protected]", subject: "登录验证码", text: "您的验证码为:483920。" })]);
+ expect(await waitForEmailCode([box], options(clocked()))).toEqual({ kind: "miss", why: "wrong-sender" });
+ expect(box.read).toEqual([]);
+ });
+
it("ignores a message from before the code was asked for, past the clock skew", async () => {
expect(await waitForEmailCode([mailbox([mail({ at: ASKED - 31_000 })])], options(clocked()))).toEqual({ kind: "miss", why: "not-found" });
expect(await waitForEmailCode([mailbox([mail({ at: ASKED - 29_000 })])], options(clocked()))).toEqual({ kind: "code", code: "483920" });
diff --git a/server/email-code.ts b/server/email-code.ts
index 00dd4da2d4..93ebec6828 100644
--- a/server/email-code.ts
+++ b/server/email-code.ts
@@ -57,10 +57,10 @@ const ADDRESSES = /\S+@\S+/gu;
const DATES = /\b\d{4}-\d{1,2}-\d{1,2}\b|\b\d{1,2}[/.]\d{1,2}[/.]\d{2,4}\b/gu;
const COPYRIGHT = /(?:©|\(c\)|copyright)\s*\d{4}/giu;
const PHONES = /\+\d[\d ().-]{6,}\d|\(\d{3}\)\s*\d{3}[-. ]?\d{4}/gu;
-const CANDIDATE = /(?<![\p{L}\p{N}-])(\d{3}[- ]\d{3}|\d{4}[- ]\d{4}|[A-Z0-9]{4}-[A-Z0-9]{4}|\d{4,10}|[A-Z0-9]{4,10})(?![\p{L}\p{N}-])/gu;
+const CANDIDATE = /(?:(?<![\p{L}\p{N}-])|(?<=\p{Script=Han}))(\d{3}[- ]\d{3}|\d{4}[- ]\d{4}|[A-Z0-9]{4}-[A-Z0-9]{4}|\d{4,10}|[A-Z0-9]{4,10})(?:(?![\p{L}\p{N}-])|(?=\p{Script=Han}))/gu;
const NOT_A_CODE_WORD = /(?:zip|postal|post|promo|coupon|discount|voucher|country|area|referral|invite|gift|error|reference|tracking|bar|qr|source)[- ]?code\b/giu;
-const CODE_WORD = /(?<![\p{L}\p{N}])(?:code|otp|passcode|pin|verification|verify|c[oó]digo|verifica[cç][aã]o|verificaci[oó]n|verifizierungscode|bestätigungscode|sicherheitscode|anmeldecode|код)(?![\p{L}\p{N}])/iu;
-const CODE_MAIL_SUBJECT = /(?<![\p{L}\p{N}])(?:code|otp|passcode|verify|verification|sign[- ]?in|log[- ]?in|login|c[oó]digo|verifica[cç][aã]o)(?![\p{L}\p{N}])/iu;
+const CODE_WORD = /验证码|驗證碼|(?<![\p{L}\p{N}])(?:code|otp|passcode|pin|verification|verify|c[oó]digo|verifica[cç][aã]o|verificaci[oó]n|verifizierungscode|bestätigungscode|sicherheitscode|anmeldecode|код)(?![\p{L}\p{N}])/iu;
+const CODE_MAIL_SUBJECT = /验证码|驗證碼|(?<![\p{L}\p{N}])(?:code|otp|passcode|verify|verification|sign[- ]?in|log[- ]?in|login|c[oó]digo|verifica[cç][aã]o)(?![\p{L}\p{N}])/iu;
/** The readable text of a message body, without its markup. */
export function bodyText(raw: string): string {
diff --git a/server/login-email-code.test.ts b/server/login-email-code.test.ts
index 9a69e4a7ad..09fef6dcba 100644
--- a/server/login-email-code.test.ts
+++ b/server/login-email-code.test.ts
@@ -102,6 +102,24 @@ describe("a code is entered only while its requesting turn remains active", () =
});
describe("an emailed code inside fill_login", () => {
+ it("fills a code from a Chinese email without exposing the mail or code", async () => {
+ const boxes: CodeMailbox[] = [{
+ list: async () => [{ id: "m", at: NOW + 1_000, from: "[email protected]", subject: "登录验证码" }],
+ body: async () => `<p>您的验证码是 <b>${CODE}</b>,5 分钟内有效</p>`,
+ }];
+ const { page: current, run } = setup("https://acme.test/verify", CODE_PAGE, async (ask) => {
+ const found = await waitForEmailCode(boxes, { site: ask.site, askedAt: NOW, isUsed: () => false, now: ask.now, sleep: ask.sleep });
+ return found.kind === "code" ? { ...found, typed: () => {} } : found;
+ });
+ current.answer = () => ({ navigate: HOME });
+ const outcome = await run();
+ expect(current.typed).toEqual([CODE]);
+ expect(outcome).toMatchObject({ ok: true, filled: ["code"] });
+ expect(JSON.stringify(outcome)).not.toContain(CODE);
+ expect(JSON.stringify(outcome)).not.toContain("验证码");
+ expect(JSON.stringify(current.engineCalls)).not.toContain(CODE);
+ });
+
it("fills the code as real input and tells the bot only that it did", async () => {
const { page: current, remembered, asks, typedCount, run } = setup("https://acme.test/verify", CODE_PAGE, code);
current.answer = () => ({ navigate: HOME });