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

항목
RuleId CA2112
범주 Microsoft.Security
주요 변경 내용 주요 변경

원인

public 또는 protected 형식이 public 필드를 포함하고 링크 요청으로 보호되었습니다.

참고 항목

이 규칙은 더 이상 사용되지 않습니다. 자세한 내용은 사용되지 않는 규칙을 참조하세요.

규칙 설명

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

위반 문제를 해결하는 방법

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

경고를 표시하지 않는 경우

보안 문제와 좋은 디자인을 위해 public 필드를 nonpublic으로 설정하여 위반 문제를 해결해야 합니다. 필드에 보호해야 하는 정보가 없고 필드 내용을 사용하지 않는 경우 이 규칙의 경고를 표시하지 않을 수 있습니다.

예시

다음 예제는 보호되지 않은 필드가 있는 라이브러리 형식(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);
      }
   }
}

예 1

보안 형식을 보호하는 링크 요청 때문에 애플리케이션에서 인스턴스를 만들 수 없습니다. 다음 클래스를 사용하면 애플리케이션이 보안 형식의 인스턴스를 얻을 수 있습니다.

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

예제 2

다음 애플리케이션은 보안 형식의 메서드에 액세스할 수 있는 권한 없이 코드에서 해당 필드에 액세스할 수 있는 방법을 보여 줍니다.

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

이 예제는 다음과 같은 출력을 생성합니다.

Creating an instance of SecuredTypeWithFields.
Secured type fields: 22, 33
Changing secured type's field...
Cached Object fields: 99, 33

참고 항목