2013-07-01 164 views
0

我有一個C#應用程序,我在上傳圖像並將其存儲在目錄中。我正在檢查文件,以便在上傳前允許使用一種圖像格式。當我測試我的Veracode的模塊就說明存在目錄遍歷問題CWE ID 73 ..我不知道,如果它的正確的,因爲我上載目錄遍歷問題c#

前檢查文件類型

我上傳文件的代碼如下

if (filebigimage.HasFile) 
{ 
    if ((((((this.filebigimage.PostedFile.ContentType == "image/gif") | (this.filebigimage.PostedFile.ContentType == "image/pjpeg")) | (this.filebigimage.PostedFile.ContentType == "image/jpeg")) | (this.filebigimage.PostedFile.ContentType == "image/jpg")) | (this.filebigimage.PostedFile.ContentType == "image/png")) | (this.filebigimage.PostedFile.ContentType == "image/bmp")) 
         { 
          // string StrBigImageName = (ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/Temp/"); 
          if ((this.filebigimage.PostedFile.ContentType == "image/gif")) 
          { 
           strBigimgformat = ".gif"; 
           objBigImgFormat = System.Drawing.Imaging.ImageFormat.Gif; 
          } 
          else if ((this.filebigimage.PostedFile.ContentType == "image/pjpeg") | (this.filebigimage.PostedFile.ContentType == "image/jpeg") | (this.filebigimage.PostedFile.ContentType == "image/jpg")) 
          { 
           strBigimgformat = ".jpg"; 
           objBigImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg; 

          } 
          else if ((this.filebigimage.PostedFile.ContentType == "image/png")) 
          { 
           strBigimgformat = ".png"; 
           objBigImgFormat = System.Drawing.Imaging.ImageFormat.Png; 
          } 
          filebigimage.SaveAs(Server.MapPath(ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/Temp/") + filebigimage.FileName); 
          strbigimagename = filebigimage.FileName; 
         } 
} 

那麼我將值存儲到數據庫中,並獲得該行的ID號,並創建一個文件名,並從臨時文件移動到主文件夾

if (File.Exists(Server.MapPath(ConfigurationManager.AppSettings["offerImagePath"] 
+ "BigImages/Temp/" + strbigimagename))) 
{ 
    File.Move(Server.MapPath("../" + ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/Temp/" 
+ strbigimagename), Server.MapPath("../" + ConfigurationManager.AppSettings["offerImagePath"] + "BigImages/" + strorderid + "_bigimage" + strBigimgformat)); 
} 

請讓我知道什麼是錯了,...它是否容易受到目錄遍歷I的影響? ssue

回答

0

它看起來像你正在使用用戶輸入(filebigimage.FileName)作爲您的服務器上的文件名的一部分。這是一個安全風險。你應該想出其他一些命名文件的方法。

+0

我使用的是消毒功能現在 –

+0

私人靜態字符串MakeValidFileName(字符串名稱){ 串 = invalidChars Regex.Escape(新的字符串(Path.GetInvalidFileNameChars())); string invalidReStr = string.Format(@「[{0}] +」,invalidChars); return Regex.Replace(name,invalidReStr,「_」); } ..........它是否有助於解決問題 –

+0

即使您清理了輸入,您仍可能遇到碰撞問題(例如,不同的用戶使用相同的文件名)。相反,您可以使用數據庫行ID或GUID將文件保存到服務器;將用戶的原始文件名存儲在數據庫中的其他位置,以便在需要時恢復它。 – RogerN