CA1055: URI dönüş değerleri dizeler olmamalıdır
TürAdı |
UriReturnValuesShouldNotBeStrings |
CheckId |
CA1055 |
Kategori |
Microsoft.Design |
Bozan Değişiklik |
Bozan |
Sebep
Bir yöntemin adı "uri", "Uri", "urn", "Urn", "url" veya "Url" içerir ve yöntem bir dize döndürür.
Kural Tanımı
Bu kural yöntem adını Pascal kasası kuralına dayanan belirteçlere böler ve her belirtecin "uri", "Uri", "urn", "Urn", "url" veya "Url" a eşit olup olmadığını denetler.Bir eşleşme varsa kural, yöntem bir tektip kaynak tanımlayıcısı (URI) döndürdüğünü varsayar.Bir URI'nin dize halinde temsili, ayrıştırma ve hataları kodlama eğilimindedir ve güvenlik açıklarına yol açabilir.Bu Uri sınıfı bu hizmetleri güvenli bir şekilde sağlar.
İhlallerin Düzeltilmesi
Bu kuralın bir ihlalini düzeltmek için dönüş türünü bir Uri 'a değiştirin.
Uyarılar Ne Zaman Bastırılmalı
Dönüş değeri bir URI'yi temsil etmiyorsa, bu kuraldan gelen bir uyarıyı bastırmak güvenlidir.
Örnek
Aşağıdaki örnek bu kuralı ihlal eden bir türü, ErrorProne, ve bu kuralı karşılayan bir türü, SaferWay, gösterir.
Imports System
Namespace DesignLibrary
Public Class ErrorProne
Dim someUriValue As String
' Violates rule UriPropertiesShouldNotBeStrings.
Property SomeUri As String
Get
Return someUriValue
End Get
Set
someUriValue = Value
End Set
End Property
' Violates rule UriParametersShouldNotBeStrings.
Sub AddToHistory(uriString As String)
End Sub
' Violates rule UriReturnValuesShouldNotBeStrings.
Function GetRefererUri(httpHeader As String) As String
Return "https://www.adventure-works.com"
End Function
End Class
Public Class SaferWay
Dim someUriValue As Uri
' To retrieve a string, call SomeUri.ToString().
' To set using a string, call SomeUri = New Uri(string).
Property SomeUri As Uri
Get
Return someUriValue
End Get
Set
someUriValue = Value
End Set
End Property
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("https://www.adventure-works.com")
End Function
End Class
End Namespace
using System;
namespace DesignLibrary
{
public class ErrorProne
{
string someUri;
// Violates rule UriPropertiesShouldNotBeStrings.
public string SomeUri
{
get { return someUri; }
set { someUri = value; }
}
// Violates rule UriParametersShouldNotBeStrings.
public void AddToHistory(string uriString) { }
// Violates rule UriReturnValuesShouldNotBeStrings.
public string GetRefererUri(string httpHeader)
{
return "https://www.adventure-works.com";
}
}
public class SaferWay
{
Uri someUri;
// To retrieve a string, call SomeUri.ToString().
// To set using a string, call SomeUri = new Uri(string).
public Uri SomeUri
{
get { return someUri; }
set { someUri = value; }
}
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("https://www.adventure-works.com");
}
}
}
#using <system.dll>
using namespace System;
namespace DesignLibrary
{
public ref class ErrorProne
{
public:
// Violates rule UriPropertiesShouldNotBeStrings.
property String^ SomeUri;
// Violates rule UriParametersShouldNotBeStrings.
void AddToHistory(String^ uriString) { }
// Violates rule UriReturnValuesShouldNotBeStrings.
String^ GetRefererUri(String^ httpHeader)
{
return "https://www.adventure-works.com";
}
};
public ref class SaferWay
{
public:
// To retrieve a string, call SomeUri()->ToString().
// To set using a string, call SomeUri(gcnew Uri(string)).
property Uri^ SomeUri;
void AddToHistory(String^ uriString)
{
// Check for UriFormatException.
AddToHistory(gcnew Uri(uriString));
}
void AddToHistory(Uri^ uriType) { }
Uri^ GetRefererUri(String^ httpHeader)
{
return gcnew Uri("https://www.adventure-works.com");
}
};
}
İlgili Kurallar
CA1056: URI özellikleri dizeler olmamalıdır
CA1054: URI parametreleri dizeler olmamalıdır
CA2234: Dizeler yerine System.Uri nesneleri gönderin
CA1057: Dize URI aşırı yüklemeleri System.Uri aşırı yüklemelerini çağırır