值
规则 ID
CA3003
类别
安全性
修复是中断修复还是非中断修复
非中断
原因
可能有不受信任的 HTTP 请求输入访问文件操作的路径。
默认情况下,此规则会分析整个代码库,但这是可配置的。
规则说明
在处理来自 Web 请求的不受信任的输入时,请谨慎使用用户控制的输入指定文件路径。 攻击者可能能够读取非预期文件,从而导致敏感数据出现信息泄漏。 或者,攻击者可能能够写入非预期文件,从而导致在未经授权的情况下修改敏感数据,或者降低服务器的安全性。 常见的攻击者技术是使用路径遍历访问预期目录之外的文件。
此规则试图查找 HTTP 请求中要访问文件操作中路径的输入。
备注
此规则无法跨程序集跟踪数据。 例如,如果一个程序集读取 HTTP 请求输入,然后将其传递给另一个会写入某个文件的程序集,则此规则不会产生警告。
备注
对于此规则跨方法调用分析数据流的深入程度存在限制,此限制是可配置的。 若要了解如何在 EditorConfig 文件中配置此限制,请参阅分析器配置。
如何解决冲突
尽可能将基于用户输入的文件路径限制在显式已知安全列表的范围内。 例如,如果应用程序只需访问“red.txt”、“green.txt”或“blue.txt”,则只允许这些值。
检查是否存在不受信任的文件名,并验证名称格式是否正确。
指定路径时使用完整路径名称。
避免潜在的危险构造,如路径环境变量。
如果用户提交短名称,则只接受长文件名并验证长名称。
将最终用户输入限制在有效字符范围内。
拒绝超出 MAX_PATH 长度的名称。
按字面处理文件名,不执行解释。
确定文件名是否表示文件或设备。
何时禁止显示警告
如果你已按照上一部分中所述验证输入,则可以禁止显示此警告。
配置代码以进行分析
使用下面的选项来配置代码库的哪些部分要运行此规则。
排除特定符号
排除特定类型及其派生类型
你可以仅为此规则、为所有规则或为此类别(安全性)中的所有规则配置这些选项。 有关详细信息,请参阅代码质量规则配置选项。
排除特定符号
可以从分析中排除特定符号,如类型和方法。 例如,若要指定规则不应针对名为 MyType 的类型中的任何代码运行,请将以下键值对添加到项目中的 .editorconfig 文件:
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType
选项值中允许的符号名称格式(用 | 分隔):
仅符号名称(包括具有相应名称的所有符号,不考虑包含的类型或命名空间)。
完全限定的名称,使用符号的文档 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。
排除特定类型及其派生类型
可以从分析中排除特定类型及其派生类型。 例如,若要指定规则不应针对名为 MyType 的类型及其派生类型中的任何代码运行,请将以下键值对添加到项目中的 .editorconfig 文件:
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType
选项值中允许的符号名称格式(用 | 分隔):
仅类型名称(包括具有相应名称的所有类型,不考虑包含的类型或命名空间)。
完全限定的名称,使用符号的文档 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;
using System.IO;
public partial class WebForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string userInput = Request.Params["UserInput"];
// Assume the following directory structure:
// wwwroot\currentWebDirectory\user1.txt
// wwwroot\currentWebDirectory\user2.txt
// wwwroot\secret\allsecrets.txt
// There is nothing wrong if the user inputs:
// user1.txt
// However, if the user input is:
// ..\secret\allsecrets.txt
// Then an attacker can now see all the secrets.
// Avoid this:
using (File.Open(userInput, FileMode.Open))
{
// Read a file with the name supplied by user
// Input through request's query string and display
// The content to the webpage.
}
}
}
Imports System
Imports System.IO
Partial Public Class WebForm
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim userInput As String = Me.Request.Params("UserInput")
' Assume the following directory structure:
' wwwroot\currentWebDirectory\user1.txt
' wwwroot\currentWebDirectory\user2.txt
' wwwroot\secret\allsecrets.txt
' There is nothing wrong if the user inputs:
' user1.txt
' However, if the user input is:
' ..\secret\allsecrets.txt
' Then an attacker can now see all the secrets.
' Avoid this:
Using File.Open(userInput, FileMode.Open)
' Read a file with the name supplied by user
' Input through request's query string and display
' The content to the webpage.
End Using
End Sub
End Class
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系外文翻译,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。