Public: anyone can read this; the crew can change it.
Repeated wrong passwords for one username are slowed or refused with a 429 and Retry-After: twelve in a row answer 422 each on production #14384
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14373-repeated-wrong-passwords-for-one-usernam → main 2 commits fork 1 thread · 1 unresolved
9 files
changed +285
−24
docs/THREATS.md +1 −1
| @@ -47,7 +47,7 @@ Wants: to act as the person or agent it was issued to, for as long as it works. | ||
| 47 | 47 | - A passport (`fp1.…`) is bound to one audience and scope set and dies in |
| 48 | 48 | minutes, so a copy lifted from a run is useful to one door briefly. |
| 49 | 49 | Enforced by: `test/passport.test.ts` |
| 50 | - Sign-in attempts are throttled per client, with a whole-forge cap beside it. | |
| 50 | - Sign-in attempts are throttled per client, with a whole-forge cap beside it; eight wrong passwords at one account from one client in ten minutes shut that pair until the window passes (`src/core/signin-window.ts`, #14373). | |
| 51 | 51 | Enforced by: `test/signin-throttle.test.ts` |
| 52 | 52 | - Stored provider tokens (origins) are sealed under a key in `secret.key`, never |
| 53 | 53 | returned by any route. |
src/core/ratelimit.ts +5
| @@ -169,6 +169,11 @@ export class RateLimiter { | ||
| 169 | 169 | times.pop(); |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | /** Forget every request counted on `key`: its window starts empty. */ | |
| 173 | forget(key: string): void { | |
| 174 | this.hits.delete(key); | |
| 175 | } | |
| 176 | ||
| 172 | 177 | /** |
| 173 | 178 | * Forget keys whose whole window has passed. Without this the map grows by |
| 174 | 179 | * one entry per token that ever spoke to the forge — small, but unbounded |
src/core/signin-window.ts +75 −10
| @@ -13,6 +13,15 @@ | ||
| 13 | 13 | * passkey, claim and parent doors with a key of their own. The passkey door |
| 14 | 14 | * is the operator's way in and is not counted in the forge-wide window, so |
| 15 | 15 | * guessing passwords at every account cannot shut it. |
| 16 | * | |
| 17 | * Under the minute windows sits a slower one for the password doors: wrong | |
| 18 | * passwords at one account from one source (`FAILURES_PER_ACCOUNT` in | |
| 19 | * `FAILURES_WINDOW_MS`). Twenty a minute is a hundred-odd guesses an hour per | |
| 20 | * address; eight in ten minutes is a person mistyping, not a script (#14373). | |
| 21 | * While it is full every attempt there is refused, the right password too, | |
| 22 | * so a guesser learns nothing from which answers are 429. A right password | |
| 23 | * clears it. Keyed by account *and* source like the minute window, so a | |
| 24 | * stranger fills only their own and the owner, elsewhere, is never locked out. | |
| 16 | 25 | */ |
| 17 | 26 | |
| 18 | 27 | import type { Database } from "bun:sqlite"; |
| @@ -23,6 +32,19 @@ import { limitValue } from "../settings/limits.ts"; | ||
| 23 | 32 | const accounts = new WeakMap<Database, RateLimiter>(); |
| 24 | 33 | // The third window, by the socket's peer: a source trying many accounts. |
| 25 | 34 | const sources = new WeakMap<Database, RateLimiter>(); |
| 35 | // The slow window: wrong passwords at one account from one source. | |
| 36 | const failures = new WeakMap<Database, RateLimiter>(); | |
| 37 | ||
| 38 | /** | |
| 39 | * Wrong passwords one account may take from one source in `FAILURES_WINDOW_MS`: more than a person | |
| 40 | * mistypes. | |
| 41 | */ | |
| 42 | export const FAILURES_PER_ACCOUNT = 8; | |
| 43 | /** | |
| 44 | * Ten minutes: long enough that guessing is not worth it, short enough that an owner who forgot is | |
| 45 | * not kept out for long. | |
| 46 | */ | |
| 47 | export const FAILURES_WINDOW_MS = 10 * 60_000; | |
| 26 | 48 | |
| 27 | 49 | function limiterOf(map: WeakMap<Database, RateLimiter>, db: Database): RateLimiter { |
| 28 | 50 | let limiter = map.get(db); |
| @@ -33,13 +55,35 @@ function limiterOf(map: WeakMap<Database, RateLimiter>, db: Database): RateLimit | ||
| 33 | 55 | return limiter; |
| 34 | 56 | } |
| 35 | 57 | |
| 58 | function failuresOf(db: Database): RateLimiter { | |
| 59 | let limiter = failures.get(db); | |
| 60 | if (limiter === undefined) { | |
| 61 | limiter = new RateLimiter({ | |
| 62 | perMinute: FAILURES_PER_ACCOUNT, | |
| 63 | adminPerMinute: 0, | |
| 64 | enabled: true, | |
| 65 | windowMs: FAILURES_WINDOW_MS, | |
| 66 | }); | |
| 67 | failures.set(db, limiter); | |
| 68 | } | |
| 69 | return limiter; | |
| 70 | } | |
| 71 | ||
| 36 | 72 | /** The answer to one attempt: refused with a wait, or let through. */ |
| 37 | 73 | export type SigninAdmission = |
| 38 | 74 | | { readonly admitted: false; readonly retryAfter: number } |
| 39 | 75 | | { |
| 40 | 76 | readonly admitted: true; |
| 41 | /** The secret proved right: give the attempt back to every window. */ | |
| 77 | /** | |
| 78 | * The secret proved right: give the attempt back, and forget this account's wrong ones from | |
| 79 | * this source. | |
| 80 | */ | |
| 42 | 81 | readonly succeeded: () => void; |
| 82 | /** | |
| 83 | * The secret was never judged (no hash ran, or the account waits): give the attempt back to | |
| 84 | * every window. | |
| 85 | */ | |
| 86 | readonly notJudged: () => void; | |
| 43 | 87 | }; |
| 44 | 88 | |
| 45 | 89 | /** |
| @@ -53,7 +97,7 @@ export function admitSignin( | ||
| 53 | 97 | db: Database, |
| 54 | 98 | account: string, |
| 55 | 99 | address: string, |
| 56 | options: { readonly forgeWide?: boolean } = {}, | |
| 100 | options: { readonly forgeWide?: boolean; readonly failures?: boolean } = {}, | |
| 57 | 101 | ): SigninAdmission { |
| 58 | 102 | const limiter = limiterOf(accounts, db); |
| 59 | 103 | // Read on every attempt, so a change on /admin/limits governs the next one. |
| @@ -66,26 +110,45 @@ export function admitSignin( | ||
| 66 | 110 | // own window, never the owner's, so guessing cannot be turned into a lockout. |
| 67 | 111 | const accountKey = `account:${account.toLowerCase()}|${bucketOf(address)}`; |
| 68 | 112 | // The one key first: a key already refused must not spend the forge's budget. |
| 113 | // The slow window first, so an account shut there spends no minute budget. | |
| 114 | const slow = options.failures === false ? undefined : failuresOf(db); | |
| 115 | if (slow !== undefined) { | |
| 116 | const failed = slow.take(accountKey, "ro"); | |
| 117 | if (!failed.allowed) return { admitted: false, retryAfter: failed.retryAfter }; | |
| 118 | } | |
| 119 | const refused = (retryAfter: number): SigninAdmission => { | |
| 120 | // A refused attempt is not counted, in the slow window either. | |
| 121 | slow?.refund(accountKey); | |
| 122 | return { admitted: false, retryAfter }; | |
| 123 | }; | |
| 69 | 124 | const one = limiter.take(accountKey, "ro"); |
| 70 | if (!one.allowed) return { admitted: false, retryAfter: one.retryAfter }; | |
| 125 | if (!one.allowed) return refused(one.retryAfter); | |
| 71 | 126 | const peers = limiterOf(sources, db); |
| 72 | 127 | peers.setLimits({ perMinute: limitValue(db, "signin.per_source"), adminPerMinute: 0, enabled: true }); |
| 73 | 128 | const sourceKey = `source:${bucketOf(address)}`; |
| 74 | 129 | const source = peers.take(sourceKey, "ro"); |
| 75 | if (!source.allowed) return { admitted: false, retryAfter: source.retryAfter }; | |
| 130 | if (!source.allowed) return refused(source.retryAfter); | |
| 76 | 131 | // The operator's door (a passkey: a signature cannot be guessed) skips the |
| 77 | 132 | // forge-wide window, so a flood of password guesses cannot lock it out. |
| 78 | 133 | const counted = options.forgeWide !== false; |
| 79 | 134 | if (counted) { |
| 80 | 135 | const whole = limiter.take("forge", "admin"); |
| 81 | if (!whole.allowed) return { admitted: false, retryAfter: whole.retryAfter }; | |
| 136 | if (!whole.allowed) return refused(whole.retryAfter); | |
| 82 | 137 | } |
| 138 | const giveBack = () => { | |
| 139 | limiter.refund(accountKey); | |
| 140 | peers.refund(sourceKey); | |
| 141 | if (counted) limiter.refund("forge"); | |
| 142 | }; | |
| 83 | 143 | return { |
| 84 | 144 | admitted: true, |
| 85 | 145 | succeeded: () => { |
| 86 | limiter.refund(accountKey); | |
| 87 | peers.refund(sourceKey); | |
| 88 | if (counted) limiter.refund("forge"); | |
| 146 | giveBack(); | |
| 147 | slow?.forget(accountKey); | |
| 148 | }, | |
| 149 | notJudged: () => { | |
| 150 | giveBack(); | |
| 151 | slow?.refund(accountKey); | |
| 89 | 152 | }, |
| 90 | 153 | }; |
| 91 | 154 | } |
| @@ -93,7 +156,9 @@ export function admitSignin( | ||
| 93 | 156 | /** |
| 94 | 157 | * Count one sign-in attempt, kept whatever it proves: for the doors whose |
| 95 | 158 | * secret is checked elsewhere (passkey, claim, parent). Seconds to wait when |
| 96 | * refused, or undefined when the attempt may go on. | |
| 159 | * refused, or undefined when the attempt may go on. These doors cannot say | |
| 160 | * whether the secret was wrong, so the slow window of wrong passwords is not | |
| 161 | * theirs. | |
| 97 | 162 | */ |
| 98 | 163 | export function signinWindow( |
| 99 | 164 | db: Database, |
| @@ -101,6 +166,6 @@ export function signinWindow( | ||
| 101 | 166 | address: string, |
| 102 | 167 | options: { readonly forgeWide?: boolean } = {}, |
| 103 | 168 | ): number | undefined { |
| 104 | const admission = admitSignin(db, account, address, options); | |
| 169 | const admission = admitSignin(db, account, address, { ...options, failures: false }); | |
| 105 | 170 | return admission.admitted ? undefined : admission.retryAfter; |
| 106 | 171 | } |
src/core/users.ts +2 −2
| @@ -574,11 +574,11 @@ export async function verifyPasswordThrottled( | ||
| 574 | 574 | ); |
| 575 | 575 | if (checked === undefined) { |
| 576 | 576 | // Never hashed, so it is not a guess: give it back and ask for a moment. |
| 577 | admission.succeeded(); | |
| 577 | admission.notJudged(); | |
| 578 | 578 | return err({ reason: "throttled", retryAfter: GATE_RETRY_AFTER }); |
| 579 | 579 | } |
| 580 | 580 | if (!checked.ok && checked.error === "waiting") { |
| 581 | admission.succeeded(); | |
| 581 | admission.notJudged(); | |
| 582 | 582 | return err({ reason: "waiting", username: attempt.username }); |
| 583 | 583 | } |
| 584 | 584 | if (!checked.ok) return err({ reason: "bad_credentials" }); |
test/origins-sync.test.ts +14
| @@ -14,6 +14,7 @@ import { | ||
| 14 | 14 | type FakeForge, |
| 15 | 15 | } from "./fake-origin.ts"; |
| 16 | 16 | import { seedAdmin } from "./helpers/admin.ts"; |
| 17 | import { createUser } from "../src/core/users.ts"; | |
| 17 | 18 | |
| 18 | 19 | /** |
| 19 | 20 | * The sync loop, against a fake that speaks GitHub's dialect — because the |
| @@ -92,6 +93,19 @@ describe("origin sync: incremental, event-shaped, and polite", () => { | ||
| 92 | 93 | }), |
| 93 | 94 | }); |
| 94 | 95 | |
| 96 | // The GitHub provider fetches with `x-access-token:<token>`, GitHub's | |
| 97 | // convention. Here a forge stands in for GitHub's git host, so it needs | |
| 98 | // that account too: an unknown name would count as a wrong password on | |
| 99 | // every fetch and fill the sign-in failures window (#14373). | |
| 100 | const gitAccount = await createUser(originApp.db, { | |
| 101 | origin: "seed", | |
| 102 | trusted: true, | |
| 103 | username: "x-access-token", | |
| 104 | email: "[email protected]", | |
| 105 | password: TOKEN, | |
| 106 | }); | |
| 107 | if (!gitAccount.ok) throw new Error(gitAccount.error); | |
| 108 | ||
| 95 | 109 | const state = newFakeState("github", LOGIN, TOKEN); |
| 96 | 110 | state.repos.push({ |
| 97 | 111 | id: 5, |
test/perimeter-mediums.test.ts +3 −2
| @@ -3,7 +3,7 @@ import { mkdtemp } from "node:fs/promises"; | ||
| 3 | 3 | import { tmpdir } from "node:os"; |
| 4 | 4 | import { join } from "node:path"; |
| 5 | 5 | import { createApp, type App } from "../src/app.ts"; |
| 6 | import { admitSignin } from "../src/core/signin-window.ts"; | |
| 6 | import { admitSignin, FAILURES_PER_ACCOUNT } from "../src/core/signin-window.ts"; | |
| 7 | 7 | import { limitValue } from "../src/settings/limits.ts"; |
| 8 | 8 | import { createToken } from "../src/core/tokens.ts"; |
| 9 | 9 | import { renderMarkdown } from "../src/web/markdown.ts"; |
| @@ -28,7 +28,8 @@ describe("perimeter mediums (#2922)", () => { | ||
| 28 | 28 | }); |
| 29 | 29 | |
| 30 | 30 | test("M1: a stranger exhausting an account's window does not lock out its owner", () => { |
| 31 | const limit = limitValue(app.db, "signin.per_account"); | |
| 31 | // Whichever window shuts first: the minute's, or eight wrong in ten minutes (#14373). | |
| 32 | const limit = Math.min(limitValue(app.db, "signin.per_account"), FAILURES_PER_ACCOUNT); | |
| 32 | 33 | for (let i = 0; i < limit; i++) expect(admitSignin(app.db, "olive", "203.0.113.9").admitted).toBe(true); |
| 33 | 34 | expect(admitSignin(app.db, "olive", "203.0.113.9").admitted).toBe(false); |
| 34 | 35 | expect(admitSignin(app.db, "olive", "198.51.100.4").admitted).toBe(true); |
test/signin-failures-window.test.ts added +171
| @@ -0,0 +1,171 @@ | ||
| 1 | /** | |
| 2 | * Wrong passwords at one account from one source are a slow window of their | |
| 3 | * own (#14373): eight in ten minutes, then every attempt there, the right | |
| 4 | * password too, answers 429 with an integer Retry-After until the window | |
| 5 | * passes. A right password clears it; an unknown name is counted the same | |
| 6 | * as a real one; another source and another account are not touched. | |
| 7 | */ | |
| 8 | import { afterAll, beforeAll, describe, expect, setSystemTime, test } from "bun:test"; | |
| 9 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { admitSignin, FAILURES_PER_ACCOUNT, FAILURES_WINDOW_MS, signinWindow } from "../src/core/signin-window.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | ||
| 17 | const PASSWORD = "a-good-passphrase"; | |
| 18 | ||
| 19 | async function withUsers(app: App, names: readonly string[]): Promise<void> { | |
| 20 | // The cheapest argon2id (verify reads the cost from the hash), so a test's | |
| 21 | // dozens of attempts stay milliseconds. | |
| 22 | const cheap = await Bun.password.hash(PASSWORD, { algorithm: "argon2id", memoryCost: 8, timeCost: 1 }); | |
| 23 | for (const [i, username] of names.entries()) { | |
| 24 | const made = await createUser(app.db, { | |
| 25 | origin: "seed", | |
| 26 | trusted: true, | |
| 27 | username, | |
| 28 | email: `${username}@forge.local`, | |
| 29 | password: PASSWORD, | |
| 30 | adminIfFirst: i === 0, | |
| 31 | }); | |
| 32 | if (!made.ok) throw new Error(made.error); | |
| 33 | app.db.run("UPDATE user SET password_hash = ?1 WHERE username = ?2", [cheap, username]); | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | describe("the limiter, by client address", () => { | |
| 38 | let dataDir = ""; | |
| 39 | let app: App; | |
| 40 | beforeAll(async () => { | |
| 41 | dataDir = await mkdtemp(join(tmpdir(), "forge-failures-")); | |
| 42 | app = createApp({ dataDir, port: 0 }); | |
| 43 | }); | |
| 44 | afterAll(async () => { | |
| 45 | setSystemTime(); | |
| 46 | app.close(); | |
| 47 | await rm(dataDir, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | test("the window and count are small and slow", () => { | |
| 51 | expect(FAILURES_PER_ACCOUNT).toBe(8); | |
| 52 | expect(FAILURES_WINDOW_MS).toBe(10 * 60_000); | |
| 53 | }); | |
| 54 | ||
| 55 | test("eight wrong at one account from one address shut it there for the window, and only there", () => { | |
| 56 | setSystemTime(new Date("2026-11-19T10:00:00Z")); | |
| 57 | for (let i = 0; i < FAILURES_PER_ACCOUNT; i++) | |
| 58 | expect(admitSignin(app.db, "dana", "203.0.113.5").admitted).toBe(true); | |
| 59 | // Spaced over minutes: the minute windows have long emptied. | |
| 60 | setSystemTime(new Date("2026-11-19T10:05:00Z")); | |
| 61 | const shut = admitSignin(app.db, "dana", "203.0.113.5"); | |
| 62 | expect(shut.admitted).toBe(false); | |
| 63 | const wait = shut.admitted ? 0 : shut.retryAfter; | |
| 64 | expect(Number.isInteger(wait)).toBe(true); | |
| 65 | expect(wait).toBeGreaterThan(4 * 60); | |
| 66 | expect(wait).toBeLessThanOrEqual(5 * 60); | |
| 67 | // Another address, and another account at this one, have their own. | |
| 68 | expect(admitSignin(app.db, "dana", "203.0.113.6").admitted).toBe(true); | |
| 69 | expect(admitSignin(app.db, "erin", "203.0.113.5").admitted).toBe(true); | |
| 70 | // The window passes, and the door opens. | |
| 71 | setSystemTime(new Date("2026-11-19T10:10:01Z")); | |
| 72 | expect(admitSignin(app.db, "dana", "203.0.113.5").admitted).toBe(true); | |
| 73 | setSystemTime(); | |
| 74 | }); | |
| 75 | ||
| 76 | test("a right password clears the account's count from that address", () => { | |
| 77 | for (let i = 0; i < FAILURES_PER_ACCOUNT - 1; i++) | |
| 78 | expect(admitSignin(app.db, "fay", "198.51.100.3").admitted).toBe(true); | |
| 79 | const right = admitSignin(app.db, "fay", "198.51.100.3"); | |
| 80 | if (!right.admitted) throw new Error("refused before the limit"); | |
| 81 | right.succeeded(); | |
| 82 | for (let i = 0; i < FAILURES_PER_ACCOUNT; i++) | |
| 83 | expect(admitSignin(app.db, "fay", "198.51.100.3").admitted).toBe(true); | |
| 84 | expect(admitSignin(app.db, "fay", "198.51.100.3").admitted).toBe(false); | |
| 85 | }); | |
| 86 | ||
| 87 | test("the doors that count every attempt (passkey, claim) are not held to wrong passwords", () => { | |
| 88 | for (let i = 0; i < FAILURES_PER_ACCOUNT + 4; i++) | |
| 89 | expect(signinWindow(app.db, "passkey:192.0.2.4", "192.0.2.4", { forgeWide: false })).toBeUndefined(); | |
| 90 | }); | |
| 91 | }); | |
| 92 | ||
| 93 | describe("POST /signin past the limit", () => { | |
| 94 | let dataDir = ""; | |
| 95 | let app: App; | |
| 96 | let servers: Servers; | |
| 97 | const attempt = (username: string, password: string) => | |
| 98 | fetch(`${servers.url}/signin`, { | |
| 99 | method: "POST", | |
| 100 | headers: { origin: servers.url }, | |
| 101 | body: new URLSearchParams({ username, password }), | |
| 102 | redirect: "manual", | |
| 103 | }); | |
| 104 | ||
| 105 | beforeAll(async () => { | |
| 106 | dataDir = await mkdtemp(join(tmpdir(), "forge-failures-http-")); | |
| 107 | app = createApp({ dataDir, port: 0 }); | |
| 108 | servers = await serve(app); | |
| 109 | await withUsers(app, ["alice", "bob", "carol"]); | |
| 110 | }); | |
| 111 | afterAll(async () => { | |
| 112 | servers.stop(); | |
| 113 | app.close(); | |
| 114 | await rm(dataDir, { recursive: true, force: true }); | |
| 115 | }); | |
| 116 | ||
| 117 | test("the ninth wrong password answers 429 with Retry-After, the right one too; another username signs in", async () => { | |
| 118 | for (let i = 0; i < FAILURES_PER_ACCOUNT; i++) { | |
| 119 | const r = await attempt("alice", `wrong-${i}`); | |
| 120 | expect(r.status).toBe(422); | |
| 121 | expect(await r.text()).toContain("That username and password"); | |
| 122 | } | |
| 123 | const shut = await attempt("alice", "wrong-again"); | |
| 124 | expect(shut.status).toBe(429); | |
| 125 | expect(shut.headers.get("retry-after")).toMatch(/^[1-9]\d*$/); | |
| 126 | expect(await shut.text()).toContain("Too many attempts"); | |
| 127 | const right = await attempt("alice", PASSWORD); | |
| 128 | expect(right.status).toBe(429); | |
| 129 | expect(right.headers.get("retry-after")).toMatch(/^[1-9]\d*$/); | |
| 130 | expect((await attempt("ALICE", PASSWORD)).status).toBe(429); | |
| 131 | const bob = await attempt("bob", PASSWORD); | |
| 132 | expect(bob.status).toBe(303); | |
| 133 | expect(bob.headers.get("set-cookie")).toContain("forge_session="); | |
| 134 | }); | |
| 135 | ||
| 136 | test("an unknown username is answered the same: 422 with the same words, then 429", async () => { | |
| 137 | // An unknown name is verified against the full-cost decoy hash, a | |
| 138 | // quarter second each; what is under test is the window, not the decoy's | |
| 139 | // cost, so its verify stands in for a cheap one here (it still answers no). | |
| 140 | const verify = Bun.password.verify; | |
| 141 | const cheap = await Bun.password.hash("not-the-decoy", { algorithm: "argon2id", memoryCost: 8, timeCost: 1 }); | |
| 142 | let decoys = 0; | |
| 143 | Bun.password.verify = (password, hash, algorithm) => { | |
| 144 | if (typeof hash === "string" && hash.startsWith("$argon2id$v=19$m=65536,t=2,p=1$AAAAAAAAAAAAAAAAAAAAAA$")) { | |
| 145 | decoys++; | |
| 146 | return verify(password, cheap, algorithm); | |
| 147 | } | |
| 148 | return verify(password, hash, algorithm); | |
| 149 | }; | |
| 150 | try { | |
| 151 | for (let i = 0; i < FAILURES_PER_ACCOUNT; i++) { | |
| 152 | const r = await attempt("nobody-here", `wrong-${i}`); | |
| 153 | expect(r.status).toBe(422); | |
| 154 | expect(await r.text()).toContain("That username and password"); | |
| 155 | } | |
| 156 | } finally { | |
| 157 | Bun.password.verify = verify; | |
| 158 | } | |
| 159 | expect(decoys).toBe(FAILURES_PER_ACCOUNT); | |
| 160 | const shut = await attempt("nobody-here", "wrong-again"); | |
| 161 | expect(shut.status).toBe(429); | |
| 162 | expect(shut.headers.get("retry-after")).toMatch(/^[1-9]\d*$/); | |
| 163 | }); | |
| 164 | ||
| 165 | test("a success clears the username's count", async () => { | |
| 166 | for (let i = 0; i < FAILURES_PER_ACCOUNT - 1; i++) expect((await attempt("carol", `wrong-${i}`)).status).toBe(422); | |
| 167 | expect((await attempt("carol", PASSWORD)).status).toBe(303); | |
| 168 | for (let i = 0; i < FAILURES_PER_ACCOUNT; i++) expect((await attempt("carol", `again-${i}`)).status).toBe(422); | |
| 169 | expect((await attempt("carol", "once-more")).status).toBe(429); | |
| 170 | }); | |
| 171 | }); |
test/signin-throttle.test.ts +5 −4
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | /** |
| 2 | * Guessing a password is throttled: an account may be tried twenty times a | |
| 3 | * minute, the forge two hundred, checked before the hash runs. A refused | |
| 2 | * Guessing a password is throttled: an account may be tried wrong eight | |
| 3 | * times in ten minutes from one source (#14373), twenty times a minute, the | |
| 4 | * forge two hundred, checked before the hash runs. A refused | |
| 4 | 5 | * attempt answers 429 with Retry-After and is not counted, and the right |
| 5 | 6 | * password still works once the window has slid. |
| 6 | 7 | */ |
| @@ -55,8 +56,8 @@ describe("sign-in attempts are throttled", () => { | ||
| 55 | 56 | await rm(dataDir, { recursive: true, force: true }); |
| 56 | 57 | }); |
| 57 | 58 | |
| 58 | test("twenty wrong guesses at one account, then the door closes for a minute", async () => { | |
| 59 | for (let i = 0; i < 20; i++) { | |
| 59 | test("eight wrong guesses at one account, then the door closes", async () => { | |
| 60 | for (let i = 0; i < 8; i++) { | |
| 60 | 61 | const r = await attempt("alice", `wrong-${i}`); |
| 61 | 62 | expect(r.status).toBe(422); |
| 62 | 63 | } |
test/signin-window-limits.test.ts +9 −5
| @@ -17,15 +17,19 @@ afterAll(async () => { | ||
| 17 | 17 | await rm(dataDir, { recursive: true, force: true }); |
| 18 | 18 | }); |
| 19 | 19 | |
| 20 | // The minute window alone: the slower window of wrong passwords (eight in | |
| 21 | // ten minutes, #14373) is `test/signin-failures-window.test.ts`'s. | |
| 22 | const minuteOnly = { failures: false } as const; | |
| 23 | ||
| 20 | 24 | test("one account is refused after twenty attempts from one address; another address has its own window", () => { |
| 21 | for (let i = 0; i < 20; i++) expect(admitSignin(app.db, "carol", "203.0.113.1").admitted).toBe(true); | |
| 22 | expect(admitSignin(app.db, "carol", "203.0.113.1").admitted).toBe(false); | |
| 23 | expect(admitSignin(app.db, "carol", "203.0.113.9").admitted).toBe(true); | |
| 25 | for (let i = 0; i < 20; i++) expect(admitSignin(app.db, "carol", "203.0.113.1", minuteOnly).admitted).toBe(true); | |
| 26 | expect(admitSignin(app.db, "carol", "203.0.113.1", minuteOnly).admitted).toBe(false); | |
| 27 | expect(admitSignin(app.db, "carol", "203.0.113.9", minuteOnly).admitted).toBe(true); | |
| 24 | 28 | }); |
| 25 | 29 | |
| 26 | 30 | test("one address is refused after sixty attempts across accounts", () => { |
| 27 | for (let i = 0; i < 60; i++) expect(admitSignin(app.db, `user${i}`, "198.51.100.7").admitted).toBe(true); | |
| 28 | const shut = admitSignin(app.db, "user-new", "198.51.100.7"); | |
| 31 | for (let i = 0; i < 60; i++) expect(admitSignin(app.db, `user${i}`, "198.51.100.7", minuteOnly).admitted).toBe(true); | |
| 32 | const shut = admitSignin(app.db, "user-new", "198.51.100.7", minuteOnly); | |
| 29 | 33 | expect(shut.admitted).toBe(false); |
| 30 | 34 | expect(!shut.admitted && shut.retryAfter).toBeGreaterThan(0); |
| 31 | 35 | }); |
new above
offline
Two consecutive comments now disagree about which window goes first. Delete the old line.