Skip to content

Entropy and tokens

In short

The threat

A token an attacker can predict, or whose every value they can try, is a password they can guess.

The rule

Mint every credential from the platform’s secure random source, with more bits than any search can cover, and ration the short codes a person has to type.

In the package
crypto/random.ts mints tokens; tokens/one-time-tokens.ts rations codes.

A token is only as strong as the number of values it could have been. If an attacker can predict how it was made, or can try every value it could take, the token is a password they can guess. Three shortcuts make that easy:

  • Math.random is built for speed, not secrecy. Its output comes from a small internal state, and someone who sees a few outputs can work out that state and every value that follows.
  • A UUID is an identifier format, not a secret. The time-based versions are partly a timestamp (and, in some, the machine’s network address). For the random version, a secure source is a recommendation to implementations, not a guarantee, and the UUID specification itself says not to use UUIDs as security capabilities.
  • A small space is a small search. A timestamp, a counter or a short random value, hashed or not, has so few possible values that an attacker can try them all.
Every bit of a token is random, so there is nothing to predict or parse, and a value of any other shape never reaches the store.

generateToken fills 32 bytes from crypto.getRandomValues, 256 bits, and writes each byte as two lowercase hex characters, so a token is 64 characters that encode nothing: no user id, no expiry, no counter. When a value comes back, isTokenShape checks that it is exactly 64 lowercase hex characters. Junk, a guess of another shape or a UUID is treated as unknown without a store lookup. A token of the right shape is hashed with SHA-256 and looked up; the shape check is a filter, and only the lookup grants anything.

Text version

Draw from a secure source, use enough of it, and turn away anything that was not made that way.

Every token: random bytes from a secure source

Section titled “Every token: random bytes from a secure source”

Every token the package hands out as a credential is 32 bytes from the platform’s secure random source. generateToken fills 32 bytes with crypto.getRandomValues and writes them as 64 lowercase hex characters: 256 bits, far beyond what any online or offline search can cover.

Every kind of token. Session tokens, email verification and password reset links, the ticket a reset code is exchanged for, email change and revert links, the two-factor plugin’s login challenges and trusted-device tokens, and invitation tokens are all made this way.

Nothing else goes in. Math.random is never used. A token encodes nothing: no user id, no expiry, no counter.

Signed values: the signature protects them

Section titled “Signed values: the signature protects them”

The lockout’s trusted-device cookie and the CSRF double-submit token each carry a random id of 16 bytes (128 bits) next to an HMAC.

  • The device cookie’s HMAC binds the id to the user, the revocation epoch and the issue time, under a key derived from the server secret.
  • The CSRF token’s HMAC binds it to the session or pre-session, under the CSRF secret.

Forging either means forging the HMAC, so the id’s length is not what stops a guess; it only has to keep two values from colliding.

Anything else is turned away before the store sees it

Section titled “Anything else is turned away before the store sees it”

A value that is not exactly 64 lowercase hex characters is treated as unknown without a lookup (isTokenShape), so junk never becomes a store key. That is a filter, not a check: only the store lookup grants anything.

A code a person types is short, so it is rationed

Section titled “A code a person types is short, so it is rationed”

Equally likely codes. Verification, reset and email change codes have 8 digits by default (6 to 9 allowed). They are drawn by rejection sampling, so every code is equally likely: taking a random number modulo a power of ten would make low codes slightly more common.

Few tries, short life. No length a person can type resists a search on its own, so each code gets 5 attempts by default, counted atomically before the comparison, lives 15 minutes by default, and is deleted when its attempts run out (A013).

Password reset and Email verification cover the per-address budgets on top.

Second-factor secrets follow the same rule

Section titled “Second-factor secrets follow the same rule”

TOTP secrets are 20 bytes (160 bits), the size the HOTP standard recommends. Recovery codes are 10 codes of 10 characters each from a 32-symbol alphabet, 50 bits per code, and every guess spends the account’s second-factor budget (A042). See Second factors.

createAuth refuses a shorter secret. The check counts bytes, not randomness: a 32-character sentence passes it and is still guessable. Generate the secret from a secure source, for example with openssl rand -hex 32, and keep it out of the repository.

What a token is, what the shape filter turns away, and where the server secret comes from.

entropy-and-tokens.ts
import { randomUUID } from 'node:crypto';
import { generateToken, isTokenShape } from '@noctcore/auth';
// Every bearer secret the package mints: 32 bytes from
// crypto.getRandomValues, written as 64 lowercase hex characters.
export const token = generateToken();
// A filter, not a check: the wrong shape is unknown without a store lookup.
export const shapes = {
token: isTokenShape(token), // true
upperCase: isTokenShape(token.toUpperCase()), // false
uuid: isTokenShape(randomUUID()), // false: an identifier, not a secret
};
// The server secret is counted in bytes, not randomness. Generate it from a
// secure source, once, and keep it out of the repository:
// openssl rand -hex 32
const value = process.env.AUTH_SECRET;
if (!value) throw new Error('Set AUTH_SECRET');
export const secret = value;

The guide recommends a 32-byte session secret from a cryptographically secure source, kept beside a separate session id. The package uses the same size and source for every bearer secret it mints, as a single token without the separate id (see Sessions and expiry). For codes a person types, which the sessions page does not cover, the package relies on the attempt cap and the expiry above, not on length.

Test Attack Stopped by
A013 Guessing a short code tokens/one-time-tokens.ts, flows/password-reset.ts
A042 Brute forcing a recovery code plugins/two-factor/recovery-codes.ts, plugins/two-factor/proofs.ts, throttle/throttle.ts