【问题标题】:Resolve Veracode's CWE-73: "External control of file name or path" MVC application解决 Veracode 的 CWE-73:“文件名或路径的外部控制”MVC 应用程序
【发布时间】:2020-07-25 19:09:55
【问题描述】:

我的应用程序中有一个函数,我从 Veracode 的扫描中发现了一个缺陷 CWE-73。该函数用于遍历特定路径,获取一个文件的内容和文件夹中的文件列表:

private LogFile GetLogFileByName(string logFileName)
{
    string fileContents = string.Empty;
    string path = this.GetBasePath + "/Logs/" + logFileName;
    if (System.IO.File.Exists(path))
    {
        using (FileStream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                fileContents = reader.ReadToEnd();
            }
        }
    }
    return new LogFile {FileContents = fileContents, LogFileName = logFileName, LogFileNames = GetNames()};
}

我寻找可能的选项来解决这个缺陷,其中一个是“模式白名单”,它适用于我,所以我用以下方式重写了函数:

private LogFile GetLogFileByName(string logFileName)
{
    string fileContents = string.Empty;
    var regex = new System.Text.RegularExpressions.Regex(@"^log\.common\.txt(\d{4}.\d{2}.\d{2})?$");
    if (regex.IsMatch(logFileName))
    {
      string path = this.GetBasePath + "/Logs/" + logFileName;
      if (System.IO.File.Exists(path))
      {
          using (FileStream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
          {
              using (StreamReader reader = new StreamReader(stream))
              {
                  fileContents = reader.ReadToEnd();
              }
          }
      }
    }
    else {
      return new LogFile();
    }

    return new LogFile {FileContents = fileContents, LogFileName = logFileName, LogFileNames = GetNames()};
}

我使用正则表达式来检查一切是否在语法上正确。但是,问题仍然出现。任何想法,如何解决?我查看了 SO 中的几个帖子,但似乎没有任何适当的解释。

【问题讨论】:

    标签: c# asp.net-mvc veracode secure-coding


    【解决方案1】:

    Veracode 有一篇知识库文章,该文章链接自缺陷查看器,提供了一些有关如何解决的指示:

    https://downloads.veracode.com/securityscan/cwe/v5/net/73.html

    【讨论】:

      猜你喜欢
      • 2018-11-23
      • 2021-11-05
      • 2016-03-06
      • 2018-07-06
      • 2014-09-04
      • 2013-12-22
      • 1970-01-01
      • 2020-12-25
      • 1970-01-01
      相关资源
      最近更新 更多