Public: anyone can read this; the crew can change it.
A fresh account's right password does not carry its source past a full forge-wide sign-in window for other accounts #15128
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15028-a-fresh-account-s-right-password-does-no → main 1 commit fork
2 files
changed +38
−17
src/core/signin-window.ts +24 −17
| @@ -27,9 +27,12 @@ | ||
| 27 | 27 | * checked first, so a source past its ceiling creates no key for any |
| 28 | 28 | * account; the account name in a key is cut to `KEY_NAME_MAX`; a refund that |
| 29 | 29 | * empties a key deletes it; every limiter holds at most `MAX_KEYS`. And a |
| 30 | * source that proved a right password in the last `GOOD_SOURCE_MS` is not | |
| 31 | * refused by the forge-wide window alone: a handful of sources flooding it | |
| 32 | * shuts out strangers, never the people who signed in today. | |
| 30 | * source that proved an account's right password in the last | |
| 31 | * `GOOD_SOURCE_MS` is not refused by the forge-wide window alone *for that | |
| 32 | * account*: a handful of sources flooding it shuts out strangers, never the | |
| 33 | * people who signed in today. The vouch is by account and source (#15028): | |
| 34 | * a source that signs into an account it made itself a minute ago is not | |
| 35 | * thereby free of the forge window for every other account. | |
| 33 | 36 | */ |
| 34 | 37 | |
| 35 | 38 | import type { Database } from "bun:sqlite"; |
| @@ -43,7 +46,7 @@ const accounts = new WeakMap<Database, RateLimiter>(); | ||
| 43 | 46 | const sources = new WeakMap<Database, RateLimiter>(); |
| 44 | 47 | // The slow window: wrong passwords at one account from one source. |
| 45 | 48 | const failures = new WeakMap<Database, RateLimiter>(); |
| 46 | // Sources that proved a right password: bucket → when, oldest first. | |
| 49 | // Account-and-source keys that proved a right password: key → when, oldest first. | |
| 47 | 50 | const goodSources = new WeakMap<Database, Map<string, number>>(); |
| 48 | 51 | // The clock the last attempt at a database read, so a test can move time. |
| 49 | 52 | const clocks = new WeakMap<Database, () => number>(); |
| @@ -68,7 +71,7 @@ export const KEY_NAME_MAX = 64; | ||
| 68 | 71 | * signed in this morning is let past a forge-wide flood this afternoon. |
| 69 | 72 | */ |
| 70 | 73 | export const GOOD_SOURCE_MS = 24 * 60 * 60_000; |
| 71 | /** Sources remembered as good at most; past it the oldest is forgotten. */ | |
| 74 | /** Account-and-source pairs remembered as good at most; past it the oldest is forgotten. */ | |
| 72 | 75 | export const MAX_GOOD_SOURCES = 10_000; |
| 73 | 76 | |
| 74 | 77 | /** |
| @@ -98,23 +101,26 @@ function goodOf(db: Database): Map<string, number> { | ||
| 98 | 101 | return perOwner(goodSources, db, () => new Map()); |
| 99 | 102 | } |
| 100 | 103 | |
| 101 | /** Remember that `bucket` proved a right password now; past `cap`, forget the oldest. */ | |
| 102 | function vouch(db: Database, bucket: string, cap: number): void { | |
| 104 | /** | |
| 105 | * Remember that `key` (account and source) proved a right password now; past `cap`, forget the | |
| 106 | * oldest. | |
| 107 | */ | |
| 108 | function vouch(db: Database, key: string, cap: number): void { | |
| 103 | 109 | const good = goodOf(db); |
| 104 | good.delete(bucket); | |
| 105 | good.set(bucket, nowOf(db)); | |
| 110 | good.delete(key); | |
| 111 | good.set(key, nowOf(db)); | |
| 106 | 112 | for (const oldest of good.keys()) { |
| 107 | 113 | if (good.size <= cap) break; |
| 108 | 114 | good.delete(oldest); |
| 109 | 115 | } |
| 110 | 116 | } |
| 111 | 117 | |
| 112 | /** Whether `bucket` proved a right password within `GOOD_SOURCE_MS`. */ | |
| 113 | function vouched(db: Database, bucket: string): boolean { | |
| 114 | const at = goodOf(db).get(bucket); | |
| 118 | /** Whether `key` (account and source) proved a right password within `GOOD_SOURCE_MS`. */ | |
| 119 | function vouched(db: Database, key: string): boolean { | |
| 120 | const at = goodOf(db).get(key); | |
| 115 | 121 | if (at === undefined) return false; |
| 116 | 122 | if (nowOf(db) - at < GOOD_SOURCE_MS) return true; |
| 117 | goodOf(db).delete(bucket); | |
| 123 | goodOf(db).delete(key); | |
| 118 | 124 | return false; |
| 119 | 125 | } |
| 120 | 126 | |
| @@ -223,13 +229,14 @@ export function admitSignin( | ||
| 223 | 229 | taken.push(() => limiter.refund(accountKey)); |
| 224 | 230 | // The operator's door (a passkey: a signature cannot be guessed) skips the |
| 225 | 231 | // forge-wide window, so a flood of password guesses cannot lock it out. |
| 226 | // A source with a right password today is let past a full forge window | |
| 227 | // too, uncounted, still held by its own and its account's windows. | |
| 232 | // A source with this account's right password today is let past a full | |
| 233 | // forge window too, uncounted, still held by its own and its account's | |
| 234 | // windows; for any other account it meets the forge window like a stranger. | |
| 228 | 235 | let counted = options.forgeWide !== false; |
| 229 | 236 | if (counted) { |
| 230 | 237 | const whole = limiter.take("forge", "admin"); |
| 231 | 238 | if (!whole.allowed) { |
| 232 | if (!vouched(db, bucket)) return refused(whole.retryAfter); | |
| 239 | if (!vouched(db, accountKey)) return refused(whole.retryAfter); | |
| 233 | 240 | counted = false; |
| 234 | 241 | } |
| 235 | 242 | } |
| @@ -243,7 +250,7 @@ export function admitSignin( | ||
| 243 | 250 | succeeded: () => { |
| 244 | 251 | giveBack(); |
| 245 | 252 | slow?.forget(accountKey); |
| 246 | vouch(db, bucket, maxGood); | |
| 253 | vouch(db, accountKey, maxGood); | |
| 247 | 254 | }, |
| 248 | 255 | notJudged: () => { |
| 249 | 256 | giveBack(); |
test/signin-window-bounds.test.ts +14
| @@ -132,3 +132,17 @@ test("a flood of distinct accounts and sources leaves the sign-in limiters at th | ||
| 132 | 132 | expect(signinKeyCount(app.db)).toBeGreaterThan(0); |
| 133 | 133 | expect(signinKeyCount(app.db)).toBeLessThanOrEqual(3 * maxKeys); |
| 134 | 134 | }); |
| 135 | ||
| 136 | test("a source good for one account meets the full forge window for any other (#15028)", () => { | |
| 137 | // A fresh account, made and signed into by the source itself a minute ago. | |
| 138 | signIn("fresh-sock", "198.51.100.40"); | |
| 139 | // The owner signed in this morning from their own source. | |
| 140 | signIn("owner", "203.0.113.60"); | |
| 141 | fillForge(); | |
| 142 | // The fresh account's right password buys the source nothing at another account … | |
| 143 | expect(admitSignin(app.db, "victim", "198.51.100.40").admitted).toBe(false); | |
| 144 | expect(admitSignin(app.db, "owner", "198.51.100.40").admitted).toBe(false); | |
| 145 | // … it is still let in to its own, and the owner to theirs. | |
| 146 | expect(admitSignin(app.db, "fresh-sock", "198.51.100.40").admitted).toBe(true); | |
| 147 | expect(admitSignin(app.db, "owner", "203.0.113.60").admitted).toBe(true); | |
| 148 | }); |
new above
offline