aboutsummaryrefslogtreecommitdiff
path: root/castle-search/test
diff options
context:
space:
mode:
authorIgor Soarez <igor@soarez.org>2026-11-18 10:50:14 +0000
committerIgor Soarez <igor@soarez.org>2026-11-18 10:50:14 +0000
commit910ac7cd424f6401ea042a84603c42ba055994f8 (patch)
tree3ff40da7d14c644428c71cab1b005eca0b186d13 /castle-search/test
parent39f887e07721c5b3ddd31657e5e8e25e4486d2e5 (diff)
castle-search: sign X back in when a search finds it signed out
Username, password, the authenticator code (TOTP) and, when X asks, the code it emails, read over IMAP. At most one try per 30 minutes, the password never sent twice, and any unknown step (a captcha) fails saying a person has to sign in. Details from the file CCS_X_LOGIN_FILE names.
Diffstat (limited to 'castle-search/test')
-rw-r--r--castle-search/test/x-login.test.ts91
1 files changed, 91 insertions, 0 deletions
diff --git a/castle-search/test/x-login.test.ts b/castle-search/test/x-login.test.ts
new file mode 100644
index 0000000..11468d5
--- /dev/null
+++ b/castle-search/test/x-login.test.ts
@@ -0,0 +1,91 @@
+import assert from "node:assert/strict";
+import { test } from "node:test";
+
+import { decodeWords, parseFetch, parseInternalDate, xCodeFromSubject } from "../src/imap.ts";
+import { classify, type LoginProbe } from "../src/x-login.ts";
+import { base32Decode, totp, totpRemaining } from "../src/totp.ts";
+
+// RFC 6238 appendix B, SHA-1: the secret "12345678901234567890".
+const RFC_SECRET = "GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ";
+
+test("totp matches RFC 6238's SHA-1 vectors", () => {
+ assert.equal(totp(RFC_SECRET, 59_000, 8), "94287082");
+ assert.equal(totp(RFC_SECRET, 1_111_111_109_000, 8), "07081804");
+ assert.equal(totp(RFC_SECRET, 1_234_567_890_000, 8), "89005924");
+ assert.equal(totp(RFC_SECRET, 59_000), "287082");
+});
+
+test("base32 ignores spaces and case, and refuses other characters", () => {
+ assert.deepEqual(base32Decode("gezd gnbv"), base32Decode("GEZDGNBV"));
+ assert.throws(() => base32Decode("GEZD1"));
+});
+
+test("totpRemaining counts down within the step", () => {
+ assert.equal(totpRemaining(0), 30);
+ assert.equal(totpRemaining(29_000), 1);
+});
+
+test("X's code is read from the subjects X uses, and nothing else", () => {
+ assert.equal(xCodeFromSubject("Your X confirmation code is abc12def"), "abc12def");
+ assert.equal(xCodeFromSubject("Your X verification code is: 123456"), "123456");
+ assert.equal(xCodeFromSubject("123456 is your X verification code"), "123456");
+ assert.equal(xCodeFromSubject("New login to X from Chrome on Mac"), undefined);
+ assert.equal(xCodeFromSubject("Security alert"), undefined);
+});
+
+test("encoded subjects are decoded", () => {
+ assert.equal(decodeWords("=?UTF-8?B?WW91ciBYIGNvbmZpcm1hdGlvbiBjb2RlIGlzIGFiYzEyZGVm?="), "Your X confirmation code is abc12def");
+ assert.equal(decodeWords("=?UTF-8?Q?Caf=C3=A9_menu?="), "Café menu");
+});
+
+test("a FETCH reply splits into one header set per message", () => {
+ const raw =
+ '* 1 FETCH (INTERNALDATE "18-Nov-2026 10:16:23 +0000" BODY[HEADER.FIELDS (FROM SUBJECT)] {60}\r\n' +
+ "From: X <verify@x.com>\r\nSubject: Your X confirmation code is\r\n abc12def\r\n\r\n)\r\n" +
+ '* 2 FETCH (INTERNALDATE "18-Nov-2026 10:20:00 +0000" BODY[HEADER.FIELDS (FROM SUBJECT)] {40}\r\n' +
+ "From: X <info@x.com>\r\nSubject: New login\r\n\r\n)\r\nc4 OK Success\r\n";
+ const mails = parseFetch(raw);
+ assert.equal(mails.length, 2);
+ assert.equal(mails[0]!.subject, "Your X confirmation code is abc12def");
+ assert.equal(mails[0]!.received, parseInternalDate("18-Nov-2026 10:16:23 +0000"));
+ assert.equal(mails[0]!.received, Date.UTC(2026, 10, 18, 10, 16, 23));
+ assert.equal(mails[1]!.from, "X <info@x.com>");
+});
+
+const probe = (over: Partial<LoginProbe>): LoginProbe => ({
+ url: "https://x.com/i/jf/onboarding/web?mode=login",
+ path: "/i/jf/onboarding/web",
+ hash: "",
+ text: "",
+ inputs: [],
+ signedIn: false,
+ ...over,
+});
+
+// The steps as castle Chrome saw them on 2026-11-18.
+test("each step of X's sign-in is recognised", () => {
+ assert.equal(
+ classify(probe({ text: "See what's happening\n\nEmail or username\n\nContinue", inputs: [{ name: "username_or_email", type: "text", autocomplete: "username webauthn" }] })).kind,
+ "username",
+ );
+ assert.equal(
+ classify(probe({ hash: "#/s/login_enter_password/r-1", text: "Login\n\nUsername\nPassword", inputs: [{ name: "username", type: "text", autocomplete: "" }, { name: "password", type: "password", autocomplete: "" }] })).kind,
+ "password",
+ );
+ assert.equal(
+ classify(probe({ hash: "#/s/two_factor_code/r-2", text: "Enter your two factor code\n\nUse your authenticator app to generate the code.", inputs: [{ name: "", type: "text", autocomplete: "one-time-code" }] })).kind,
+ "totp",
+ );
+ assert.equal(
+ classify(probe({ text: "Check your email\n\nYou'll receive a code to verify here so you can sign in.", inputs: [{ name: "text", type: "text", autocomplete: "" }] })).kind,
+ "email-code",
+ );
+ assert.equal(
+ classify(probe({ text: "Enter your phone number or username\n\nThere was unusual login activity on your account.", inputs: [{ name: "text", type: "text", autocomplete: "" }] })).kind,
+ "username",
+ );
+ assert.equal(classify(probe({ url: "https://x.com/home", path: "/home", signedIn: true })).kind, "done");
+ assert.equal(classify(probe({})).kind, "loading");
+ assert.equal(classify(probe({ text: "Authenticate your account\n\nSolve this puzzle" })).kind, "unknown");
+ assert.equal(classify(probe({ text: "Wrong password!", inputs: [{ name: "password", type: "password", autocomplete: "" }] })).kind, "wrong-password");
+});