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 #16904

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 1 thread · 1 unresolved

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

Review

Sign in to review this change.
1 file changed +17 −4
test/signin-password-doors.test.ts +17 −4
@@ -4,6 +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";
78 import * as users from "../src/core/users.ts";
89 import { serve, type Servers } from "../src/http/server.ts";
910 import { setLimit } from "../src/settings/limits.ts";
@@ -98,16 +99,28 @@ describe("password doors share the sign-in windows", () => {
9899 test("the doors count in one window: tries at /signin close the API door", async () => {
99100 const { url } = await forge();
100101 // The process-wide hash gate refuses (and does not count) a try made while
101 // other hashes fill it, as a busy check machine can: a counted try answers
102 // 422, so repeat until three have been counted.
102 // other hashes fill it, as a busy check machine can: it answers 429 with
103 // Retry-After 1 (GATE_RETRY_AFTER), where a counted wrong password answers
104 // 422. Ask again until TRIES have been counted, against a deadline rather
105 // than a fixed number of asks, and fail loudly on any other answer (a 403
106 // from the origin check would otherwise count nothing and pass for the
107 // wrong reason).
108 const deadline = Date.now() + 20_000;
103109 for (let i = 0; i < TRIES; i++) {

The 20 s deadline is fine. The terminal test above still uses 50 x 100 ms asks; give it the same deadline pattern.

104 for (let attempt = 0; attempt < 50; attempt++) {
110 for (;;) {
105111 const res = await fetch(`${url}/signin`, { method: "POST", body: new URLSearchParams({ username: "pat", password: `wrong-${i}` }), redirect: "manual" });
106112 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);
107116 await Bun.sleep(50);
108117 }
109118 }
110 expect((await post(`${url}/api/v1/sessions`, { username: "pat", password: PASSWORD })).status).toBe(429);
119 // The window, not the gate, closes the door: its Retry-After is the
120 // window's wait, longer than the gate's one second.
121 const door = await post(`${url}/api/v1/sessions`, { username: "pat", password: PASSWORD });
122 expect(door.status).toBe(429);
123 expect(Number(door.headers.get("retry-after"))).toBeGreaterThan(GATE_RETRY_AFTER);
111124 }, 30_000);
112125 });
113126

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

Keyboard shortcuts

Docs