问题描述
我怎样才能在那个代码中解决这个问题。我尝试了一些方法,但我无法通过 checkmarx 测试(用于执行扫描的系统)
FinalUploadFolder 来自 WebConfig 文件,这是文件的保存位置
public FileResult Index(string attachedFile)
{
string rootPath = System.Configuration.ConfigurationManager.AppSettings.Get("FinalUploadFolder");
byte[] file= System.IO.File.ReadAllBytes(string.Format(Path.Combine(rootPath,attachedFile.ToString())));
return File(file,System.Net.Mime.MediaTypeNames.Application.Octet,attachedFile.ToString());
}
解决方法
验证和清理输入是安全编码的最佳实践。 Checkmarx 会寻找很多“消毒剂”,Path.GetFilename 就是其中之一。
另外,我认为Checkmarx更可能关注的是attachedFile,恶意输入可能会传入参数中。因此,请尝试使用以下内容更改您的代码:
public FileResult Index(string attachedFile)
{
attachedFile = Path.GetFileName(attachedFile);
string rootPath = System.Configuration.ConfigurationManager.AppSettings.Get("FinalUploadFolder");
byte[] file= System.IO.File.ReadAllBytes(string.Format(Path.Combine(rootPath,attachedFile.ToString())));
return File(file,System.Net.Mime.MediaTypeNames.Application.Octet,attachedFile.ToString());
}