Udostępnij za pośrednictwem


CA2112: Zabezpieczonych typów nie powinny wystawiać pól

TypeName

SecuredTypesShouldNotExposeFields

CheckId

CA2112

Kategoria

Microsoft.Security

Złamanie zmiany

Złamanie

Przyczyna

Typ publicznych lub chronionych zawiera pola publicznych i jest zabezpieczony przez LinkDemand.

Opis reguły

Jeśli kod ma dostęp do wystąpienia typu, który jest zabezpieczony przez żądanie łącza, kod nie ma popytu łącze do pola Typ dostępu.

Jak naprawić naruszenia

Aby naprawić naruszenie tej zasady, ustawianie niepublicznych pól i dodać publicznych właściwości lub metod, które zwracają dane pole.Typy kontroli zabezpieczeń LinkDemand ochrony dostępu do właściwości i metod typu.Jednakże ochronę dostępu do kodu nie stosuje się do pól.

Kiedy do pomijania ostrzeżenia

Zarówno dla problemów z zabezpieczeniami i dla dobra projektu powinny ustalić naruszenia poprzez nonpublic pola publiczne.Ostrzeżenie od tej reguły można pominąć, jeśli pole nie przechowuje informacje, które powinny pozostać zabezpieczonych i nie polegać na zawartość tego pola.

Przykład

Poniższy przykład składa się z typu biblioteki (SecuredTypeWithFields) z niezabezpieczoną pola, typ (Distributor), można utworzyć wystąpienia typu biblioteki i wystąpienia błędnego stwierdzenia przechodzi do typów nie ma uprawnień do tworzenia ich i aplikacji kod, który można odczytać pola wystąpienia, nawet, jeśli nie ma uprawnień, które zabezpiecza typu.

Poniższy kod biblioteki narusza reguły.

using System;
using System.Reflection;
using System.Security;
using System.Security.Permissions;

namespace SecurityRulesLibrary
{
   // This code requires immediate callers to have full trust.
   [System.Security.Permissions.PermissionSetAttribute(
       System.Security.Permissions.SecurityAction.LinkDemand, 
       Name="FullTrust")]
   public class SecuredTypeWithFields 
   {
      // Even though the type is secured, these fields are not.
      // Violates rule: SecuredTypesShouldNotExposeFields.
      public double xValue;
      public double yValue;

      public SecuredTypeWithFields (double x, double y) 
      {
         xValue = x;
         yValue = y;
         Console.WriteLine(
            "Creating an instance of SecuredTypeWithFields.");
      }
      public override string ToString()
      {
          return String.Format (
            "SecuredTypeWithFields {0} {1}", xValue, yValue);
      }
   }
}

Aplikacja nie może utworzyć wystąpienia ze względu na żądanie łącze chroni zabezpieczonych typu.Poniższa klasa umożliwia aplikacji do uzyskania wystąpienia typu zabezpieczone.

using System;
using System.Reflection;
using System.Security;
using System.Security.Permissions;

// This assembly executes with full trust. 

namespace SecurityRulesLibrary
{
   // This type creates and returns instances of the secured type.
   // The GetAnInstance method incorrectly gives the instance 
   // to a type that does not have the link demanded permission.

   public class Distributor
   {
      static SecuredTypeWithFields s = new SecuredTypeWithFields(22,33);
      public static SecuredTypeWithFields GetAnInstance ()
      {
            return s;
      }

      public static void DisplayCachedObject ()
      {
         Console.WriteLine(
            "Cached Object fields: {0}, {1}", s.xValue , s.yValue);
      }
   }
}

Następująca aplikacja ilustruje sposób, bez uprawnień dostępu do zabezpieczonych typu metody, kod może korzystać jego pól.

using System;
using System.Security;
using System.Security.Permissions;
using SecurityRulesLibrary;

// This code executes with partial trust.
[assembly: System.Security.Permissions.PermissionSetAttribute(
   System.Security.Permissions.SecurityAction.RequestRefuse,
   Name = "FullTrust")]
namespace TestSecurityExamples
{
    public class TestLinkDemandOnField
    {
        [STAThread]
        public static void Main()
        {
            // Get an instance of the protected object.
            SecuredTypeWithFields secureType = Distributor.GetAnInstance();

            // Even though this type does not have full trust,
            // it can directly access the secured type's fields.
            Console.WriteLine(
               "Secured type fields: {0}, {1}",
               secureType.xValue,
               secureType.yValue);
            Console.WriteLine("Changing secured type's field...");
            secureType.xValue = 99;

            // Distributor must call ToString on the secured object.
            Distributor.DisplayCachedObject();

            // If the following line is uncommented, a security 
            // exception is thrown at JIT-compilation time because 
            // of the link demand for full trust that protects 
            // SecuredTypeWithFields.ToString().

            // Console.WriteLine("Secured type {0}",secureType.ToString());
        }
    }
}

Ten przykład generuje następujące wyniki.

  
  
  

Zasady pokrewne

CA1051: Nie są deklarowane widoczne instancji pola

Zobacz też

Koncepcje

LinkDemand

Inne zasoby

Dane i modelowania w.NET Framework