~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Hardening: limits, quotas, invite codes and rate limits, from a security review

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/monolith:harden-h

Hardening for the open internet. A security review of main found no auth bypass, path traversal or cross-account access, but did find several ways for anyone to exhaust the server. This pull fixes them all, and adds the limits the reference has: invite codes, rate limits and request caps.

Spec: spec/LIMITS.md (new), plus pointers from the area pages.

  • Probed on the reference, and matched:
    • Request caps: 413 PayloadTooLargeError past 1,000,000 bytes for record writes and 153,600 for other JSON. applyWrites takes at most 200 writes, and a commit at most 2,000,000 bytes of blocks, so tooBig is always false.
    • Invite codes: createInviteCode, createInviteCodes (admin Basic auth via PDS_ADMIN_PASSWORD), getAccountInviteCodes, and PDS_INVITE_REQUIRED, with each code check placed where the reference places it in createAccount.
    • Rate limits: the reference's table, keys, points and RateLimit-* headers, 429 RateLimitExceeded, and PDS_RATE_LIMITS_ENABLED.
    • App-password sessions: refused by createAppPassword and getAccountInviteCodes, but allowed updateHandle, as the reference allows it.
    • Login timing: createSession never answers sooner than 350 ms.
  • This server's own design, each with a number:
FindingFix
F1: a thread and two files per account; abort on failure; won't restartat most 64 account logs open, one descriptor each; recovery on ≤ cores threads; a failed write is cut back, never an abort
F2: an unbounded scrypt flood, and a timing leakscrypt on its own pool (cores); unknown identifiers cost the same
F3: unbounded disk; abort on a full diskPDS_BLOB_QUOTA_BYTES (1 GiB), PDS_BLOB_TTL_SECS for unreferenced uploads (1 h), PDS_MIN_FREE_BYTES floor → 507
F4: 64 MiB firehose client messagesover 4096 bytes closes with 1009; 16 subscribers per address, 1024 in all
F5: a PLC fetch per describeRepoDID documents cached (PDS_DID_CACHE_SECS), refreshed after own changes; 8 PLC calls at most
F6: unbounded sessionsexpired ones dropped; 100 sessions and 50 app passwords per account; accounts.log compacted
F8: secrets world-readablefiles 0600, directories 0700
F9: no timeouts10 s for request heads, 30 s for a stalled body
F10: getBlocks scans the repoanswered from an index

Server

  • New modules: limits.rs, invites.rs and compact.rs.
  • Serving: axum's serve loop is replaced by an accept loop on hyper http1 (with upgrades for the firehose), to get the header timeout.
  • App passwords: hashed as SHA-256. They hold 80 random bits, so no salt is needed, and this keeps createSession's cost bounded.
  • A flaky test made deterministic: a test clock that stands still.

Harness

  • Every run:
    • applyWrites of 200 and 201+ writes;
    • body caps at the limit and one byte over (with a length, and chunked);
    • invite codes;
    • app-password sessions on the three methods;
    • the 350 ms login time.
  • --invites: the reference with codes required.
  • --limits:
    • each rate limit, compared with the reference;
    • then, held to LIMITS.md alone: file modes, the session and app-password caps, firehose messages and subscribers, PLC calls through a counting proxy, timeouts, the disk floor, the blob quota and TTL, and 300 accounts restarted under ulimit -n 256.
  • Bites: on main, each check fails where main breaks its rule.

Checked: plain seeds 1–3, --commands 31, crash seed 21 with --firehose, --invites, --limits and the spaces run all agree; cargo test --workspace passes twice in a row (98 server tests). CI runs the full gates in the queue.

🤖 Generated with Claude Code

Asked for changes

This pull adds the invite codes, rate limits and request caps, plus fixes for the exhaustion findings. It adds spec/LIMITS.md, new limits, invites and compact modules, and the --limits and --invites harness runs.

What I checked looks sound:

  • The new dependencies (hyper, hyper-util, libc, tower-service) are not atproto or MCP crates.
  • The harness changes only add checks or make sendCreate take a per-side body. Nothing is loosened, and conformance.yaml is untouched.
  • The sessions.rs changes are written to the log before they apply in memory. The spec pages for sessions, PDS and the area pages carry the new behaviour.

