Validate game state and stop common cheating in browser multiplayer games. Server authority, message schemas, size limits and safe parsing, rate limits and strike counts, input sequence checks against replays and spam, clamping inputs, movement plausibility, hidden information, signed run tokens and plausible-score checks for leaderboards, cleaning player names and chat, origin checks, secrets,...
Download skill (.zip) Files on GitHubMultiplayer security
The browser belongs to the player. Anything in it (code, memory, messages, storage) can be read and changed. Security means the server decides what is true, and checks everything a browser sends.
Server authority
| Client sends | Server does |
|---|---|
Inputs (dx, dy, fire) |
Moves the player with its own rules and speed |
| “I want to buy X” | Checks price, money and stock, then changes state |
| “I shot at angle a” | Checks cooldown and ammo, does the hit check itself (with lag compensation) |
| Never: positions, damage, scores, “I won”, “I’m host” |
Hidden information (other hands, fog, roles, answers) never goes to a client that shouldn’t see it. Hiding it in the UI is not enough; it’s readable in the browser’s tools.
assets/guard.js
import { schema, rateLimit, sequenceGuard, checkMove, clampStick, cleanName, plausibleScore, parseMessage, strikes } from "./guard.js";
const p = parseMessage(raw, 4096); // size before JSON.parse; must be an object
const bad = schema({ t: "str", seq: "int", dx: ["num", -1, 1], dy: ["num", -1, 1], fire: "bool?" })(msg); // null or a reason
const limit = rateLimit({ rate: 90, burst: 180 }); if (!limit.take(playerId)) drop();
const fresh = sequenceGuard(); if (!fresh(playerId, msg.seq)) drop(); // old, repeated or jumping sequence numbers
const strike = strikes({ limit: 25 }); if (strike(playerId, 2)) socket.close(1008, "bad input");
cleanName("<b>x</b>\u202E") ; plausibleScore({ score, seconds, maxPerSecond: 50 })
checkMoveandclampStickfor games that must accept positions or analog input: clamp to the legal speed.- Rules:
["num", min, max],["int", min, max],["str", minLen, maxLen],"bool",["enum", [...]],["arr", maxLen]; add?to the type for optional. Unknown fields are refused. - Count strikes instead of kicking on the first bad message (a bug shouldn’t kick honest players).
assets/run-token.mjs (leaderboards)
The server signs a token when a run starts; a score is accepted once per token, only from the same player, and only if the time played makes it possible. See leaderboard-system for the full server.
Also
- Origins: pass
origins: ["https://html-classic.itch.zone"](where itch.io runs HTML games) plus your own site to the WebSocket server so other sites can’t connect their pages (it does not stop scripts, which is why the checks above matter). - Secrets (run token secret, database keys) only in server environment variables. Never in the page, the repo, or a Bash-served folder. Run bash-public-repo-privacy-check before making a repo public.
- Chat: clean like names, limit length and rate, a mute button per player, and a report or block option for public games.
- Bash kits: page writes to
sharedvalues can come from any page; rules and secrets go in server routes (playing-together). - Peer-to-peer: the host is the authority and can cheat. Use a server for anything ranked.
What you can’t fully stop
Aim bots, auto-clickers and modified clients that play “legally” but superhumanly. Limit the damage: server-checked replays for top scores, reports, and statistical flags (accuracy or score rate far above everyone else).
Tested
Node tests: schema (ranges, extra fields, NaN, optional), token-bucket rate limits and refill, sequence guard, speed-hack clamping, stick clamping, name cleaning (HTML, invisible characters, length), plausible scores, size-limited parsing, strikes, run tokens (valid, used twice, forged, wrong player). With real sockets: out-of-range input refused, 150 queued inputs can’t outrun the speed limit, malformed and oversize messages ignored, a 600-message flood disconnected.