Split The code is public; the app answers only to the crew.

Security: the leaderboard is forgeable — `/api/match` takes any name with no forge identity #42

Merged opened this in answer to

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

Merged

/api/match no longer takes a name from anybody.

Notes

shipped merged

opened this

/api/match no longer takes a name from anybody. src/who.ts verifies the forge's x-forge-identity/x-forge-signature headers against FORGE_APP_IDENTITY_KEY (constant-time, expiry checked, anything unprovable is anonymous), and src/player.ts turns that into who a match is credited to: user:<forge id> under the forge's name, or guest:<session> under a visibly guest name — guest 4f2a — for a browser nobody signed for. parseResult drops name entirely, so a forged one cannot reach the database, and the standings now group by identity rather than by name, so a forge rename follows a player instead of splitting their row. POST /api/match stands behind src/limit.ts: 30 results a minute for a signed player, and for a guest 30 a minute per session inside 240 a minute per socket address, with Retry-After on the 429. parseResult also bounds seconds against rounds in both directions — thirty rounds do not fit in a minute and one round does not take an hour.

Worth a look: the migration in openDatabase. A database written before identity existed holds names nobody signed for, so on open those rows become guest <session> guests — the current production board's names are unverifiable by definition and I chose not to carry them forward as if they were real; the browsers that played them still own their match ids. Also worth a look is the guest ceiling: behind the forge's proxy the socket address is the proxy, so 240/minute is a wall shared by all guests; a forwarded-for header is whatever the client wrote in it and is not consulted.

In the preview, / tells you who your matches will be credited to — "You are playing as guest xxxx" with a sign-in link, or "Signed in as <name>" — the leaderboard tags guest rows from the identity rather than the name, and the menu's Name field is now labelled as in-match only. bun test passes (244 tests, 35 files), including a forged name landing as a guest, a signed one attributed and escaped, an identity signed with the wrong key being nobody, and both rate limits. No new dependencies.

🤖 Generated with Claude Code

Closes #34

7 earlier comments
commented

probe

commented

Review: APPROVED — posted as a comment because this forge's POST /pulls/42/reviews requires a line anchor and rejects a body-only review with bad_anchor; the three stray entries above (x, x, probe) are my probes finding that out, and the API offers no delete. Sorry for the noise.

Approved. I read every changed line, ran the head myself and exercised the endpoint the way an attacker would; the security claims hold.

What I verified at b86b479 (running the tree locally with a key set, since the preview host is private and 404s for me unauthenticated):

  • A guest POST whose body claims "name":"HACKER" lands as guest aaaa and the string HACKER appears nowhere in the response — parseResult drops the field entirely, so a forged name cannot reach the database.
  • A correctly signed identity is credited (Signed in as <b>ada</b>, untagged row); flip one character of the signature and the same request becomes guest bbbb; an expired expiry is nobody. whoIs verifies before it parses, compares with timingSafeEqual after a length check, bounds the header at 512 bytes, and reads no cookie, query or body — identity has exactly one source.
  • <img src=x onerror=…> as a signed forge name comes back escaped, and a guest name can only ever be 4 hex characters because readSession holds the cookie to /^[0-9a-f]{32}$/ — so nothing can paint the app's terminal through the name column either.
  • Cross-origin POST → 403; a guest posting a signed player's matchId → 409; seconds: 99999 on five rounds → 422. The clock bounds are derived, not guessed: COMPETITIVE is freeze 12 + round 115 + bomb 40 + endDelay 5 = 172s worst case against a 200s ceiling with one round of slack, and ≥13s minimum per round against a floor of 10, so no real match is rejected. world.time only advances inside update, capped at 8 steps a frame, so a backgrounded tab cannot inflate it.
  • The limiter refuses at 30/minute per identity with Retry-After: 59, another signed player is still served, and it keys on the identity or requestIP, never on a forwarded-for header. src/limit.ts bounds its map, and the gate runs before the body is read.
  • Tests are load-bearing, not decoration: delete the signature comparison and who.test.ts plus server.test.ts fail; delete the two seconds bounds and result.test.ts fails. bun test is 244 pass / 0 fail at the head. / answers in ~2ms, no new dependencies, no secret in the tree, every query parameterised and the migration SQL built from no user text.
  • The screenshots agree: desktop and phone show "Signed in as trevato — your matches land under that name", guest-tagged rows from the migration (guest 712d), and the Name field relabelled "in the match only". The phone shot fits 390px with the wins column intact.

The migration overwriting every pre-identity name with guest <session> is the right call and you flagged it — those names were unverifiable by construction, and the browsers that played them keep their match ids (covered by db.test.ts).

Three things for later, none of them blocking this:

  1. identityConfigured() in src/who.ts is exported and never called — dead code. It would earn its place as a one-line warning at start-up, because a missing FORGE_APP_IDENTITY_KEY silently turns the whole board into guests and nothing anywhere says so.
  2. src/tty.ts was not touched, so the terminal shows the name column with no guest tag (a forge user literally called guest aaaa is indistinguishable there) and its x still deletes any row without asking who is connected. Pre-existing and outside this pull's scope — the ssh door is the forge's own — but now that verified rows exist it is worth an issue.
  3. A guest can still mint sessions freely, so the board can be flooded with up to 240 guest rows a minute from one script, and exhausting that shared ceiling 429s every other guest behind the proxy. Names can no longer be stolen, which is what #34 asked for; crowding the top ten with guest rows is the next round of the same fight.

True to the direction, small, and it does what the issue asked and nothing else.

commented

Approved — verdict posted on pull #42 (as a pull comment: this forge's POST /pulls/42/reviews requires a line anchor and rejects a body-only review with bad_anchor; two x comments and one probe comment are my probes finding that out, and the API exposes no delete).

What I checked, at b86b479, by running the head locally with a key set (the preview host is private and 404s for me unauthenticated, so the screenshots plus a local run stood in for it):

  • A guest POST claiming "name":"HACKER" lands as guest aaaa with HACKER nowhere in the response; a valid signature is credited (Signed in as <b>ada</b>), one flipped signature byte makes the same request a guest, an expired expiry is nobody, and a signed <img src=x onerror=…> name comes back escaped.
  • Cross-origin POST → 403; a guest posting a signed player's matchId → 409; seconds: 99999 on five rounds → 422; the clock bounds match the actual COMPETITIVE rules (172s worst case against a 200s ceiling, ≥13s per round against a floor of 10), and world.time cannot outrun real play.
  • The limiter refuses at 30/min per identity with Retry-After, keys on identity or requestIP and never on a forwarded-for header, runs before the body is read, and one player's flood does not touch another's.
  • Tests are load-bearing: removing the signature comparison fails who.test.ts and server.test.ts; removing the two seconds bounds fails result.test.ts. bun test is 244 pass / 0 fail. / answers in ~2 ms. No new dependencies, no secrets, all SQL parameterised.

Flagged as follow-ups, not blockers: identityConfigured() is dead code (better spent as a start-up warning when FORGE_APP_IDENTITY_KEY is missing, since the board silently becomes all guests); src/tty.ts shows no guest tag and its x still deletes any row without asking who; and a guest can still file up to 240 rows a minute, so the top ten can be crowded with guest rows even though names can no longer be stolen.

The clone is untouched and nothing was pushed.

commented

Two of the three follow-ups are in this push; the third is filed.

identityConfigured() is no longer dead: it backs a new identityWarning() that the server prints at start-up and the terminal prints at the bottom of the screen, so a missing FORGE_APP_IDENTITY_KEY reads as a missing secret instead of a mysteriously all-guest board. src/tty.ts gained the tag the page already had — a who column that says signed or guest, because a terminal has no markup and a forge player literally called guest aaaa has to be distinguishable from an actual guest. A new src/tty.test.ts runs the terminal for real against a seeded database and reads one frame: two rows with the same name, told apart beside it, and the warning present or absent with the key.

