Skip to content

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

Support unlabeled six-digit verification forms

Tony Gomez6594e666f7f5
diff --git a/server/login-fill.test.ts b/server/login-fill.test.ts
index 820493d1c8..d9a5fc5753 100644
--- a/server/login-fill.test.ts
+++ b/server/login-fill.test.ts
@@ -8,6 +8,7 @@ import { parseTotpKey, totpCode, type TotpKey } from "./totp.ts";
// Obvious fakes only.
const PASSWORD = "fake-S3cret!pass";
const KEY = parseTotpKey("JBSWY3DPEHPK3PXP")!;
+const SECOND_KEY = parseTotpKey("GEZDGNBVGY3TQOJQ")!;
const NOW = 1_700_000_010_000; // 10 s into a 30 s step
const DASHBOARD = { url: "https://example.com/dashboard?session=abc", html: "<title>Dashboard</title><h1>Welcome back</h1>" };
@@ -323,6 +324,39 @@ describe("one-time codes and fields", () => {
expect(Array.from({ length: 6 }, (_, index) => page.field(`input[name=d${index}]`).value).join("")).toBe(totpCode(KEY, NOW));
});
+ it.each([
+ ["Verify Security Code", "[email protected]", KEY],
+ ["Verify Security Code", "[email protected]", SECOND_KEY],
+ ["Enter ZIP code", "[email protected]", KEY],
+ ])("uses the selected account's code on the verification screen (%s, %s)", async (prompt, username, key) => {
+ const boxes = Array.from({ length: 6 }, (_, index) =>
+ `<div><input name="d${index}" type="number" maxlength="1"></div>`
+ ).join("");
+ const shouldFill = prompt === "Verify Security Code";
+ const { page, fill } = setup(
+ "https://example.com/login",
+ LOGIN_FORM,
+ [
+ { host: "example.com", username: "[email protected]", totp: KEY },
+ { host: "example.com", username: "[email protected]", totp: SECOND_KEY },
+ ],
+ );
+ page.answer = () => page.sent.length === 1
+ ? { navigate: {
+ url: "https://example.com/verify",
+ html: `<h2>${prompt}</h2><form><div>${boxes}</div><button type="submit">Verify</button></form>`,
+ } }
+ : { navigate: DASHBOARD };
+
+ const outcome = await fill(username);
+ const code = totpCode(key, NOW);
+ expect(outcome.filled).toEqual(shouldFill ? ["username", "password", "code"] : ["username", "password"]);
+ expect(page.typed.slice(2).join("")).toBe(shouldFill ? code : "");
+ expect(page.sent.slice(1)).toEqual(shouldFill
+ ? [Object.fromEntries([...code].map((digit, index) => [`d${index}`, digit]))] : []);
+ expectNoSecret(outcome, page, [code, PASSWORD, key.secret]);
+ });
+
it("points at the person's inbox, then the person, when a code is asked for and no 2FA key is saved", async () => {
const { page, fill } = setup("https://example.com/2fa", `<input name="verification_code">`, [{ host: "example.com", username: "alice" }]);
const outcome = await fill();
diff --git a/server/login-fill.ts b/server/login-fill.ts
index 413c61eaa6..903395f52e 100644
--- a/server/login-fill.ts
+++ b/server/login-fill.ts
@@ -195,6 +195,13 @@ const FIND_FIELDS = `((nonce, step) => {
const near = (el) => el.parentElement === first.parentElement || (el.parentElement && first.parentElement && el.parentElement.parentElement === first.parentElement.parentElement);
const group = first.maxLength === 1 ? inputs.filter((el) => el.maxLength === 1 && near(el)) : [];
boxes = group.length >= 4 ? group : [first];
+ } else if (inputs.length === 6 && inputs.every((el) =>
+ el.type === "number" && el.getAttribute("maxlength") === "1" &&
+ !NOT_CODE.test(hint(el)) && el.parentElement?.parentElement === inputs[0].parentElement?.parentElement
+ ) && all("h1, h2, h3, h4, h5, h6, [role=heading], legend").some((el) =>
+ visible(el) && /^verify security code$/i.test((el.textContent || "").trim())
+ )) {
+ boxes = inputs;
}
if (username) username.setAttribute(MARK, key + "username");
if (password) password.setAttribute(MARK, key + "password");