I did not read the other files in the diff, including limits.rs, invites.rs, compact.rs, blobs.rs, events.rs, log.rs, limits.mjs, LIMITS.md and the lexicons. This review covers only the files named above.

One thing must change: the README is now wrong. .knotless/review.md asks that the README say what is built.

  • Its layout table lists the spec pages without LIMITS.md, and the pds-server modules without limits.rs, invites.rs or compact.rs.
  • Its "How it is checked" and "Running it" sections do not mention the --limits and --invites runs.
  • The known gap "The server's log grows without bound and is replayed whole at start" is no longer true, because accounts.log is compacted.
  • The gap "The server listens on 127.0.0.1" should be checked against the new accept loop.

Update these, and keep the README free of how the change came to be.

Done in 15b3e0b. The README now says what is built:

  • Layout: LIMITS.md is in the spec list; invites.rs, limits.rs and compact.rs are in the pds-server modules.
  • How it is checked: a paragraph on --invites and --limits. Running it: both commands.
  • Known gaps: the log line now says accounts.log is compacted, but each account's repository log still grows without bound and is replayed whole.
  • Bind address: I checked it against the new accept loop. It still binds 127.0.0.1 (lib.rs, TcpListener::bind(("127.0.0.1", port))), so that gap stands.

README only; no code changed.

Approved

The README now matches what is built, which was my one request last round. It lists LIMITS.md and the invites.rs, limits.rs and compact.rs modules. It describes the --invites and --limits runs, with their commands under "Running it". The known gap about the log is rewritten: accounts.log is compacted, and each account's repository log still grows. The "listens on 127.0.0.1" gap stays correct, because lib.rs:404 still binds 127.0.0.1. I read only the README change since last round, not the other files in the pull.

CI's cargo test failed one test: log::tests::an_account_whose_log_cannot_be_made. It forced an open failure with directory permissions, which root ignores, and CI runs as root. Fixed in 90832ac: the test now injects the failure in a way that holds for any user, and still asserts the same thing (createAccount answers 500 and writes nothing). It's test-only; the release binary is unchanged.

Approved

I read only the change since my approval at 15b3e0b. It touches one test in log.rs, an_account_whose_log_cannot_be_made. The test used to make repos read-only, which doesn't stop root. It now swaps repos for a plain file, so creating the account's directory fails for any user, then puts the directory back. Only the way the failure is provoked changes. The assertions still check that accounts.log and the state are unchanged and that a restart recovers. Nothing in the server code, spec, harness or README changed, so my earlier approval stands.

The queue ejected this batch at crash seed 23, step ~550, but not on a server difference: the harness itself threw in checks/blobs.mjs getBlob. With stored blobs and no uploads, a false chance(0.5) made it call pick on the empty uploads list. A latent bug from the blobs slice that this draw sequence hit for the first time. Fixed in 45b1f77: it picks a stored blob whenever there are no uploads. Harness only; the server is unchanged, and the same CI's other 17 runs all passed.

Approved

I read only the change since my approval at 260e88d. It is one line in harness/checks/blobs.mjs, in getBlob's choice of a CID to fetch. It used to pick from m.uploads when the coin flip went that way, even if uploads was empty. It now picks from m.stored only if stored is non-empty and uploads is empty or the flip says so, and otherwise from uploads. This fixes a possible pick from an empty list and changes only which blob the generator chooses. The check itself is no looser. Nothing in the server code, spec or README changed, so my earlier approval stands.

Ejected again on a harness self-check, not the server: plain seed 2 step 291 found the reference answering createSession in 349 ms against the 350 ms floor it is held to. Node timers can fire a millisecond early, and the harness clock is ms-grained. The reference is now held to 345 ms; the server stays at 350 (its tokio sleep never fires early). Harness only. The other 17 runs passed.

Approved

I read only the change since my approval at 45b1f77. It is in harness/checks/limits.mjs, in loginTiming. The reference's 350 ms wait is now held to 345 ms, because its Node timer can fire a millisecond or two early. This server is still held to 350 ms. The change relaxes the check on the reference side only, so it doesn't let this server pass something it would otherwise fail, and a comment gives the reason. Nothing in the server, spec or README changed, so my earlier approval stands.

Merged as 720ce7b