docs: use crypto.randomBytes in DiskStorage filename example#1417
Open
luisangelrod wants to merge 1 commit into
Open
docs: use crypto.randomBytes in DiskStorage filename example#1417luisangelrod wants to merge 1 commit into
luisangelrod wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The README's
DiskStorageexample usesMath.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 withDate.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.jsusescrypto.randomBytes(16). This PR aligns the README example with that existing best practice.Changes
crypto(Node.js built-in, no new dependency)Date.now() + Math.random()withcrypto.randomBytes(16)Math.random()is inappropriate hereCloses #1386