Skip to content

Security: mlziade/liveboard

Security

docs/security.md

Security

LiveBoard is a casual, real-time league-table simulator. Rooms, guesses, a live leaderboard among friends. It doesn't touch payment data, and the only sensitive-ish data it holds is a Google account's basic profile (name, email, avatar) and optional room passwords. The threat model is light accordingly. This isn't a bank's security doc, but real accounts and real passwords exist, so login and room passwords get treated seriously even though the rest of the stack is deliberately unhardened for a small deployment. For how identity/ownership/permissions actually work, see architecture_auth_and_permissions.md.

What's implemented today

Transport

  • realtime/src/server.ts sets CORS to a single configured origin (cors({ origin: CORS_ORIGIN, exposedHeaders: ["X-Room-Token"] }), both on the Express app and the Socket.io server), not a wildcard. exposedHeaders is needed only so browser JS can read X-Room-Token off a cross-origin response.
  • Both realtime (Express) and frontend (Next.js) listen in plain HTTP. Neither app terminates TLS itself. There's no nginx/caddy/traefik config anywhere in this repo, and docker-compose.yml maps ports straight to the containers (4000:4000, 3000:3000) with no proxy in front. TLS is expected to terminate at a reverse proxy you run in front of the stack, which this repo does not ship (see the gaps and checklist below, and docs/deployment.md).

Authentication (Google accounts)

  • Google ID tokens are verified server-side via google-auth-library's OAuth2Client.verifyIdToken({ idToken, audience: GOOGLE_CLIENT_ID }) (realtime/src/auth/googleVerify.ts). The audience check stops a token issued for some other Google client id from being accepted here.
  • Session tokens are a hand-rolled HMAC scheme, not a JWT library: <base64url(userId.jti.expiresAt)>.<hmac-sha256 hex>, signed with AUTH_TOKEN_SECRET (realtime/src/auth/token.ts). Verification uses timingSafeEqual for the signature comparison and checks expiry before trusting the payload. TTL is 24 hours.
  • Logout revokes a single token by its jti: session_revoked:<jti> is written to Redis with a TTL equal to the token's remaining lifetime, and verifyAuthToken checks that key on every use. A logged-out token stops working immediately, rather than staying valid until its signature-checked expiry.

Room access control

  • Room passwords are hashed with salted scrypt, not stored or compared in plaintext (realtime/src/rooms/password.ts): scryptSync(password, salt, 64), salt is randomBytes(16), stored as "<salt-hex>:<hash-hex>". Verification uses timingSafeEqual (after a length check, since timingSafeEqual throws rather than returning false on mismatched buffer lengths).
  • Room tokens (realtime/src/rooms/roomToken.ts) are the same HMAC-signed pattern as session tokens, but scoped to roomId + passwordVersion and signed with a separate secret (ROOM_TOKEN_SECRET). TTL is 12 hours, with sliding renewal on every authenticated request. Because passwordVersion is folded into the payload, changing a room's password immediately invalidates every token minted under the old password, not just after their own TTL expires.
  • Access to a password-protected room is checked in a fixed three-tier order (checkPasswordAccess in realtime/src/routes/rooms.ts, mirrored in the socket join-room handler): (1) the room's logged-in owner bypasses the password entirely; (2) a valid X-Room-Token is accepted; (3) the raw X-Room-Password header is checked against the scrypt hash. Guests never get the owner bypass, only a signed-in User.id match does.

Abuse mitigation

  • Room admins can kick a live participant (admin-kick socket event, realtime/src/socket/index.ts), which force-disconnects their socket and also writes room_ban:<roomId>:<identityKey> to Redis with a 7-day TTL (KICK_BAN_TTL_SECONDS). A future join-room from that same identity is rejected with join-error { reason: "banned" } before a participant record is even created. It's the only abuse-mitigation mechanism in the app today. Per-room and identity-scoped, not IP-based, and (see below) nothing stops repeated attempts at the things that matter more, like password guessing or login.

Public report form

POST /reports takes free text from anyone, signed in or not, and writes it to Postgres. Four things limit the damage. reportLimiter allows 10 requests per hour per IP. validateReportBody caps every field (120 characters for the title, 4000 for the description) and rejects an unknown problem type. The route ignores any userId in the body and reads the user from the session token, so nobody can file a report under another account. The admin panel shows the text as plain text, not HTML. The email address is not verified, so a report proves nothing about who sent it.

