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

Merged opened this

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

Merged

torque/forge:torque/15028-a-fresh-account-s-right-password-does-no → main 1 commit fork

  • Merged. torque/15028-a-fresh-account-s-right-password-does-no was merged into main as e0c5b8b.

Review

Sign in to review this change.
2 files changed +38 −17
src/core/signin-window.ts +24 −17
@@ -27,9 +27,12 @@
2727 * checked first, so a source past its ceiling creates no key for any
2828 * account; the account name in a key is cut to `KEY_NAME_MAX`; a refund that
2929 * 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.
3336 */
3437
3538 import type { Database } from "bun:sqlite";
@@ -43,7 +46,7 @@ const accounts = new WeakMap<Database, RateLimiter>();
4346 const sources = new WeakMap<Database, RateLimiter>();
4447 // The slow window: wrong passwords at one account from one source.
4548 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.
4750 const goodSources = new WeakMap<Database, Map<string, number>>();
4851 // The clock the last attempt at a database read, so a test can move time.
4952 const clocks = new WeakMap<Database, () => number>();
@@ -68,7 +71,7 @@ export const KEY_NAME_MAX = 64;
6871 * signed in this morning is let past a forge-wide flood this afternoon.
6972 */
7073 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. */
7275 export const MAX_GOOD_SOURCES = 10_000;
7376
7477 /**
@@ -98,23 +101,26 @@ function goodOf(db: Database): Map<string, number> {
98101 return perOwner(goodSources, db, () => new Map());
99102 }
100103
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 {
103109 const good = goodOf(db);
104 good.delete(bucket);
105 good.set(bucket, nowOf(db));
110 good.delete(key);
111 good.set(key, nowOf(db));
106112 for (const oldest of good.keys()) {
107113 if (good.size <= cap) break;
108114 good.delete(oldest);
109115 }
110116 }
111117
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);
115121 if (at === undefined) return false;
116122 if (nowOf(db) - at < GOOD_SOURCE_MS) return true;
117 goodOf(db).delete(bucket);
123 goodOf(db).delete(key);
118124 return false;
119125 }
120126
@@ -223,13 +229,14 @@ export function admitSignin(
223229 taken.push(() => limiter.refund(accountKey));
224230 // The operator's door (a passkey: a signature cannot be guessed) skips the
225231 // 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.
228235 let counted = options.forgeWide !== false;
229236 if (counted) {
230237 const whole = limiter.take("forge", "admin");
231238 if (!whole.allowed) {
232 if (!vouched(db, bucket)) return refused(whole.retryAfter);
239 if (!vouched(db, accountKey)) return refused(whole.retryAfter);
233240 counted = false;
234241 }
235242 }
@@ -243,7 +250,7 @@ export function admitSignin(
243250 succeeded: () => {
244251 giveBack();
245252 slow?.forget(accountKey);
246 vouch(db, bucket, maxGood);
253 vouch(db, accountKey, maxGood);
247254 },
248255 notJudged: () => {
249256 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
132132 expect(signinKeyCount(app.db)).toBeGreaterThan(0);
133133 expect(signinKeyCount(app.db)).toBeLessThanOrEqual(3 * maxKeys);
134134 });
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 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs