使用 multer 上传文件漏洞

Son*_*ngh 5 node.js express

我正在使用 multer 在我的应用程序中上传文件。\n例如: https: //github.com/expressjs/multer/blob/master/README.md

\n\n

路径遍历漏洞可能存在吗?如果是,请告诉我们如何阻止这种情况?

\n\n

目前,我正在使用下面的代码。我只是想确认一下,是否存在文件漏洞风险?请给出意见。

\n\n
var storage = multer.diskStorage({\ndestination: function(req, file, cb) {\n    cb(null, \'uploads/\');\n},\nfilename: function (req, file, cb) {\n    console.log(file);\n    var ext = file.originalname.split(\'.\').pop();\n    cb(null, Date.now() + \'_\' + file.originalname);\n}});\n\nconst fileFilter = (req, file, cb) => {\n    // Accept or reject the file based on if MIME type is found in accepted list\n    if (acceptedMimeTypes.indexOf(file.mimetype) < 0) {\n        return cb("\xe3\x82\xa8\xe3\x83\xa9\xe3\x83\xbc\xef\xbc\x9a\xe3\x81\x93\xe3\x81\xae\xe3\x82\xbf\xe3\x82\xa4\xe3\x83\x97\xe3\x81\xae\xe3\x83\x95\xe3\x82\xa1\xe3\x82\xa4\xe3\x83\xab\xe3\x81\xaf\xe3\x82\xa2\xe3\x83\x83\xe3\x83\x97\xe3\x83\xad\xe3\x83\xbc\xe3\x83\x89\xe3\x81\xa7\xe3\x81\x8d\xe3\x81\xbe\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82ZIP\xe5\xbd\xa2\xe5\xbc\x8f\xe3\x81\xa8LZH\xe5\xbd\xa2\xe5\xbc\x8f\xe3\x81\xae\xe3\x83\x95\xe3\x82\xa1\xe3\x82\xa4\xe3\x83\xab\xe3\x81\xae\xe3\x81\xbf\xe3\x82\xa2\xe3\x83\x83\xe3\x83\x97\xe3\x83\xad\xe3\x83\xbc\xe3\x83\x89\xe3\x81\x97\xe3\x81\xa6\xe3\x81\x8f\xe3\x81\xa0\xe3\x81\x95\xe3\x81\x84\xe3\x80\x82", false) // Error: You can\'t upload files of this type. Please upload images and zips only.\n    }\n    else {\n        return cb(null, true);\n    }\n}\nvar upload = multer({ storage: storage, fileFilter: fileFilter, limits: { fileSize: 1024 * 1024 * 1 } }).single(\'file\');\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果存在任何风险,请建议我使用expressjs multer 更好的方法。

\n

Mah*_*hdi 0

您可以使用此代码修改文件名,这样就没有人可以威胁您:

const storage = multer.diskStorage({
  destination: './public',
  filename(req, file, cb) {
    cb(null, 'Your File Name');
  },
});
Run Code Online (Sandbox Code Playgroud)

randomatic您还可以使用和使其动态化,time如下所示:

const storage = multer.diskStorage({
  destination: './public',
  filename(req, file, cb) {
    cb(null, `${new Date().getTime()}`);
  },
});
Run Code Online (Sandbox Code Playgroud)