From 288273c0985040a1dbbb101a130796fdbbd0fa3b Mon Sep 17 00:00:00 2001 From: Hashim Khan Date: Thu, 23 Jul 2026 00:35:29 +0500 Subject: [PATCH] docs: use crypto.randomBytes in diskStorage README example Replace Math.random() in the custom filename example with crypto.randomBytes, matching multer's secure default filename generator. Fixes #1386 --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed2a4e6d..0ab40f49 100644 --- a/README.md +++ b/README.md @@ -207,13 +207,17 @@ 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) + crypto.randomBytes(16, function (err, raw) { + if (err) return cb(err) + cb(null, file.fieldname + '-' + raw.toString('hex')) + }) } })