c# - 在这个文件上传功能中如何检查文件是否存在?

标签 c# asp.net security file-upload

我正在尝试按照我的编程老师的要求开发安全的上传文件功能。我以这样的方式实现它,它会检查文件的大小、文件格式和文件是否存在。除了检查文件是否存在之外,逻辑运行良好。例如,当我尝试上传一个已经存在的文件时,我不会收到一条消息告诉我该文件已经存在,而且我不知道为什么它不起作用。

protected void UploadFile(object sender, EventArgs e)
    {
        if(FileUpload1.HasFile)
            try 
            {
                string[] validTypes = { "bmp", "gif"};
                string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);

                if (size < limit) 
                {
                    for (int i = 0; i < validTypes.Length; i++)
                    {
                        if (ext == "." + validTypes[i])
                        {
                            string path = @"~\Images\"; 
                            string comPath = Server.MapPath(path + "\\" + FileUpload1.FileName);
                            if (!File.Exists(comPath))
                            {
                                FileUpload1.PostedFile.SaveAs(comPath);
                                Label1.Text = "File uploaded";
                            }
                            else
                            {
                                Label1.Text = "Existed";
                            }
                        }
                        else 
                        {
                            Label1.Text = "Invalid File." + string.Join(",", validTypes);
                        }
                    }                         
                }

                else 
                {
                    Label2.ForeColor = System.Drawing.Color.Red;
                    Label2.Text = "file is heavy";
                }
            }

            catch (Exception ex)
            {
                Label2.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
            }
}

当我调试代码时,我发现它会执行else语句,但它不会将其显示给用户,而是会在外部else语句中显示消息“无效文件”。 为什么?

if (ext == "." + validTypes[i])
                            {
                                string path = @"~\Images\"; 
                                string comPath = Server.MapPath(path + "\\" + FileUpload1.FileName);
                                if (!File.Exists(comPath))
                                {
                                    FileUpload1.PostedFile.SaveAs(comPath);
                                    Label1.Text = "File uploaded";
                                }
                                else
                                {
                                    Label1.Text = "Existed";
                                }
                            }
                            else 
                            {
                                Label1.Text = "Invalid File." + string.Join(",", validTypes);
                            }

此外,我的导师告诉我,以下行会导致称为路径遍历的漏洞。

string path = @"~\Images\"; 

那么如何防止这个安全漏洞呢? ?有什么想法吗?

最佳答案

您的代码中存在逻辑问题。
在 block 中

for (int i = 0; i < validTypes.Length; i++)

每个文件始终会运行两次。

你可以做什么,将一个 bool 变量设置为 false。
进入循环内部,如果找到文件,则将 bool 值设置为 true 并使用break语句。
在循环结束时检查 bool 值和相应的代码。

编辑-1

您可以像这样使用而不是循环遍历数组

string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
int pos = Array.IndexOf(stringArray, value);
if (pos >- 1)
{
    // the array contains the string and the pos variable
    // will have its position in the array
}

就你的情况

 string[] validTypes = { "bmp", "gif"};
 string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
 int pos = Array.IndexOf(validTypes , ext );
 if(pos>=0)
 {
     string path = @"~\Images\"; 
     string comPath = Server.MapPath(path + "\\" + FileUpload1.FileName);
     if (!File.Exists(comPath))
     {
         FileUpload1.PostedFile.SaveAs(comPath);
         Label1.Text = "File uploaded";
     }
     else
     {
         Label1.Text = "Existed";
     }
 }
 else
 {
    Label1.Text = "Invalid File." + string.Join(",", validTypes);
 }

关于c# - 在这个文件上传功能中如何检查文件是否存在?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15519628/

相关文章:

html - 在经典 ASP 中自动 HTML 转义变量输出的机制?

mysql - 如何正确使用 AES_ENCRYPT?

c# - 在c#中运行cmd命令

c# - 以编程方式添加具有数据库权限的角色

c# - 切换 Windows 时 KeyBinding 不起作用

javascript - 使用 jQuery 时如何在 ASP.NET 中同时触发 OnClick 和 OnClientClick 事件?

asp.net - 如何解决 ASP .Net 中的 “This is an invalid webresource request” 错误

c# - 如何在 C# 中的 Windows 应用程序中创建单个文件设置?

c# - .net Framework 4.0 中地理 sql server 数据类型的 C# 等价物是什么?

security - 有没有办法让HTTP请求头不可变?