Public: anyone can read this; the crew can change it.
Password sign-in limits cannot be used to lock everyone out or to grow memory without bound #14924
Check · Review · Land · Live done — Landed and live in production.
Password sign-in limits can no longer be used to lock people out or to grow memory without bound (#14907, from the #14766 sweep). src/core/signin-window.ts Source checked first. The per-source window is now checked before anything else.
shipped production is serving it deploy 5f527c6
2 earlier comments · 1 event
Password sign-in limits can no longer be used to lock people out or to grow memory without bound (#14907, from the #14766 sweep).
src/core/signin-window.ts
- Source checked first. The per-source window is now checked before anything else. A source past its sixty a minute is refused before any key exists for the account it named. Every later refusal (the slow ten-minute window, the per-account window, the forge-wide window) gives back what was already counted.
- Name capped in the key. The account name in a key is cut to
KEY_NAME_MAX= 64 characters. Usernames are at most 40 (theNAMEpattern incore/chapters.ts), so no real account shares a key; only made-up names longer than that do. - Hard key cap. Every sign-in limiter holds at most
MAX_KEYS= 50,000 keys, about 20 MB at worst. The code says why: at the default limits the forge-wide window lets about 2,000 keys live in the ten-minute window, so the cap is 25 times that and a real forge never reaches it. - Good sources get past a full forge-wide window. A source that proved a right password in the last 24 hours (
GOOD_SOURCE_MS) is let through when the forge-wide window alone would refuse it. That attempt is not counted there, and its own per-source and per-account windows still hold it. At most 10,000 such sources are remembered (MAX_GOOD_SOURCES). The trade-off: someone who can sign in to an account of their own can, from that source, keep trying while the forge-wide window is full, still held to sixty a minute. signinKeyCount(db)is a new test seam.
src/core/ratelimit.ts
- A
refundthat empties a key deletes it. - A refused take on a key with nothing counted creates no key.
- A new optional
maxKeys: when the limiter is full, expired keys go first, then the least recently counted. Every take now re-inserts its key so the order stays least-recent-first. Theforgekey is touched on every attempt, so it is never the one evicted.
Tests
test/ratelimit.test.ts: four new tests. Three fail on main; the fourth (expired keys go before live ones) passes on main and guards the eviction order.test/signin-window-bounds.test.ts, new:- a source past its window creates no keys
- a long name shares one key
- an attempt that is given back leaves no key
- a forge-wide refusal leaves no keys
- a source that signed in earlier gets past a full forge-wide window
- This file can't run on main because
signinKeyCountdoesn't exist there. I didn't run its tests one by one against the old behaviour.
Checks run: scripts/gate.ts --fix and tsc --noEmit are clean. scripts/fast.ts named 3,198 files, since nearly everything imports ratelimit.ts, so I ran 38 files myself: every sign-in suite, ratelimit, agents, and the tests that use RateLimiter. 402 passed, 1 was skipped, none failed.
Not done:
- No change to the limit defaults or
/admin/limits. - No hashing of names; cutting them short is enough because the key also holds the source.
- I haven't ticked the item in #14766.
- The
keep_planandestimatetools the issue asks for aren't available in this run, so there is no plan or estimate on the issue.
To try it: bun test test/signin-window-bounds.test.ts test/ratelimit.test.ts.
🤖 Generated with Claude Code
This makes the sign-in rate limits impossible to use as a way to lock people out or to fill memory (#14907). Each attempt checks the source's window first, so a source past its limit leaves no record for any account. The account name stored with each count is cut to 64 characters, and a count that drops back to zero is deleted. Each limiter keeps counts for at most 50,000 sign-in keys and drops the least recently used first. A source that signed in with the right password in the last 24 hours is not refused just because the forge-wide window is full. The changes are in src/core/signin-window.ts and src/core/ratelimit.ts.
The check failed on the previous push because nothing under src/ called signinKeyCount; only test/signin-window-bounds.test.ts uses it, to show an attempt leaves no extra keys behind. It is now listed in scripts/unused-exports-allow.json, like the other functions that exist only for tests. No code changed.
The dead-code test, test/signin-window-bounds.test.ts, test/ratelimit.test.ts, bun scripts/gate.ts --fix and bun x tsc --noEmit all pass locally. The gate printed one note: goodOf in signin-window.ts matches sessionsOf in src/machines/shell.ts and could perhaps share one implementation. It didn't fail the gate, and I left both as they are.
To try it: bun test test/signin-window-bounds.test.ts test/ratelimit.test.ts test/unused-exports.test.ts.
🤖 Generated with Claude Code
The design does what #14907 asks and I found no security hole, but the tests do not cover every behaviour the change adds. Fix these in one round.
- (Fix first.) Add tests for the good-source memory.
vouchedandvouchreadDate.now()directly, so nothing tests that a source stops being good afterGOOD_SOURCE_MS, or thatMAX_GOOD_SOURCESevicts the oldest entry. Both are behaviour this pull adds. Take an injectable clock the wayRateLimiterdoes, or let a test pass innow, and cover both cases. Also add a test that a source which is not good and meets a full forge window is still refused after an earlier refusal. Only the stranger case is tested now. - The key cap is tested only on a bare
RateLimiter. No test shows that the sign-in limiters stay bounded when many sources and names are used.MAX_KEYSis a hard-coded 50,000, so a test cannot reach it. Make the cap a parameter, or letadmitSigninoptions override it, and add one test where a flood of distinct (account, source) pairs leavessigninKeyCountat or under the cap. Box 1 says the key count has a hard cap, so the test should show it. touchsetslastSweep = 0and runs a fullsweepon every take once the map is atmaxKeys. A flood at the cap makes each request scan up to 50,000 keys. That works against the under-50 ms goal and gives an attacker a cheap CPU cost. Evict only the oldest key by insertion order (the Map iteration order), and sweep on the normal interval.- Known trade-off, which the pull states: any account that can sign in once vouches its source and so skips the forge-wide window for 24 hours. Fine for now, but open a follow-up issue so it is not forgotten.
- The gate notes that
goodOfduplicatessessionsOfinsrc/machines/shell.ts. Share one helper or say why not.
There is no page or terminal change, so no screenshots, docs or TUI work is needed. README is untouched, which is correct.
Password sign-in limits can no longer be used to lock everyone out or to grow memory without bound (#14907). The source's window is checked first, so a source past its limit creates no key for any account. Account names in keys are cut to 64 characters, and an emptied key is deleted. Each limiter holds at most MAX_KEYS keys. A source that signed in with a right password in the last 24 hours (GOOD_SOURCE_MS) is not refused by the forge-wide window alone.
This round covers all five review points:
- Good-source memory is tested.
admitSigninnow takes a clock (now) through its options. The sign-in limiters and the good-source list read that clock instead of callingDate.now()directly. New tests intest/signin-window-bounds.test.ts:- a source stops being good once
GOOD_SOURCE_MShas passed; - past
MAX_GOOD_SOURCESthe oldest good source is forgotten first; - a source that is not good is refused by a full forge window, and refused again after that first refusal.
- a source stops being good once
- The key cap is tested on the real limiters.
maxKeysandmaxGoodSourcescan now be set throughadmitSigninoptions, so a test can reach the caps. One test sends 4,000 distinct (account, source) pairs and checkssigninKeyCountstays at or under three times the cap. Without the cap the same flood leaves 411 keys. - No full scan at the cap. In
RateLimiter.touch(src/core/ratelimit.ts), a take atmaxKeysnow deletes only the key that comes first in the Map's order, which is the least recently counted. Expired keys are swept on the normal interval. A new test intest/ratelimit.test.tschecks this; it fails on the old code. - Follow-up issue opened: #15019. Any account that signs in once lets its source past a full forge-wide window for a day. The
P0 Trustlabel I asked for was not applied. - One shared helper.
perOwnerinsrc/kernel/per_owner.tsnow replaces the get-or-make code insignin-window.ts(its limiters andgoodOf) andsessionsOfinsrc/machines/shell.ts.
Checks: bun scripts/gate.ts --fix and bun x tsc --noEmit are clean. The 18 test files that use the limiter, the sign-in windows or the machine shell, plus test/unused-exports.test.ts, pass (202 tests). scripts/fast.ts could not name the tests because the new kernel file makes the diff too broad, so I also ran a wider set of 614 files. Four tests failed there; none is about sign-in or the limiter:
test/time-element.test.tsfails on this branch without my change too.- The two warm page-budget tests in
test/page-budget.test.tsare timed; they ran while 614 test files shared the machine, and this pull does not touchsrc/web. - The chromium capture test could not run here: the sandbox does not provide
/usr/bin/chromium.
To try it: bun test test/signin-window-bounds.test.ts test/ratelimit.test.ts.
🤖 Generated with Claude Code
All five earlier asks are met in the diff since 44dc801415db. (1) Good-source expiry and the MAX_GOOD_SOURCES cap are tested through the injected clock. (2) The key cap is tested on the real limiters, with 4,000 pairs held to 3 x maxKeys. (3) touch() now evicts only the first key in Map order, with no scan, and a test pins it. (4) The follow-up is #15019. (5) perOwner is shared by signin-window and shell. No sign-in page or terminal screen changes, so the two-doors and docs rules do not apply. Next, non-blocking: (a) a test that the forge key is never evicted under key pressure; (b) settle #15019, since any one successful sign-in vouches for its source for a day.
Password sign-in limits can no longer be used to lock people out or to grow memory without bound (#14907, from the #14766 sweep).
src/core/signin-window.tsKEY_NAME_MAX= 64 characters. Usernames are at most 40 (theNAMEpattern incore/chapters.ts), so no real account shares a key; only made-up names longer than that do.MAX_KEYS= 50,000 keys, about 20 MB at worst. The code says why: at the default limits the forge-wide window lets about 2,000 keys live in the ten-minute window, so the cap is 25 times that and a real forge never reaches it.GOOD_SOURCE_MS) is let through when the forge-wide window alone would refuse it. That attempt is not counted there, and its own per-source and per-account windows still hold it. At most 10,000 such sources are remembered (MAX_GOOD_SOURCES). The trade-off: someone who can sign in to an account of their own can, from that source, keep trying while the forge-wide window is full, still held to sixty a minute.signinKeyCount(db)is a new test seam.src/core/ratelimit.tsrefundthat empties a key deletes it.maxKeys: when the limiter is full, expired keys go first, then the least recently counted. Every take now re-inserts its key so the order stays least-recent-first. Theforgekey is touched on every attempt, so it is never the one evicted.Tests
test/ratelimit.test.ts: four new tests. Three fail on main; the fourth (expired keys go before live ones) passes on main and guards the eviction order.test/signin-window-bounds.test.ts, new:signinKeyCountdoesn't exist there. I didn't run its tests one by one against the old behaviour.Checks run:
scripts/gate.ts --fixandtsc --noEmitare clean.scripts/fast.tsnamed 3,198 files, since nearly everything importsratelimit.ts, so I ran 38 files myself: every sign-in suite,ratelimit,agents, and the tests that useRateLimiter. 402 passed, 1 was skipped, none failed.Not done:
/admin/limits.keep_planandestimatetools the issue asks for aren't available in this run, so there is no plan or estimate on the issue.To try it:
bun test test/signin-window-bounds.test.ts test/ratelimit.test.ts.🤖 Generated with Claude Code
Closes #14907