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

Merged opened this

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

Merged

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.

Notes

shipped production is serving it deploy 5f527c6

opened this

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 (the NAME pattern in core/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 refund that 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. The forge key 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 signinKeyCount doesn'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_plan and estimate tools 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

2 earlier comments · 1 event
commented

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 (the NAME pattern in core/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 refund that 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. The forge key 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 signinKeyCount doesn'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_plan and estimate tools 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

commented

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

Changes requested — before the latest push

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.

  1. (Fix first.) Add tests for the good-source memory. vouched and vouch read Date.now() directly, so nothing tests that a source stops being good after GOOD_SOURCE_MS, or that MAX_GOOD_SOURCES evicts the oldest entry. Both are behaviour this pull adds. Take an injectable clock the way RateLimiter does, or let a test pass in now, 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.
  2. 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_KEYS is a hard-coded 50,000, so a test cannot reach it. Make the cap a parameter, or let admitSignin options override it, and add one test where a flood of distinct (account, source) pairs leaves signinKeyCount at or under the cap. Box 1 says the key count has a hard cap, so the test should show it.
  3. touch sets lastSweep = 0 and runs a full sweep on every take once the map is at maxKeys. 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.
  4. 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.
  5. The gate notes that goodOf duplicates sessionsOf in src/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.

commented

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:

  1. Good-source memory is tested. admitSignin now takes a clock (now) through its options. The sign-in limiters and the good-source list read that clock instead of calling Date.now() directly. New tests in test/signin-window-bounds.test.ts:
    • a source stops being good once GOOD_SOURCE_MS has passed;
    • past MAX_GOOD_SOURCES the 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.
  2. The key cap is tested on the real limiters. maxKeys and maxGoodSources can now be set through admitSignin options, so a test can reach the caps. One test sends 4,000 distinct (account, source) pairs and checks signinKeyCount stays at or under three times the cap. Without the cap the same flood leaves 411 keys.
  3. No full scan at the cap. In RateLimiter.touch (src/core/ratelimit.ts), a take at maxKeys now 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 in test/ratelimit.test.ts checks this; it fails on the old code.
  4. 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 Trust label I asked for was not applied.
  5. One shared helper. perOwner in src/kernel/per_owner.ts now replaces the get-or-make code in signin-window.ts (its limiters and goodOf) and sessionsOf in src/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.ts fails on this branch without my change too.
  • The two warm page-budget tests in test/page-budget.test.ts are timed; they ran while 614 test files shared the machine, and this pull does not touch src/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

Approved

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.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs