Skip to content

docs: use crypto.randomBytes for custom filename example (#1386)#1392

Open
SAY-5 wants to merge 1 commit intoexpressjs:mainfrom
SAY-5:docs-crypto-random-filename
Open

docs: use crypto.randomBytes for custom filename example (#1386)#1392
SAY-5 wants to merge 1 commit intoexpressjs:mainfrom
SAY-5:docs-crypto-random-filename

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 15, 2026

Closes #1386.

The custom `diskStorage` example in the README uses `Date.now() + Math.round(Math.random() * 1E9)` to build the unique filename suffix. That's the pattern production apps copy when they need a custom `filename` to keep the file extension or add a field-based prefix, so the insecure form ends up in a lot of deployed code.

The issue is that:

  • `Math.random()` is not cryptographically secure — V8's xorshift128+ state is recoverable from a small number of outputs (~30 bits of entropy per call).
  • `Date.now()` contributes ~0 bits of unpredictability when the attacker knows roughly when a file was uploaded.
  • If uploads land in a web-accessible directory, the filename space is enumerable → cross-user access without authz.

Multer's own built-in default already uses `crypto.randomBytes(16)` (`storage/disk.js:6-9`). This PR just updates the README's custom-`filename` example to teach the same pattern so copy-paste users get the secure default by construction.

No code changes — docs only.

)

The custom `diskStorage` example in the README used
`Date.now() + Math.round(Math.random() * 1E9)` to build a unique
filename suffix, which is exactly the pattern production apps copy
when they need a custom `filename` (e.g. to preserve a file
extension). `Math.random()` is not cryptographically secure — V8's
xorshift128+ state can be recovered from a small sample of outputs —
and combined with `Date.now()` the entropy is effectively ~30 bits,
which is enumerable when uploads land in a web-accessible directory.

Multer's built-in default already uses `crypto.randomBytes(16)`
(`storage/disk.js:6-9`). Update the README to teach the same pattern
so copy-paste users get the secure default by construction. No code
changes. Docs only.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: README diskStorage example uses Math.random() for filenames instead of crypto-safe random

1 participant