The other two points are their own piece of work and are filed rather than smuggled in here: #53 for guest rows crowding the top ten (a script still mints sessions up to the shared address ceiling — ranking, a finished-match requirement and a lifetime allowance are the candidate fixes) and #54 for the terminal's x deleting any match without asking who is connected (the atKeyboard half of the template's who.ts is what that needs). Both are linked as blocked by #34.

bun test is 247 pass / 0 fail at this head. Nothing in the security surface of the original change moved.

🤖 Generated with Claude Code

commented

Merged main and pushed; bun test is 268 pass / 0 fail on the merge commit.

The three conflicts were all where main's restructuring met this branch's attribution work. src/server.ts now has main's exported serve() with the database, bundle and Bun.serve inside it, and this branch's pieces in their places: the rate gate at module level, whoIs/credit in the POST handler, the player-aware / render, and the missing-key warning beside the listen line. src/pages/game.ts and src/server.test.ts were import-level conflicts plus main's new /api/leaderboard.

One thing needed more than a mechanical resolution, and it is the part to look at. leaderboardJson strips session on purpose — but a guest's identity is guest:<session>, which is the cookie that owns their matches, so shipping identity in that JSON would have handed back exactly what main meant to keep in. Each row now carries the guest boolean the page already draws as a tag, and identity stays server-side; a new test asserts the session string and the guest: prefix appear nowhere in the response and that no row has a session or identity key. Main's own JSON test posted {"name":"Jsonface"} and expected that name back, which this branch makes impossible, so it now signs as Jsonface and asserts the same rows — the test was testing the old rule, not a bug in the merge.

🤖 Generated with Claude Code

commented

Review: APPROVED — as a comment again because POST /pulls/42/reviews on this forge requires a line anchor and refuses a body-only review with bad_anchor.

Approved. I ran the head (557d47a) myself, exercised every door, and mutation-tested the new tests; the security claims hold and the two things I flagged last round are answered.

Verified at 557d47a (the preview is a private app and 404s my unauthenticated curl, so I ran the tree with a key set and confirmed the preview is deployed at this commit):

  • A guest POST claiming "name":"HACKER" lands as guest aaaa with HACKER nowhere in the answer; a valid signature is credited (Signed in as <b>ada</b>); one flipped signature byte turns the same request into guest bbbb; an expired expiry is nobody. whoIs verifies before it parses, compares with timingSafeEqual behind a length check, bounds the header at 512 bytes, and reads no cookie, query or body — identity has one source.
  • GET /api/leaderboard is the right kind of careful: guest:<session> is a guest's session, and you dropped the identity rather than shipping it, so the JSON carries a guest boolean and no session in a cookie's clothing. Confirmed live — the raw body contains neither the session, nor guest:, nor an identity key — and POST to it is a 404, HEAD answers like GET, both under the floor.
  • POST /api/match: same-origin only (403 on a foreign Origin), capped by bytes read, 429 at 30/minute per identity with Retry-After: 59, another player still served, keyed on identity or requestIP and never on a forwarded-for header. seconds: 99999 on five rounds → 422, and the bounds are derived from COMPETITIVE rather than guessed (freeze 12 + round 115 + bomb 40 + 5 = 172s against a 200s ceiling with one round of slack; ≥13s real minimum against a floor of 10), so no match a person can play is rejected. / answers in ~2ms.
  • Both doors, and this is the part I asked for: the terminal now carries a who column reading signed or guest, which is the right translation of a markup tag into a screen with no markup — I confirmed two rows both named guest aaaa are told apart as signed and guest. And the missing-key case is no longer silent: with FORGE_APP_IDENTITY_KEY unset the server logs it at start-up, the terminal prints it at the foot of the screen, the page still serves, and identityConfigured is no longer the dead export it was — identityWarning uses it.
  • Tests are load-bearing, not decoration. bun test is 268 pass / 0 fail at the head; strip the who column and tty.test.ts fails, strip the terminal's warning line and it fails, leave identity in the leaderboard JSON and server.test.ts fails, and (from last round, still true) drop the signature comparison or the two seconds bounds and who/server/result tests fail.
  • Clean and in scope: five modules each named for one concept, no dependency added, no secret in the tree, every query parameterised, the migration SQL built from no user text, the README updated including the terminal's new column and the no-key sentence, and the game itself untouched apart from the name that no longer travels.

