December 4, 2026
Preventing Cheating in an Online Quiz: The Answer Never Reaches the Browser
If answers reach the browser, the network tab reveals them. In my quiz the answer stays on the server, the round rides in an encrypted…

By Miraç Deprem
5 min read
If answers reach the browser, the network tab reveals them. In my quiz the answer stays on the server, the round rides in an encrypted token, replays fail.
The easiest way to cheat at a quiz that runs in the browser is to look at the data that arrives when the game loads: if questions come with their correct answers, anyone who opens the network tab of the browser's developer tools sees every answer. This post is about how I prevented cheating in an online quiz in my quiz app: the answer never reaching the browser, the round travelling in an encrypted token, a single-use token against the send a wrong answer, learn the right one, go back trick, and a file name that gave away a flag question.
Why the first plan would not work
The simplest design: when a round starts, the server sends 10 questions with their answers, the browser runs the game, and at the end it reports the score. No server load, no latency.
In that design two things live in the browser: the answers and the score. Everything in the browser is in the user's hands. The network tab shows the answers, and the request that reports the score can be written by hand. Isn't the code hidden? The project is open source, and any code running in a browser can be read anyway. The rule is simple: nothing that decides the score is trusted to the browser.
The answer stays on the server
In the new design the browser sees only the question and the choices. When the user picks one, the choice goes to the server; whether it is right, how many points it earns and whether time ran out are all computed there. The correct answer is shown only after the user has answered, together with the result.
Time is measured on the server too. The moment a question is shown is written into the round's state; when the answer arrives, elapsed time is computed with the server's clock. The browser saying I answered in 3 seconds means nothing:
const elapsed = Math.max(0, now - state.shownAt);
const limit = timeLimit(state);
const timedOut = choice === null || elapsed > limit + GRACE_MS;
const correct = !timedOut && isCorrect(question, choice);
const points = correct ? BASE_POINTS + speedBonus(elapsed, limit) : 0;const elapsed = Math.max(0, now - state.shownAt);
const limit = timeLimit(state);
const timedOut = choice === null || elapsed > limit + GRACE_MS;
const correct = !timedOut && isCorrect(question, choice);
const points = correct ? BASE_POINTS + speedBonus(elapsed, limit) : 0;GRACE_MS is a small allowance for network latency, so an answer sent right as time runs out isn't lost because the network was a few hundred milliseconds slow.
Where does the round state live?
If the answer stays on the server, the server has to know, on every request, which question the round is on, the correct answers and the score. A database is one option, but the app runs on Vercel's serverless functions and opening and closing a record for every round is unnecessary load. Instead, the round's whole state goes into an encrypted token that is handed to the browser. The browser sends it back with every request; the server opens it, reads it and issues a new token with the new state.
The important detail: the token is encrypted, not just signed. A signed-only token such as a JWT cannot be changed, but it can be read; its body is plain JSON in base64. This token contains the correct answers; if it could be read, the answers would be in the browser again. AES-256-GCM both hides the content and detects a single changed byte:
// Turns the round state into an encrypted, tamper-proof token (AES-256-GCM).
// The token lives in the browser but cannot be read; change one byte and it won't open.
export function seal(data, key, now) {
const body = JSON.stringify({ jti: randomUUID(), exp: now + TOKEN_TTL_MS, data });
const iv = randomBytes(IV_BYTES);
const cipher = createCipheriv('aes-256-gcm', key, iv);
const encrypted = Buffer.concat([cipher.update(body, 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();
return `${VERSION}.${Buffer.concat([iv, tag, encrypted]).toString('base64url')}`;
}// Turns the round state into an encrypted, tamper-proof token (AES-256-GCM).
// The token lives in the browser but cannot be read; change one byte and it won't open.
export function seal(data, key, now) {
const body = JSON.stringify({ jti: randomUUID(), exp: now + TOKEN_TTL_MS, data });
const iv = randomBytes(IV_BYTES);
const cipher = createCipheriv('aes-256-gcm', key, iv);
const encrypted = Buffer.concat([cipher.update(body, 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();
return `${VERSION}.${Buffer.concat([iv, tag, encrypted]).toString('base64url')}`;
}The token also expires through its exp field: after an hour it no longer opens.
The go-back trick
With an encrypted token one trick was still possible. Because the token carries the state, the same token could be sent twice:
- The user sends a random choice with the token. The server says wrong, the answer is C and issues a new token.
- The user ignores the new token and sends the old one again, this time with C.
- The old token is still valid and belongs to the moment before the question was answered. The server says
correct.
This is a kind of replay attack. The fix is to give every token a unique ID called jti and make sure each ID is used once. The ID is saved to Redis with SET NX, a write only if absent command; if it already exists, the request is rejected:
async claim(key, ttlMs) {
const [result] = await pipeline([['SET', key, '1', 'PX', String(ttlMs), 'NX']]);
return result === 'OK';
},
const next = answer(state, input.choice, now());
if (next === state) return fail(400, 'bad_request');
if (!(await claim(store, opened.jti))) return fail(409, 'already_used');async claim(key, ttlMs) {
const [result] = await pipeline([['SET', key, '1', 'PX', String(ttlMs), 'NX']]);
return result === 'OK';
},
const next = answer(state, input.choice, now());
if (next === state) return fail(400, 'bad_request');
if (!(await claim(store, opened.jti))) return fail(409, 'already_used');The record is kept for as long as the token lives; the token won't open after it expires anyway, so there is no need to keep it longer. Because SET NX is a single atomic command, even if the same token is sent twice at the same moment only one gets through. Using a joker follows the same rule. The test:
test('a token cannot be used twice', async () => {
const { api } = setup();
const round = await call(api.round, START);
assert.equal((await call(api.answer, { token: round.body.token, choice: 0 })).status, 200);
assert.equal((await call(api.answer, { token: round.body.token, choice: 1 })).status, 409);
});test('a token cannot be used twice', async () => {
const { api } = setup();
const round = await call(api.round, START);
assert.equal((await call(api.answer, { token: round.body.token, choice: 0 })).status, 200);
assert.equal((await call(api.answer, { token: round.body.token, choice: 1 })).status, 409);
});The only thing the server keeps is the IDs of used tokens; the round itself still lives in the token. Much lighter than keeping rounds in a database.
Choice order and flag files
There were two more small leaks.
The order of choices. Choices are shuffled every round. The shuffle uses a seed so that the same question can be rebuilt with the same seed. Since the code is open source, anyone who knows the seed could repeat the shuffle on their own computer and find which letter the correct answer landed on. So the seed never reaches the browser either; it lives only inside the encrypted token. The view sent to the browser strips the answer, the explanation and the reference that carries the seed:
export function toPublic(question) {
const { answer, explain, ref, id, ...view } = question;
return view;
}export function toPublic(question) {
const { answer, explain, ref, id, ...view } = question;
return view;
}File names in flag questions. In Which country's flag is this?, the flag is shown as an image. Had it come from an address such as /flags/tr.svg, the answer would be in the image's name. So flags are sent without a file name, as a data URL embedded in the question:
flags.set(file.slice(0, 2).toUpperCase(), `data:image/svg+xml;base64,${svg.toString('base64')}`);flags.set(file.slice(0, 2).toUpperCase(), `data:image/svg+xml;base64,${svg.toString('base64')}`);These leaks look small but share one thing: even when the answer is not sent directly, information derived from it, like the shuffle seed or a file name, does the same job.
What this design does not stop
To be honest, it cannot stop a user from searching the question in another tab or asking the person next to them; the short time limit only makes that harder. The goal of this design is narrower: the score cannot be earned by changing something on the user's computer.
What I took away
- Everything that decides the score is computed on the server: the correct answer, the time, the points. The browser only sends the choice.
- If state travels through the browser, signing is not enough; it has to be encrypted. A signed token cannot be changed but can be read; if it holds a secret, encrypt it.
- A token that carries state must be used once. Otherwise an old state can be replayed to go back. An atomic write if absent command makes that cheap.
- Anything derived from the answer is as secret as the answer. A shuffle seed, a file name, a position number.
Originally published at miracdeprem.com.