Server-side anti-cheat: making progression tamper-proof with NestJS and Supabase
Published on · 7 min read
In a gamified fitness app, any user-entered metric can be faked. Here is how I designed Grindrise so progression stays credible: four layers of defense, from the scoring model to the tests.
The problem: everything the client sends can be faked
Grindrise turns every workout into RPG progression: players pick a class, earn XP, level up and unlock story fragments. The whole game rests on one promise: a high level means you actually trained.
But a mobile app controls nothing the user enters. Duration, distance, tonnage: everything can be inflated, and an HTTP request can be replayed or forged without going through the app. So the question is not "how do we prevent cheating", which is impossible on the client, but "how do we make it pay almost nothing".
Layer 1: the client never writes XP
The database runs on Supabase (PostgreSQL 17), with deny-by-default Row Level Security: without an explicit policy, no row can be read or changed. The mobile client reads directly from the database with the anon key, which keeps the app fast. Every sensitive write, however, goes through the NestJS API, which alone holds the service_role key.
In practice, the app sends "I ran for 45 minutes", never "give me 80 XP". The server computes the points, using rules the client cannot change.
Layer 2: an atomic transaction and an append-only log
A workout and the XP it earns are written in the same transaction, through a Postgres RPC function (log_workout_with_xp). You cannot end up with a workout without its XP, or XP without a workout, even if a request fails halfway.
Every XP gain is an event in the xp_events table, which is append-only: a trigger forbids any update or delete. The history is therefore a reliable log, and the level is recomputed from it.
Another benefit: the level curve is stored in the database. If I rebalance the game, a recompute (recomputeProgress) applies the new curve to every player without rewriting a single history row.
Layer 3: a scoring model where cheating pays little
Technical security is not enough: a user can always log a fake workout through the app itself. So the scoring model is designed to limit what cheating earns.
- 60 XP per workout for showing up: consistency matters more than performance.
- Up to 40 XP for effort on a concave curve: doubling the declared duration does not double the points.
- At most two credited workouts per day, at least 30 minutes apart.
- Backfill limited to 7 days: you cannot retroactively fill in a whole month.
- No effort bonus for strength training: tonnage is too easy to inflate.
The result: the best way to progress fast is still to train regularly. A cheater earns a little more, but never enough to be worth the effort.
Layer 4: authentication verified with no network round-trip
A global NestJS guard verifies every Supabase JWT locally, against the public JWKS, with the jose library. No network call per request, so no added latency. Accepted algorithms are allow-listed (ES256 and RS256), which closes algorithm-confusion attacks. Public routes are declared explicitly with a @Public decorator: everything is protected by default.
This choice has a cost, documented in the code: local verification cannot see an immediate revocation. The token stays valid until it expires. For a fitness app, that is an acceptable trade-off.
Testing security, not just logic
A badly written RLS policy breaks nothing visible: it simply lets through what it should block. Migrations and policies are therefore tested in an embedded Postgres, PGlite in WebAssembly, with no Docker. These tests check in particular that an authenticated client cannot grant itself XP.
XP, streak and narrative unlock rules are isolated as pure functions, tested edge case by edge case. On the API side, e2e tests with Supertest run against a mock JWKS server. In total, the project has about 450 test cases.
What I take away for other products
- Start from a threat model: list what a malicious user can send before writing the first route.
- Read directly, write through the API: you keep Supabase's speed without exposing sensitive writes.
- Make cheating unprofitable rather than impossible: scoring design is a security measure.
- Test permissions like business logic: untested RLS is RLS you assume is correct.
These principles go well beyond games: loyalty programs, referrals, usage quotas, credits. As soon as a user action earns something, the server must be its only referee.