다음을 통해 공유


CA2112: 보안 형식은 필드를 노출하면 안 됩니다.

TypeName

SecuredTypesShouldNotExposeFields

CheckId

CA2112

범주

Microsoft.Security

변경 수준

주요 변경

원인

public 또는 protected 형식이 public 필드를 포함하고 링크 요청에 의해 보안됩니다.

규칙 설명

코드에 링크 요청으로 보안된 형식의 인스턴스에 대한 액세스 권한이 있으면 코드에서 링크 요청을 만족하지 않아도 해당 형식의 필드에 액세스할 수 있습니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 필드를 public이 아닌 필드로 만들고 필드 데이터를 반환하는 public 속성 또는 메서드를 추가합니다. 형식에 대한 LinkDemand 보안 검사는 형식의 속성 및 메서드에 대한 액세스를 보호합니다. 그러나 코드 액세스 보안이 필드에는 적용되지 않습니다.

경고를 표시하지 않는 경우

보안 문제를 해결하고 디자인을 향상시키려면 public 필드를 public이 아닌 필드로 만들어 위반 문제를 해결해야 합니다. 필드에 보안을 유지해야 하는 정보가 들어 있지 않고 필드의 내용에 의존하지 않는 경우에는 이 규칙에서 경고를 표시하지 않을 수 있습니다.

예제

다음 예제에서는 보안되지 않는 필드가 있는 라이브러리 형식(SecuredTypeWithFields)을 보여 주고, 이 라이브러리 형식의 인스턴스를 만들 수 있는 형식(Distributor)과 이를 만들 권한이 없는 형식에 인스턴스를 잘못 전달하는 동작을 보여 주며, 형식에 대한 보안 권한이 없는 경우에도 인스턴스의 필드를 읽을 수 있는 응용 프로그램 코드를 보여 줍니다.

다음 라이브러리 코드는 규칙을 위반합니다.

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

보안 형식을 보호하는 링크 요청 때문에 응용 프로그램에서 인스턴스를 만들 수 없습니다. 응용 프로그램에서는 다음 클래스를 사용하여 보안 형식의 인스턴스를 가져올 수 있습니다.

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

다음 응용 프로그램에서는 보안 형식의 메서드에 대한 액세스 권한 없이 코드에서 해당 필드에 액세스할 수 있는 방법을 보여 줍니다.

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

이 예제의 결과는 다음과 같습니다.

        

관련 규칙

CA1051: 표시되는 인스턴스 필드를 선언하지 마십시오.

참고 항목

개념

링크 요청

기타 리소스

데이터 액세스 및 모델링