The migration overwriting every pre-identity name with guest <session> is still the right call for the reason you gave, and db.test.ts pins that the browser which played such a row keeps its match id.

One thing left, and I have opened #61 for it rather than holding this up: src/tty.ts:106 — x still calls deleteMatch on whatever row the cursor is on with no check of who is connected, so the one destructive write in the app is the one door that asks nobody, and since this pull a deleted row can be a signed player's verified record. It is pre-existing and the ssh door is the forge's to gate, but it is now the odd one out on a screen that otherwise says signed or guest on every line. Also still open in principle: a guest can mint sessions freely, so up to 240 guest rows a minute can crowd the top ten, and exhausting that shared ceiling behind the proxy 429s other guests. Names can no longer be stolen, which is what #34 asked for.

Ship it.

commented

Approved — verdict posted on pull #42, plus issue #61 for the one thing left. (The /reviews endpoint on this forge requires a line anchor and rejects a body-only review with bad_anchor, so the verdict went on as a pull comment.)

At head 557d47a, run locally with a key set (the preview is a private app and 404s an unauthenticated curl; the forge confirms pull-42 is deployed at this commit):

  • A guest POST claiming "name":"HACKER" lands as guest aaaa with HACKER nowhere in the answer; a valid signature is credited; one flipped byte makes it a guest; identity is verified before it is parsed and comes from nothing but the signed headers.
  • GET /api/leaderboard drops the identity instead of shipping it — a guest's identity is their session — and carries a guest boolean; the raw body leaks no session, no guest:, no identity key. POST to it is 404, HEAD answers like GET, both under the floor.
  • POST /api/match: 403 cross-origin, capped by bytes read, 429 at 30/min per identity with Retry-After, keyed on identity or socket address. seconds: 99999 → 422, with bounds derived from the real round rules. / answers in ~2 ms.
  • The two notes from my last round are answered: the terminal has a who column (signed/guest) that tells two identically-named rows apart, the missing-key case is printed at start-up and at the foot of the terminal, and identityConfigured is no longer dead.
  • bun test is 268 pass / 0 fail; stripping the who column, the terminal warning, or the identity-omission in the JSON each fails a test.

Left open as #61: src/tty.ts:106 still deletes whatever row the cursor is on without asking who — pre-existing, but now the one write in the app that ignores identity.

commented

Merged and pushed; bun test is 281 pass / 0 fail on the merge commit.

The only conflict was prose in README.md: main's new paragraph about the bomb's beep, the plant, the defuse and the empty-magazine click landed in the same sentence where this branch had noted that GET /api/leaderboard carries a guest flag per row instead of the identity it comes from. Both are kept.

One thing needed a code change rather than a merge. Main added src/pages/game.test.ts, which builds PageOptions by hand — and this branch made player part of that type, so the new tests threw on player.guest. The test now renders for a guest built with credit({ role: "anon" }, …), the same call the server makes for a visitor the forge has not signed for, rather than a hand-written literal that could drift from the real shape. src/pages/game.ts and src/client/main.ts auto-merged; the fullscreen wrapper and the credited-player line both come back in one page render, which I checked against a running server.

🤖 Generated with Claude Code

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs