Security Hardening
The complete security concept behind a live multiplayer game — token isolation, edge validation, socket auth, node trust and hot-rotated TLS. Part of the Box of Cards work.
TS · NestJS · NodeJs · Socket.IO · JWT · Passport · HS256 · class-validator · CORS · Let's Encrypt · ACME · Jest
Defense in Depth, End to End
Box of Cards runs as two live services: a broker that mints tokens and owns the database, and game nodes that run the realtime matches. The browser talks REST to the broker and opens a WebSocket to a node — and every edge between them is guarded. Player calls carry a client JWT and pass CORS plus a validation pipe; the socket is authenticated on its handshake against an origin allowlist; the server-to-server hop is a node JWT plus an in-domain address check. Wildcard TLS wraps all of it.
The sections below walk the interesting parts one at a time — starting with a privilege-escalation gap that was found and closed.
A Privilege-Escalation Gap
Admin routes were guarded by the node token — so any game node, each of which holds the shared node secret, could mint a token that admin endpoints accepted. A dedicated admin guard was added that verifies against a broker-only secret and asserts an explicit ADMIN role claim on every admin route; a node or player token now gets a flat 401.
A regression test pins the fix in place: a valid role-less node JWT and a valid player JWT are both rejected from an admin route.
Three Token Audiences, Three Secrets
Players, servers and the operator each get their own token, verified by a separate Passport strategy against a separate secret — a client token can never stand in for a node or admin token. Every verification is pinned to a single algorithm (HS256) so a forged header can't downgrade to alg:none or trigger algorithm confusion, and expiry is always enforced.
Authenticating the Realtime Socket
A socket is authenticated the moment it connects — the JWT is verified from the handshake before any game logic runs, and a failure disconnects the socket outright. Beyond the signature, the connection is authorized against membership: the token's user has to actually belong to this game, so a valid token for one match can't drive another. A stale-socket guard keeps a late disconnect from clobbering a fresh reconnect.
A Node Can't Redirect Players Off-Domain
When a game node registers, it self-reports the address players will be redirected to. The broker rejects any address outside its own domain and logs the attempt loudly. So even a node that holds the shared node secret — the strongest server-side credential — still can't point real player sessions at an attacker-controlled host.
Server-Authoritative Game State
The game is server-authoritative: every move is validated against turn, role and count rules before it touches the board, so a tampered client can't make an illegal play. And the board is masked per player before it's broadcast — other players' submitted answers and unrevealed cards are stripped out — so the hidden information a cheater would want simply never reaches the client. Even deck lookups return 404 rather than 403 for private decks, so the API doesn't leak what exists.
Wildcard TLS, Rotated Without Downtime
Certificates are wildcard, issued from Let's Encrypt over the ACME dns-01 challenge — the broker writes a TXT record to Route53, gets the cert, and stores the full chain in S3. Issuing once from the broker and keeping the chain in object storage is what stays clear of Let's Encrypt's rate limits: no node ever talks to the CA. Once a month the broker re-issues, and every node fetches its up-to-date certificate from S3 every day and hot-swaps it via setSecureContext with no restart and no dropped games. The IAM policy for the challenge is scoped to a single hosted zone.