Skip to content

docs: use crypto.randomBytes in DiskStorage filename example#1417

Open
luisangelrod wants to merge 1 commit into
expressjs:mainfrom
luisangelrod:docs/secure-diskstorage-filename-example
Open

docs: use crypto.randomBytes in DiskStorage filename example#1417
luisangelrod wants to merge 1 commit into
expressjs:mainfrom
luisangelrod:docs/secure-diskstorage-filename-example

Conversation

@luisangelrod

Copy link
Copy Markdown

Summary

The README's DiskStorage example uses Math.random() to generate unique filenames. This is a security antipattern that developers copy into production code, as documented in #1386.

Math.random() uses V8's xorshift128+ PRNG, which is not cryptographically secure — its state can be recovered from a small number of outputs. Combined with Date.now() (which an attacker can narrow to ~1 second), the generated filename provides only ~30 bits of unpredictable entropy. In web-accessible upload directories this is enough for an attacker to enumerate filenames and access other users' files.

Multer's own internal default already does this correctly — storage/disk.js uses crypto.randomBytes(16). This PR aligns the README example with that existing best practice.

Changes

  • Require crypto (Node.js built-in, no new dependency)
  • Replace Date.now() + Math.random() with crypto.randomBytes(16)
  • Add brief comments explaining why Math.random() is inappropriate here

Closes #1386

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