Reset tokens from Math.random? How to make tokens nobody can guess

Math.random does not give cryptographically secure random numbers, and MDN says not to use it for anything security related. For tokens and session IDs, use a secure generator like crypto.getRandomValues or crypto.randomUUID.

Published 3 min read#App security#Web development

The same function you use to shuffle cards in a game on your page may be the one making password reset tokens for your users. The game does not care if someone guesses the next card. The reset token cares a lot. By the end of this article you will know which function to use in each language, and how to find this mistake in your code.

What is the difference between a random number and a secure random number?

A secure random number comes from a generator built for cryptography, so nobody can predict the next number it gives. A regular generator is faster, but its randomness is weak.

The OWASP Cryptographic Storage Cheat Sheet names the two kinds. A regular generator (PRNG) gives low-quality randomness and is much faster, and it must not be used for anything security critical. A generator built for cryptography (CSPRNG) gives much higher quality randomness, so it is the safe one for security work.

Why is Math.random unsafe for tokens?

Math.random() is unsafe for tokens because it does not give cryptographically secure random numbers. The MDN documentation says it plainly: do not use it for anything related to security.

The MDN page for Math.random() says the function returns a floating-point, pseudo-random number that is greater than or equal to 0 and less than 1, and points you to the Web Crypto API instead, and more precisely to crypto.getRandomValues().

The mistake is human and simple. A developer needs a random string for a reset link, finds Math.random() right there, and uses it. The link arrives, the user resets the password, and nobody notices the token came from a generator that was never made for this job. The same thing happens with session IDs, invite links and email confirmation codes.

What do you use instead in each language?

Each language has a secure function next to the regular one, and the OWASP cheat sheet itself names them:

Language

Do not use for security

Use

Node.js

Math.random()

crypto.randomBytes(), crypto.randomInt() and crypto.randomUUID()

PHP

rand(), mt_rand() and uniqid()

random_bytes() and random_int()

Python

random()

secrets

Java

java.util.Random

java.security.SecureRandom

In Node.js the swap is one line:

import { randomUUID } from 'node:crypto';

const resetToken = randomUUID();

In the browser you have crypto.getRandomValues(), which fills the array you give it with cryptographically strong random values. The same page says to prefer generateKey() for encryption keys themselves.

Is a UUID safe as a token?

A UUID is not always safe as a token. Version 1 is built from a high precision timestamp and the MAC address of the machine, so it is not random. Version 4 is random, but whether it comes from a secure generator depends on the implementation.

The OWASP cheat sheet says this in the same section on generators, and asks you not to rely on the randomness of UUIDs unless you know it is secure in your language or framework. So crypto.randomUUID() in Node.js is fine, because the cheat sheet lists it as secure, while a UUID library you know nothing about needs a check first.

How do you find the mistake in your code today?

You find it by locating every regular random function in the project, and asking of each one whether the value it makes protects something.

  1. Search the project for Math.random, rand(, mt_rand(, uniqid( and random.random.

  2. For each result, ask: does this value protect something? A reset token, a session ID, an invite link, a confirmation code, or a file name nobody should guess.

  3. If yes, swap the function for the secure one from the table in the same language.

  4. Tokens made before the fix that still work, like open reset links, should be cancelled so the user asks for a new one.

If your reset token is already safe, carry on with the rest of the password reset flow and with renewing the session ID after login, because both rely on a number nobody can guess.

Where do you start now?

Open the code that builds the password reset link in your app, and check which function makes the token. If it is Math.random() or rand(), swap it today for the secure function from the table. And store the passwords themselves with Argon2id, not only the tokens.

If you want someone to test your platform's tokens and sessions the way an attacker would, there is Muhassan. Which function makes your tokens right now?

FAQ

Is Math.random fine for games and everyday things?

Yes. Shuffling items, a random colour or a game is fine. The problem is only when the number protects something, like a token or a session ID.

Does crypto.getRandomValues work in the browser?

Yes. It is part of the Web Crypto API, and MDN says it gives cryptographically strong random values.

What do I use for encryption keys themselves?

Per MDN, prefer generateKey for key generation over getRandomValues, because it is guaranteed to run in a secure context.

Share

More articles