[研究]ASP.NET, Fortify SCA 報告CreateDirectory()有 Portability Flaw: File Separator (可移植性 缺陷檔案分隔符) 問題
2024-03-11
環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 19
********************************************************************************
【Fortify SCA】Details
Abstract:
由於使用了 Hardcoded 檔案分隔符號,因此在第 15 行呼叫 CreateDirectory() 會造成可移植性問題。
Explanation:
不同的作業系統會使用不同的字元來做為檔案分隔符號。例如,Microsoft Windows 系統使用的是「\」,而 UNIX 系統則使用「/」。若要在不同的平台上執行應用程式,則 Hardcoded 檔案分隔符號會導致應用程式邏輯的執行不正確,與可能的 Denial of Service。
在此案例中,Default.aspx.cs 的第 15 行呼叫 CreateDirectory() 時使用了硬式編碼的檔案分隔符號。
範例 1:以下程式碼使用 Hardcoded 檔案分隔符號來開啟檔案:
FileStream f = File.Create(directoryName + "\\" + fileName);
敝人註: "\\" 可寫成 @"\"
********************************************************************************
【Fortify SCA】Recommendations:
為了寫入行動程式碼,就要避免使用 Hardcoded 檔案分隔符號。請使用語言程式庫所提供的平台獨立 API。
範例 2:以下程式碼實作與 Example 1 相同的功能,但使用平台獨立 API 來指定檔案分隔符號:
FileStream f = File.Create(directoryName + Path.DirectorySeparatorChar.ToString() + fileName);
|
*******************************************************************************
【ChatGPT】
User
ASP.NET, WebForm, 如何避免 Fortify SCA 報告下面程式有 Path Manipulation 問題
string fd = (string)ConfigurationManager.AppSettings["InformationSystemFiles"] + "\\";
if (!System.IO.Directory.Exists(fd))
{
System.IO.Directory.CreateDirectory(fd);
} |
Fortify SCA 指出的 Path Manipulation 問題可能是由於在構建檔案路徑時未使用安全的方式。為了避免這個問題,你可以改用 Path.Combine 方法來建構檔案路徑,這樣可以確保路徑被正確地合併。
這裡是如何修改你的程式碼:
string folderPath = ConfigurationManager.AppSettings["InformationSystemFiles"];
string fullPath = Path.Combine(Server.MapPath(folderPath), ""); // 使用 MapPath 將相對路徑轉換為絕對路徑
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
|
這樣做的好處是,Path.Combine 會處理路徑分隔符號和驅動器名稱的差異,確保生成的路徑是正確格式的。同時,使用 Server.MapPath 將相對路徑轉換為絕對路徑,這樣可以確保你的應用程序可以正確地找到指定的資源路徑。
這樣做可以幫助你避免 Fortify SCA 報告中指出的 Path Manipulation 問題。
********************************************************************************
【實際測試】
Web.Config
<?xml version="1.0" encoding="utf-8"?>
<!--
如需如何設定 ASP.NET 應用程式的詳細資訊,請前往
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.8" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
</compilers>
</system.codedom>
<appSettings>
<add key="TempFolder" value="D:\Production2\TempImageFiles\" /><!--有產生,Fortify SCA沒通過-->
<add key="TempFolder2" value="\TempImageFiles2\" /><!--有產生,Fortify SCA通過-->
<add key="TempFolder3" value="\TempImageFiles3\" /><!--沒產生-->
<add key="TempFolder4" value="D:\Production4\TempImageFiles\" /><!--沒產生-->
<add key="TempFolder5" value="D:\Production5\TempImageFiles\" /><!--有產生,Fortify SCA通過-->
<add key="TempFolder6" value="\TempImageFiles6\" /><!--沒產生-->
</appSettings>
</configuration>
|
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
|
Default.aspx.cs
using System;
using System.Configuration;
using System.IO;
namespace WebApplication1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//有產生,Fortify SCA 沒通過
string fd = (string)ConfigurationManager.AppSettings["TempFolder"] + "\\";
if (!System.IO.Directory.Exists(fd))
{
System.IO.Directory.CreateDirectory(fd);
}
//有產生,Fortify SCA 通過
string fd5 = (string)ConfigurationManager.AppSettings["TempFolder5"] + Path.DirectorySeparatorChar.ToString();
if (!System.IO.Directory.Exists(fd5))
{
System.IO.Directory.CreateDirectory(fd5);
}
//沒有產生,Fortify SCA 通過
string fd6 = (string)ConfigurationManager.AppSettings["TempFolder6"] + Path.DirectorySeparatorChar.ToString();
if (!System.IO.Directory.Exists(fd6))
{
System.IO.Directory.CreateDirectory(fd6);
}
//有產生,Fortify SCA 通過
string folderPath = ConfigurationManager.AppSettings["TempFolder2"];
string fullPath = Path.Combine(Server.MapPath(folderPath), ""); // 使用 MapPath 將相對路徑轉換為絕對路徑
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
//沒有產生,Fortify SCA 沒通過
string fd3 = (string)ConfigurationManager.AppSettings["TempFolder3"] + "\\";
if (!System.IO.Directory.Exists(fd3))
{
System.IO.Directory.CreateDirectory(fd3);
}
//System.Web.HttpException: ''D:/Production4/TempImageFiles/' 是實體路徑,但必須使用虛擬路徑。'
//string folderPath4 = ConfigurationManager.AppSettings["TempFolder4"];
//string fullPath4 = Path.Combine(Server.MapPath(folderPath4), ""); // 使用 MapPath 將相對路徑轉換為絕對路徑
//if (!Directory.Exists(fullPath4))
//{
// Directory.CreateDirectory(fullPath4);
//}
}
}
}
|
(完)
相關

沒有留言:
張貼留言