c# - ASP.NET - Path Traversel exploit when downloading a File
问题描述
How could I solve this problem in that code. I've tried some approaches, but I couldn't pass the checkmarx test (system used to perform the scan)
FinalUploadFolder comes from the WebConfig file, which is where the files are saved
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 就是其中之一。
另外,我相信 attachFile 是 Checkmarx 更可能关心的,并且有可能将恶意输入传递到参数中。因此,请尝试使用以下内容更改您的代码:
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());
}
推荐阅读
- php - 如何在 PHP 中使用 WCF 服务?
- javascript - 使用公共 ACM 在 AWS 上部署 Expressjs
- android - 使用 Mapbox SDK for Android 开始地理围栏的任何可能方式
- php - 在 Genesis Child Theme Wordpress 中创建全高移动菜单
- imap - 应该使用什么 url 来使用 libcurl 选择 imap 邮箱
- python - 使用 Pyplot 散布不透明度
- java - 错误:无法找到或加载主类 discord.java.bot.JavaDiscordBot
- javascript - VueJs - 如何在 v-if 中使用变量?
- reactjs - 使用 ReactJS 禁用点击
- excel - 调用goal seek sub的场景分析宏