簡體   English   中英

使用 nodejs 驗證文件上傳

[英]validating files upload with nodejs

我有下面的代碼用於將圖像上傳到服務器。 我需要在生產中部署代碼。 目前,代碼僅根據 mimetypes 文件格式進行驗證,並確保任何上傳的文件都以 .png 作為擴展名。 我知道這種安全性還不夠,因為有人很容易損壞圖像標題等。 我還發現一些黑客現在使用一些工具(如 glimp 等)嵌入可執行代碼。請有人幫助我使此代碼更安全,以防止文件上傳攻擊

exports.photo = function(req, res){
var multer  =   require('multer');
var storage =   multer.diskStorage({
  destination: function (req, file, callback) {
    


if(file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg'){
                    res.send("Supported image files are jpeg, jpg, and png");
                    return false;
                }
callback(null, './uploads');

  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '-' + Date.now() +'.png');
  },

});


var upload = multer({ storage : storage},{limits : {fieldNameSize : 20}}).single('userPhoto');
    upload(req,res,function(err) {


        if(err) {
            return res.end("Error uploading file.");
        }

console.log(req.files);
console.log(req.file);
        res.end("File is uploaded");
    });

}

你可以試試這個模塊Image-Type來檢查圖像格式。 這樣,如果有人試圖破壞圖像,該模塊將無法識別有效格式並阻止它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM