CA1054:URI 参数不应为字符串

属性
规则 ID CA1054
标题 URI 参数不应为字符串
类别 设计
修复是中断修复还是非中断修复 重大
在 .NET 8 中默认启用

原因

类型声明一个方法,该方法具有名称中包含“uri”、“Uri”、“urn”、“Urn”、“url”或“Url”的字符串参数,且类型未声明采用 System.Uri 参数的相应重载。

默认情况下,此规则仅查看外部可见的类型,但这是可配置的。

规则说明

此规则根据 Camel 大小写约定将参数名称拆分为标记,并检查每个标记是否等于“uri”、“Uri”、“urn”、“Urn”、“url”或“Url”。 如果存在匹配项,此规则假定该参数表示统一资源标识符 (URI)。 URI 的字符串表示形式容易导致分析和编码错误,并且可造成安全漏洞。 如果某方法采用 URI 的字符串表示形式,则应提供采用 Uri 类的实例的相应重载,该类以安全的方式提供这些服务。

如何解决冲突

若要解决此规则的冲突,请将参数更改为 Uri 类型;这是一项中断性变更。 或者,提供采用 Uri 参数的方法的重载;这是一项非中断性变更。

何时禁止显示警告

如果该参数不表示 URL,则可以安全地禁止显示此规则的警告。

抑制警告

如果只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用该规则。

#pragma warning disable CA1054
// The code that's violating the rule is on this line.
#pragma warning restore CA1054

若要对文件、文件夹或项目禁用该规则,请在配置文件中将其严重性设置为 none

[*.{cs,vb}]
dotnet_diagnostic.CA1054.severity = none

有关详细信息,请参阅如何禁止显示代码分析警告

配置代码以进行分析

使用下面的选项来配置代码库的哪些部分要运行此规则。

可以仅为此规则、为适用的所有规则或为适用的此类别(设计)中的所有规则配置这些选项。 有关详细信息,请参阅代码质量规则配置选项

包含特定的 API 图面

你可以根据代码库的可访问性,配置要针对其运行此规则的部分。 例如,若要指定规则应仅针对非公共 API 图面运行,请将以下键值对添加到项目中的 .editorconfig 文件:

dotnet_code_quality.CAXXXX.api_surface = private, internal

排除特定符号

可以从分析中排除特定符号,如类型和方法。 例如,若要指定规则不应针对名为 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 匹配名为 MyType1MyType2 的所有符号。
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) 匹配带有各自的完全限定签名的特定方法 MyMethod1MyMethod2

排除特定类型及其派生类型

可以从分析中排除特定类型及其派生类型。 例如,若要指定规则不应针对名为 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 匹配名为 MyType1MyType2 的所有类型及其所有派生类型。
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 匹配带有各自的完全限定名称的特定类型 MyType1MyType2 及其所有派生类型。

示例

下面的示例显示了一个与此规则冲突的 ErrorProne 类型,以及一个符合此规则的 SaferWay 类型。

public class ErrorProne
{
    // Violates rule UriPropertiesShouldNotBeStrings.
    public string? SomeUri { get; set; }

    // Violates rule UriParametersShouldNotBeStrings.
    public void AddToHistory(string uriString) { }

    // Violates rule UriReturnValuesShouldNotBeStrings.
    public string GetRefererUri(string httpHeader)
    {
        return "http://www.adventure-works.com";
    }
}

public class SaferWay
{
    // To retrieve a string, call SomeUri.ToString().
    // To set using a string, call SomeUri = new Uri(string).
    public Uri? SomeUri { get; set; }

    public void AddToHistory(string uriString)
    {
        // Check for UriFormatException.
        AddToHistory(new Uri(uriString));
    }

    public void AddToHistory(Uri uriType) { }

    public Uri GetRefererUri(string httpHeader)
    {
        return new Uri("http://www.adventure-works.com");
    }
}
Imports System

Namespace ca1054

    Public Class ErrorProne

        ' Violates rule UriPropertiesShouldNotBeStrings.
        Property SomeUri As String

        ' Violates rule UriParametersShouldNotBeStrings.
        Sub AddToHistory(uriString As String)
        End Sub

        ' Violates rule UriReturnValuesShouldNotBeStrings.
        Function GetRefererUri(httpHeader As String) As String
            Return "http://www.adventure-works.com"
        End Function

    End Class

    Public Class SaferWay

        ' To retrieve a string, call SomeUri.ToString().
        ' To set using a string, call SomeUri = New Uri(string).
        Property SomeUri As Uri

        Sub AddToHistory(uriString As String)
            ' Check for UriFormatException.
            AddToHistory(New Uri(uriString))
        End Sub

        Sub AddToHistory(uriString As Uri)
        End Sub

        Function GetRefererUri(httpHeader As String) As Uri
            Return New Uri("http://www.adventure-works.com")
        End Function

    End Class

End Namespace