【发布时间】: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