github / github/codeql

Possible false positive "Uncontrolled data used in path expression" still after stripping path from input.

未關閉
#18,225 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
false-positive
主要語言
CodeQL
星號
10.1k
分支
2.1k
平均合併
2 天 15 小時
30 天內合併 PR
141

描述

The .NET `System.IO.Path.GetFileName` will take a path, relative or otherwise, and return only the filename part. I am using this to sanitize the input to a Download action that serves files from a hardcoded folder. The initial code looked like this, which is in fact vulnerable:

```
[HttpGet]
public IActionResult Download(string fileName)
{
string filesDir = "Files\\FilesToServe";
string filePath = Path.Combine(filesDir, fileName);

if (System.IO.File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open);

return File(fs, "text/plain", fileName);
}

return NotFound();
}
```

Corrective action was taken so that only a file name with no additional path will be used to look for the file:

```
[HttpGet]
public IActionResult Download(string fileName)
{
string strippedFileName = Path.GetFileName(fileName);
string filesDir = "Files\\FilesToServe";
string filePath = Path.Combine(filesDir, strippedFileName);

if (System.IO.File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open);

return File(fs, "text/plain", strippedFileName);
}

return NotFound();
}
```

This still detects "Uncontrolled data used in path expression (cs/path-injection)" for `filePath` even though it has been tested to no longer serve files outside of `filesDir` when passing in parameters such as "../../../FileThatShouldntBeServed.txt".

貢獻指南

開啟貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。