다음을 통해 공유


링크 요청이 있는 메서드를 간접적으로 노출하지 마십시오.

업데이트: 2007년 11월

TypeName

DoNotIndirectlyExposeMethodsWithLinkDemands

CheckId

CA2122

범주

Microsoft.Security

변경 수준

주요 변경 아님

원인

public 또는 protected 멤버에 링크 요청이 있으며 보안 검사를 수행하지 않는 멤버에 의해 호출됩니다.

규칙 설명

링크 요청은 직접 실행 호출자의 권한만 검사합니다. 멤버 X에서 호출자에 대해 보안 요청을 적용하지 않고 링크 요청으로 보호되는 코드를 호출하는 경우 필요한 권한이 없는 호출자가 X를 사용하여 보호되는 멤버에 액세스할 수 있습니다.

위반 문제를 해결하는 방법

보안 데이터 액세스 또는 링크 요청을 멤버에 추가하여 링크 요청으로 보호되는 멤버에 보안되지 않은 액세스가 제공되지 않도록 합니다.

경고를 표시하지 않는 경우

이 규칙에 따른 경고를 표시하지 않으려면 악용될 수 있는 작업이나 리소스에 대한 액세스 권한을 코드에서 호출자에게 부여하지 않는지 확인해야 합니다.

예제

다음 예제에는 이 규칙을 위반하는 라이브러리와 해당 라이브러리의 취약성을 보여 주는 응용 프로그램이 나와 있습니다. 샘플 라이브러리에서는 함께 규칙을 위반하는 두 개의 메서드를 제공합니다. 여기서 링크 요청은 환경 변수에 대한 무제한 액세스로부터 EnvironmentSetting 메서드를 보호합니다. DomainInformation 메서드는 EnvironmentSetting을 호출하기 전에는 호출자에 대해 보안 요청을 적용하지 않습니다.

using System;
using System.IO;
using System.Security;
using System.Security.Permissions;

namespace SecurityRulesLibrary
{
   public class DoNotIndirectlyExposeMethodsWithLinkDemands
   {
      // Violates rule: DoNotIndirectlyExposeMethodsWithLinkDemands.
      public static string DomainInformation()
      {
         return EnvironmentSetting("USERDNSDOMAIN");
      }

      // Library method with link demand.
      // This method holds its immediate callers responsible for securing the information.
      // Because a caller must have unrestricted permission, the method asserts read permission
      // in case some caller in the stack does not have this permission. 

      [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
      public static string EnvironmentSetting(string environmentVariable)
      {
         EnvironmentPermission envPermission = new EnvironmentPermission( EnvironmentPermissionAccess.Read,environmentVariable);
         envPermission.Assert();

         return Environment.GetEnvironmentVariable(environmentVariable);
      }
   }
}

다음 응용 프로그램은 보안되지 않은 라이브러리 멤버를 호출합니다.

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

// You have no permission to access the sensitive information,
// but you will get data from the unprotected method.
[assembly:EnvironmentPermissionAttribute(
   SecurityAction.RequestRefuse,Unrestricted=true)]
namespace TestUnsecuredMembers
{
   class TestUnsecured
   {
      [STAThread]
      static void Main(string[] args)
      {
         string value = null;
         try 
         {
            value = DoNotIndirectlyExposeMethodsWithLinkDemands.DomainInformation();
         }
         catch (SecurityException e) 
         {
            Console.WriteLine(
               "Call to unsecured member was stopped by code access security! {0}",
               e.Message);
            throw;
         }
         if (value != null) 
         {
            Console.WriteLine("Value from unsecured member: {0}", value);
         }
      }
   }
}

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

Value from unsecured member: seattle.corp.contoso.com

참고 항목

개념

링크 요청

기타 리소스

보안 코딩 지침

데이터 액세스