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
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/4406-tries-at-signin-close-the-api-door-passe → main 0 commits fork 1 thread · 1 unresolved
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"; | ||
| 4 | 4 | import { tmpdir } from "node:os"; |
| 5 | 5 | import { join } from "node:path"; |
| 6 | 6 | import { createApp, type App } from "../src/app.ts"; |
| 7 | import { GATE_RETRY_AFTER } from "../src/core/hash-gate.ts"; | |
| 7 | 8 | import * as users from "../src/core/users.ts"; |
| 8 | 9 | import { serve, type Servers } from "../src/http/server.ts"; |
| 9 | 10 | import { setLimit } from "../src/settings/limits.ts"; |
| @@ -98,16 +99,28 @@ describe("password doors share the sign-in windows", () => { | ||
| 98 | 99 | test("the doors count in one window: tries at /signin close the API door", async () => { |
| 99 | 100 | const { url } = await forge(); |
| 100 | 101 | // 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; | |
| 103 | 109 | for (let i = 0; i < TRIES; i++) { |
| 104 | for (let attempt = 0; attempt < 50; attempt++) { | |
| 110 | for (;;) { | |
| 105 | 111 | const res = await fetch(`${url}/signin`, { method: "POST", body: new URLSearchParams({ username: "pat", password: `wrong-${i}` }), redirect: "manual" }); |
| 106 | 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); | |
| 107 | 116 | await Bun.sleep(50); |
| 108 | 117 | } |
| 109 | 118 | } |
| 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); | |
| 111 | 124 | }, 30_000); |
| 112 | 125 | }); |
| 113 | 126 |
new above
offline
The 20 s deadline is fine. The terminal test above still uses 50 x 100 ms asks; give it the same deadline pattern.