diff --git a/README.md b/README.md index ed2a4e6d..64416ecc 100644 --- a/README.md +++ b/README.md @@ -207,13 +207,20 @@ where you are handling the uploaded files. The disk storage engine gives you full control on storing files to disk. ```javascript +const crypto = require('crypto') + const storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, '/tmp/my-uploads') }, filename: function (req, file, cb) { - const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9) - cb(null, file.fieldname + '-' + uniqueSuffix) + // Use crypto.randomBytes for a cryptographically secure unique name. + // Math.random() is NOT cryptographically secure and should be avoided + // for filenames in web-accessible upload directories. + crypto.randomBytes(16, function (err, raw) { + if (err) return cb(err) + cb(null, file.fieldname + '-' + raw.toString('hex')) + }) } })