| 属性 | 值 |
|---|---|
| 规则 ID | CA5389 |
| Title | 请勿将存档项的路径添加到目标文件系统路径中 |
| 类别 | Security |
| 修复是中断修复还是非中断修复 | 非破坏性 |
| 在 .NET 10 中默认启用 | 否 |
| 默认在 .NET 11 中启用 | 否 |
| 适用的语言 | C# 和 Visual Basic |
原因
将非净化的源文件路径用作以下参数之一中的目标文件路径:
- 方法
destinationFileName的参数 ZipFileExtensions.ExtractToFile - 方法
path的参数 File.Open - 方法 File.OpenWrite 的参数
path - 方法
path的参数 File.Create -
FileStream 的构造函数的参数
path -
fileName的构造函数的参数 FileInfo
默认情况下,此规则会分析整个代码库,但这是可配置的。
规则说明
文件路径可以是相对的,并且可能导致文件系统访问预期文件系统目标路径以外的内容,从而导致攻击者通过“布局和等待”技术恶意更改配置和执行远程代码。
如何修复违规
不要使用源文件路径来构造目标文件路径,或确保提取路径上的最后一个字符是目录分隔符。
何时抑制警告
如果源路径始终来自受信任的源,则可以禁止显示此警告。
抑制警告
如果只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用该规则。
#pragma warning disable CA5389
// The code that's violating the rule is on this line.
#pragma warning restore CA5389
若要对文件、文件夹或项目禁用该规则,请在配置文件中将其严重性设置为none。
[*.{cs,vb}]
dotnet_diagnostic.CA5389.severity = none
有关详细信息,请参阅如何禁止显示代码分析警告。
配置代码以进行分析
使用下面的选项来配置要对此规则运行的代码库部分。
你可以仅针对此规则、针对适用的所有规则或针对此类别(安全)中适用的所有规则配置这些选项。 有关详细信息,请参阅代码质量规则配置选项。
排除特定符号名称
你可以通过设置 excluded_symbol_names 选项从分析中排除特定符号,例如类型和方法。 例如,若要指定规则不应针对名为 MyType 的类型中的任何代码运行,请将以下键值对添加到项目中的 .editorconfig 文件:
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType
注意
将 XXXX 的 CAXXXX 部分替换为适用规则的 ID。
选项值中允许的符号名称格式(用 | 分隔):
- 仅符号名称(包括具有相应名称的所有符号,不考虑包含的类型或命名空间)。
- 完全限定的名称,使用符号的文档 ID 格式。 每个符号名称都需要带有一个符号种类前缀,例如方法使用
M:、类型使用T:、命名空间使用N:。 -
.ctor用于构造函数和.cctor静态构造函数。
示例:
| 选项值 | 总结 |
|---|---|
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType |
匹配名为 MyType 的所有符号。 |
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType1|MyType2 |
匹配名为 MyType1 或 MyType2 的所有符号。 |
dotnet_code_quality.CAXXXX.excluded_symbol_names = M:NS.MyType.MyMethod(ParamType) |
匹配带有指定的完全限定签名的特定方法 MyMethod。 |
dotnet_code_quality.CAXXXX.excluded_symbol_names = M:NS1.MyType1.MyMethod1(ParamType)|M:NS2.MyType2.MyMethod2(ParamType) |
匹配带有各自的完全限定签名的特定方法 MyMethod1 和 MyMethod2。 |
排除特定类型及其派生类型
你可以通过设置 excluded_type_names_with_derived_types 选项从分析中排除特定类型及其派生类型。 例如,若要指定规则不应针对名为 MyType 的类型及其派生类型中的任何方法运行,请将以下键值对添加到项目中的 .editorconfig 文件:
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType
注意
将 CAXXXX 中的 XXXX 部分替换为适用规则的 ID。
选项值中允许的符号名称格式(用 | 分隔):
- 仅类型名称(包括具有相应名称的所有类型,不考虑包含的类型或命名空间)。
- 完全限定的名称,使用符号的文档 ID 格式,前缀为
T:(可选)。
示例:
| 选项值 | 摘要 |
|---|---|
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType |
匹配名为 MyType 的所有类型及其所有派生类型。 |
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType1|MyType2 |
匹配名为 MyType1 或 MyType2 的所有类型及其所有派生类型。 |
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = M:NS.MyType |
匹配带有给定的完全限定名称的特定类型 MyType 及其所有派生类型。 |
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = M:NS1.MyType1|M:NS2.MyType2 |
匹配带有各自的完全限定名称的特定类型 MyType1 和 MyType2 及其所有派生类型。 |
示例
下面的代码片段演示了此规则可检测的情况。
违规:
using System.IO.Compression;
class TestClass
{
public void TestMethod(ZipArchiveEntry zipArchiveEntry)
{
zipArchiveEntry.ExtractToFile(zipArchiveEntry.FullName);
}
}
解决方案:
using System;
using System.IO;
using System.IO.Compression;
class Program
{
static void Main(string[] args)
{
string zipPath = @".\result.zip";
Console.WriteLine("Provide path where to extract the zip file:");
string extractPath = Console.ReadLine();
// Normalizes the path.
extractPath = Path.GetFullPath(extractPath);
// Ensures that the last character on the extraction path
// is the directory separator char.
// Without this, a malicious zip file could try to traverse outside of the expected
// extraction path.
if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
extractPath += Path.DirectorySeparatorChar;
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
{
// Gets the full path to ensure that relative segments are removed.
string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));
// Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
// are case-insensitive.
if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
entry.ExtractToFile(destinationPath);
}
}
}
}
}