Identity trust boundaries

  • Guest ids are entirely client-generated (crypto.randomUUID(), cached in localStorage) and never issued or stored server-side. The server's only check is a format guard, ^[A-Za-z0-9-]{1,64}$ (GUEST_ID_PATTERN in realtime/src/auth/identity.ts), which exists purely to bound the value as a Redis-key/ownership-comparison input, not to establish any real trust. Anyone can claim any guest id that matches the pattern. The security boundary that actually matters (room ownership, admin rights) rests on a signed session token for logged-in users, and on "whoever happens to hold this guest id string" for guests. A deliberately weak tradeoff, and an accepted one, given this app's stakes.

Known gaps / hardening backlog

These are honest gaps, not oversights hidden from the reader. Worth noting, low urgency for the current deployment scale:

  • No rate limiting anywhere. POST /auth/google, room password checks (GET /rooms/:id/state with X-Room-Password), room creation, and every other route are completely unthrottled. Nothing stops scripted room-password brute-forcing or hammering the Google-token-verification endpoint today. No express-rate-limit (or equivalent) dependency exists in realtime/package.json.
  • No helmet (or similar) security-headers middleware. realtime/package.json has no helmet dependency; server.ts sets no security headers beyond what Express does by default, which is close to nothing: no X-Content-Type-Options, no Strict-Transport-Security, no CSP.
  • No HTTPS/TLS termination inside the app stack. Both apps serve plain HTTP; TLS has to be handled by a reverse proxy in front of the VPS. This repo ships no proxy config. See docs/deployment.md for deployment specifics.
  • No DDoS protection layer. Nothing beyond whatever the hosting provider offers by default.
  • Secrets silently fall back to a random per-restart value if unset, rather than failing loudly:
    • AUTH_TOKEN_SECRET: unset means a fresh random secret is generated on every process start, which silently invalidates every existing session token on every restart. docker-compose.yml (the production compose file) guards this one with Compose's :? required-variable syntax, so a production deploy without it set fails to start. docker-compose.dev.yml and running the server directly (npm start/tsx) have no such guard.
    • ROOM_TOKEN_SECRET: same fallback behavior and same blast radius (a 12h room-access token, not a login session). It's now listed in realtime/env.example and guarded by the same :? required-variable syntax in docker-compose.yml, matching AUTH_TOKEN_SECRET. Running the server directly, or docker-compose.dev.yml, still has no such guard, same as AUTH_TOKEN_SECRET.
  • No consistent input-validation library. There's no zod/joi/express-validator/yup anywhere in realtime/. Request body validation is ad hoc typeof/length checks scattered across realtime/src/routes/*.ts (e.g. rooms.ts's championshipName/password/displayName checks). It works, but there's no shared schema layer, so it's easy for a new route to skip validation it should have.
  • Guest identity is trivially spoofable/resettable. Clearing site data or switching browsers produces a new guest id, sidestepping room ownership and ban state. An accepted tradeoff for this app's stakes, not something planned to be fixed. See docs/architecture_auth_and_permissions.md §8 "Known limitations" for the fuller list, including the related admin/ownership caveats.

If you're deploying this publicly

Practical steps matched to the gaps above, roughly in priority order:

  1. Put a reverse proxy (nginx or Caddy) in front of both services, terminating TLS there and forwarding to realtime:4000 / frontend:3000 over plain HTTP internally. Caddy in particular gets automatic Let's Encrypt certs with minimal config. This also gives you a natural place to add a global rate limit / connection cap before traffic even reaches the app.
  2. Set AUTH_TOKEN_SECRET and ROOM_TOKEN_SECRET explicitly in your production environment. docker-compose.yml fails fast if either is missing, but that only helps if you're actually deploying through it. Run realtime any other way and it still falls back to a random per-restart secret silently.
  3. Add express-rate-limit (or equivalent) on POST /auth/google (login-attempt throttling) and specifically on the room-password check path (GET /rooms/:id/state with X-Room-Password, and the socket join-room handler's password branch) to make brute-forcing a room password impractical.
  4. Add helmet to realtime's Express app for baseline security headers. Cheap to add, no known downside for this app.
  5. If traffic or abuse patterns justify it, put the whole stack behind a provider with basic DDoS protection (e.g. Cloudflare in front of the reverse proxy) rather than building anything custom.

There aren't any published security advisories