Compartilhar via


CA1055: os valores de retorno de URI não devem ser cadeias de caracteres

TypeName

UriReturnValuesShouldNotBeStrings

CheckId

CA1055

Categoria

Microsoft.Design

Alteração Significativa

Quebra

Causa

O nome de um método contém “uri”, “Uri”, “urn”, “urn”, “URL”, ou “URL”, e o método retorna uma cadeia de caracteres.

Descrição da Regra

Essa regra se divide o nome do método em tokens com base na convenção de maiúsculas e minúsculas de Pascal e verifique se cada token é igual a “uri”, “Uri”, “urn”, “urn”, “URL”, ou “URL”.Se houver uma correspondência, a regra supõe que o método retornará (Uniform Resource Identifier (URI).Uma representação de cadeia de caracteres de um URI for susceptível a erros de análise e de codificação, e pode resultar em vulnerabilidades de segurança.A classe de Uri fornece estes serviços em um cofre e uma maneira segura.

Como Corrigir Violações

Para corrigir uma violação desta regra, altere o tipo de retorno a Uri.

Quando Suprimir Alertas

É seguro suprimir um aviso dessa regra se o valor de retorno não representa um URI.

Exemplo

O exemplo a seguir mostra um tipo, ErrorProne, que viola essa regra, e um tipo, SaferWay, que satisfaça a regra.

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");
      }
   };
}

Regras Relacionadas

CA1056: as propriedades de URI não devem ser cadeias de caracteres

CA1054: os parâmetros de URI não devem ser cadeias de caracteres

CA2234: passar objetos System.Uri em vez de cadeias de caracteres

CA1057: as sobrecargas de URI da cadeia de caracteres chamam sobrecargas System.Uri