Should not allow file upload if anyone changes extension from exe to png via multer in node js application

隐身守侯 提交于 2019-12-06 02:35:31
rsp

In your route handler when you have the saved file name, you can use the mmmagic module:

var mmm = require('mmmagic'),
var magic = new mmm.Magic(mmm.MAGIC_MIME_TYPE);
magic.detectFile(fileName, function (err, mime) {
  if (err) {
    // handle error
  } else {
    // check the mime
    // and remove the file if you don't like it
    // plus send a correct response to the client
  }
});

Update

If mmmagic doesn't work for you then you can use the file-type module but it works on buffers so you first will have to read the file (or some part of it) into a buffer and check the mime type with file-type. The read-chunk module can be handy to read part of the file.

See:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!