# Math.random is not secure for secrets or tokens

> Math.random is not cryptographically secure; HFS CVE-2026-61500 shows the cost, and four calls replace it.

Source: https://fullbytedeveloper.com/blog/math-random-not-secure-tokens
Published here: 2026-12-01

Math.random() is not secure for secrets: MDN says it does not provide cryptographically secure random numbers and should not be used for anything related to security. [1] Rejetto HFS 3.0.0 through 3.2.0 built its session-cookie signing key from it, and CISA added that bug, CVE-2026-61500, to its list of exploited vulnerabilities on 2026-11-30. [2][3]

Related reading: [read what a JWT gives away to anyone](/blog/jwt-peek), [pick the OAuth flow that resists stolen codes](/blog/oauth-pkce) and [patch the Xen Orchestra empty-object login bypass](/blog/patch-xen-orchestra-empty-login-bypass). Browse the [Security topic](https://fullbytedeveloper.com/blog/topic/security).

## Update HFS to 3.2.1 or later

If you run HFS, this is the step with a deadline. VulnCheck rates the bug CVSS 9.3 and lists versions 3.0.0 through 3.2.0. [2] Version 3.2.1 was released on 2026-07-13, and its notes say "Multiple security vulnerabilities have been found in all previous versions, potentially allowing an attacker to gain administrative access to HFS." [4]

The newest release on the HFS releases page is 3.4.2, published 2026-11-24 and labelled "Important security update", so update to that rather than stopping at 3.2.1. [5]

CISA gave US federal agencies until 2026-12-03 to act and marked forensic triage as required. [3]

## Check whether someone already got in

Updating closes the hole. It does not undo an admin session an attacker already forged.

The VulnCheck advisory describes the attack: collect a few login responses, rebuild the state of the Math.random() generator from the values they leak, recover the signing key, forge an administrator cookie, then run code through the `server_code` configuration feature. [2] Read what is in `server_code` on your server and remove anything you did not put there.

The HFS 3.2.1 notes credit Zach Hanley of Horizon3.ai, "in collaboration with Claude and Anthropic Research". [4]

## Find Math.random in your own code

The HFS bug is not specific to file servers. Any session key, reset link, API token or invite code made with Math.random() has the same weakness. [1]

In a git repository, `git grep` lists every line that matches, with `-n` adding line numbers. [6]

```bash
git grep -n "Math.random"
```

Most hits will be harmless: shuffling a list, jitter on a retry, a random colour. Replace the ones that make something secret.

## Replace it with a secure generator

Every language here ships a generator meant for secrets. Use it for anything a stranger should not be able to guess.

In Node.js, `crypto.randomBytes()` "generates cryptographically strong pseudorandom data". [7]

```js
const { randomBytes } = require('node:crypto');
const buf = randomBytes(256);
```

In the browser, MDN points to `Crypto.getRandomValues()`, which fills a typed array in place. MDN also says to prefer `generateKey()` when you are making a key. [1][8]

```js
const array = new Uint32Array(10);
self.crypto.getRandomValues(array);
```

In Python, the `secrets` module is meant for this. The docs say to use it in preference to the `random` module, which is "designed for modelling and simulation, not security or cryptography". [9]

```python
import secrets
token = secrets.token_urlsafe()
```

In Go, `crypto/rand` is the secure generator. `rand.Text()`, added in Go 1.24, returns a random string with at least 128 bits of randomness, meant for a secret string, token or password. [10]

```go
import "crypto/rand"

token := rand.Text()
```

You now have one search that finds the risky calls and a secure replacement for each runtime.

Send this to the developer on your team whose reset links come from Math.random().

## Sources

1. [MDN: Math.random()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
2. [VulnCheck: Rejetto HFS < 3.2.1 session forgery via predictable signing key (CVE-2026-61500)](https://www.vulncheck.com/advisories/rejetto-hfs-session-forgery-via-predictable-signing-key)
3. [CISA: Known Exploited Vulnerabilities catalog, CVE-2026-61500](https://www.cisa.gov/known-exploited-vulnerabilities-catalog?field_cve=CVE-2026-61500)
4. [rejetto/hfs: release 3.2.1](https://github.com/rejetto/hfs/releases/tag/v3.2.1)
5. [rejetto/hfs: releases](https://github.com/rejetto/hfs/releases)
6. [Git docs: git grep](https://git-scm.com/docs/git-grep)
7. [Node.js docs: crypto.randomBytes](https://nodejs.org/api/crypto.html#cryptorandombytessize-callback)
8. [MDN: Crypto.getRandomValues()](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)
9. [Python docs: secrets](https://docs.python.org/3/library/secrets.html)
10. [Go docs: crypto/rand](https://pkg.go.dev/crypto/rand)
