Public: anyone can read this; the crew can change it.

"tries at /signin close the API door" passes every time on a loaded check machine #16915

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/4406-tries-at-signin-close-the-api-door-passe → main 0 commits fork

  • Merged. torque/4406-tries-at-signin-close-the-api-door-passe was merged into main as 7a62f7b.

Review

Sign in to review this change.
2 files changed +60 −22
scripts/test-honesty-allow.json +1 −1
@@ -365,7 +365,7 @@
365365 "test/session-settle.test.ts": {"wait":2},
366366 "test/shards-run.test.ts": {"wait":1},
367367 "test/signin-page.test.ts": {"wait":1},
368 "test/signin-password-doors.test.ts": {"wait":2},
368 "test/signin-password-doors.test.ts": {"wait":1},
369369 "test/signins.test.ts": {"wait":5},
370370 "test/signup-fresh.test.ts": {"wait":1},
371371 "test/snapshot-destination.test.ts": {"wait":2},
test/signin-password-doors.test.ts +59 −21
@@ -4,7 +4,7 @@ import { mkdtemp, readdir, readFile, rm } from "node:fs/promises";
44 import { tmpdir } from "node:os";
55 import { join } from "node:path";
66 import { createApp, type App } from "../src/app.ts";
7 import { GATE_RETRY_AFTER } from "../src/core/hash-gate.ts";
7 import { GATE_RETRY_AFTER, MAX_CONCURRENT_HASHES, withHashSlot } from "../src/core/hash-gate.ts";
88 import * as users from "../src/core/users.ts";
99 import { serve, type Servers } from "../src/http/server.ts";
1010 import { setLimit } from "../src/settings/limits.ts";
@@ -39,6 +39,23 @@ async function forge(): Promise<{ app: App; url: string }> {
3939 return { app: made, url: servers.url };
4040 }
4141
42 // Ask until the try is counted against the window, within one deadline
43 // rather than a fixed number of asks: a loaded machine can keep the gate
44 // turning tries away for longer than any count of sleeps. `outcome` names an
45 // answer "gate" (turned away unhashed: ask again), `counted`, or anything else,
46 // which fails loudly instead of passing for the wrong reason.
47 const DEADLINE_MS = 20_000;
48 async function askUntilCounted<T>(ask: () => Promise<T>, outcome: (answer: T) => string, counted: string, deadline: number): Promise<T> {
49 for (;;) {
50 const answer = await ask();
51 const seen = outcome(answer);
52 if (seen === counted) return answer;
53 expect(seen).toBe("gate");
54 expect(Date.now()).toBeLessThan(deadline);
55 await Bun.sleep(50); // real-timer: the process-wide hash gate frees on real time, not a clock we hold
56 }
57 }
58
4259 const post = (url: string, body: Record<string, string | boolean>): Promise<Response> =>
4360 fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
4461
@@ -81,20 +98,22 @@ describe("password doors share the sign-in windows", () => {
8198 test("the terminal sign-in refuses the right password after the window fills", async () => {
8299 const made = (await forge()).app;
83100 const session = new Session(made, { sshUsername: "pat", remoteAddress: "203.0.113.9" });
101 // The hash gate is process-wide: a busy machine, or a hash another suite
102 // left running, can turn a guess away unhashed (and uncounted). That is
103 // not the window under test, so ask again until it is checked.
104 const deadline = Date.now() + DEADLINE_MS;
84105 for (let i = 0; i < TRIES; i++) {
85 // The hash gate is process-wide: a busy machine, or a hash another
86 // suite left running, can turn a guess away unhashed (and uncounted).
87 // That is not the window under test, so ask again until it is checked.
88 let wrong = await session.signIn("pat", `wrong-${i}`);
89 for (let again = 0; !wrong.ok && wrong.error === "throttled" && again < 50; again++) {
90 await Bun.sleep(100);
91 wrong = await session.signIn("pat", `wrong-${i}`);
92 }
106 const wrong = await askUntilCounted(
107 () => session.signIn("pat", `wrong-${i}`),
108 (r) => (r.ok ? "signed in" : r.error === "throttled" ? "gate" : r.error),
109 "bad_credentials",
110 deadline,
111 );
93112 expect(wrong.ok ? "signed in" : wrong.error).toBe("bad_credentials");
94113 }
95114 const right = await session.signIn("pat", PASSWORD);
96115 expect(right.ok ? "signed in" : right.error).toBe("throttled");
97 });
116 }, 30_000);
98117
99118 test("the doors count in one window: tries at /signin close the API door", async () => {
100119 const { url } = await forge();
@@ -105,23 +124,42 @@ describe("password doors share the sign-in windows", () => {
105124 // than a fixed number of asks, and fail loudly on any other answer (a 403
106125 // from the origin check would otherwise count nothing and pass for the
107126 // wrong reason).
108 const deadline = Date.now() + 20_000;
109 for (let i = 0; i < TRIES; i++) {
110 for (;;) {
111 const res = await fetch(`${url}/signin`, { method: "POST", body: new URLSearchParams({ username: "pat", password: `wrong-${i}` }), redirect: "manual" });
112 if (res.status === 422) break;
113 const gate = res.status === 429 && res.headers.get("retry-after") === String(GATE_RETRY_AFTER);
114 expect(gate ? "gate" : `status ${res.status}`).toBe("gate");
115 expect(Date.now()).toBeLessThan(deadline);
116 await Bun.sleep(50);
117 }
118 }
127 const deadline = Date.now() + DEADLINE_MS;
128 for (let i = 0; i < TRIES; i++)
129 await askUntilCounted(
130 () => fetch(`${url}/signin`, { method: "POST", body: new URLSearchParams({ username: "pat", password: `wrong-${i}` }), redirect: "manual" }),
131 (res) => (res.status === 422 ? "counted" : res.status === 429 && res.headers.get("retry-after") === String(GATE_RETRY_AFTER) ? "gate" : `status ${res.status}`),
132 "counted",
133 deadline,
134 );
119135 // The window, not the gate, closes the door: its Retry-After is the
120136 // window's wait, longer than the gate's one second.
121137 const door = await post(`${url}/api/v1/sessions`, { username: "pat", password: PASSWORD });
122138 expect(door.status).toBe(429);
123139 expect(Number(door.headers.get("retry-after"))).toBeGreaterThan(GATE_RETRY_AFTER);
124140 }, 30_000);
141
142 // The evidence for the cause (#4406): with every hash slot taken, which is
143 // what a loaded machine does to a try, a /signin post is turned away
144 // unhashed and uncounted, so tries made then leave the API door open. This
145 // is the state the old fixed-count loop could end in.
146 test("tries the hash gate turns away are not counted: they leave the API door open", async () => {
147 const { url } = await forge();
148 let release: () => void = () => {};
149 const held = new Promise<void>((resolve) => (release = resolve));
150 const holders = Array.from({ length: MAX_CONCURRENT_HASHES }, () => withHashSlot(() => held));
151 try {
152 for (let i = 0; i < TRIES; i++) {
153 const res = await fetch(`${url}/signin`, { method: "POST", body: new URLSearchParams({ username: "pat", password: `wrong-${i}` }), redirect: "manual" });
154 expect(res.status).toBe(429);
155 expect(res.headers.get("retry-after")).toBe(String(GATE_RETRY_AFTER));
156 }
157 } finally {
158 release();
159 await Promise.all(holders);
160 }
161 expect((await post(`${url}/api/v1/sessions`, { username: "pat", password: PASSWORD })).status).not.toBe(429);
162 });
125163 });
126164
127165 describe("no door verifies a password around the windows", () => {

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs