2026年6月11日 星期四

[研究]Mend (WhiteSource)對 Path Traversal建議導致程式異常

[研究]Mend (WhiteSource)對 Path Traversal建議導致程式異常

2025-06-11

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20.2

********************************************************************************

因為 Path/Directory Traversal,Mend建議把


string fileName = fd + FileUpload1.FileName;
ㄍㄞ

改成


string fileName = fd + FileUpload1.FileName; string normalizedPath = Path.GetFullPath(fileName); if (!normalizedPath.StartsWith(fd)) { throw new InvalidOperationException("Invalid file path."); }

結果功能異常。

********************************************************************************

做了些研究,

(1)參考這篇 (此狀況下,非必須改)

[研究]Web.Config 中路徑變數最後要加上 倒斜線嗎 ?
https://shaurong.blogspot.com/2026/06/webconfig.html

路徑結合推薦用 string file = Path.Combine(folder, "test.jpg");

(2)Path.GetFullPath 能正規化路徑,Mend 只對 fileName 做,沒對 fd 做。

程式改成下面


string fileName = fd + FileUpload1.FileName; string normalizedPath = Path.GetFullPath(fileName); if (!normalizedPath.StartsWith(GetFullPath(fd)))
{ throw new InvalidOperationException("Invalid file path."); }

結果功能異常。

實際測試正常了ˊ。

(完)

相關

沒有留言:

張貼留言