Share via


CA2147: 투명 메서드는 보안 어설션을 사용할 수 없습니다.

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

원인

SecurityTransparentAttribute로 표시된 코드에 어설션하는 데 필요한 권한이 부여되지 않았습니다.

참고 항목

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

규칙 설명

이 규칙은 100% 투명 어셈블리나 투명/중요 혼합 어셈블리의 모든 메서드와 형식을 분석하고 선언적이거나 명령적인 Assert 사용에 플래그를 지정합니다.

런타임에 투명 코드에서 Assert를 호출하면 InvalidOperationException이 throw됩니다. 이 예외는 100% 투명 어셈블리와 메서드 또는 형식이 투명으로 선언되었지만 선언적 또는 명령적 Assert를 포함하는 투명/중요 혼합 어셈블리에서 모두 발생할 수 있습니다.

.NET Framework 2.0에서는 ‘투명도’라는 기능이 도입되었습니다. 개별 메서드, 필드, 인터페이스, 클래스, 형식은 투명하거나 중요할 수 있습니다.

투명 코드는 보안 권한을 상승시킬 수 없습니다. 따라서 투명 코드에 대해 부여되었거나 요청된 모든 권한은 코드를 통해 호출자나 호스트 애플리케이션 도메인에 자동으로 전달됩니다. 권한 상승의 예로는 Assert, LinkDemand, SuppressUnmanagedCode, unsafe 코드가 있습니다.

위반 문제를 해결하는 방법

이 문제를 해결하려면 Assert를 호출하는 코드를 SecurityCriticalAttribute로 표시하거나 Assert를 제거합니다.

경고를 표시하지 않는 경우

이 규칙에서는 메시지를 표시해야 합니다.

예 1

SecurityTestClass가 투명한 경우 Assert 메서드가 InvalidOperationException을 throw하면 이 코드가 실패합니다.

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

namespace TransparencyWarningsDemo
{

    public class TransparentMethodsUseSecurityAssertsClass
    {
        // CA2147 violation - transparent code using a security assert declaratively.  This can be fixed by
        // any of:
        //   1. Make DeclarativeAssert critical
        //   2. Make DeclarativeAssert safe critical
        //   3. Remove the assert attribute
        [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
        public void DeclarativeAssert()
        {
        }

        public void ImperativeAssert()
        {
            // CA2147 violation - transparent code using a security assert imperatively.  This can be fixed by
            // any of:
            //   1. Make ImperativeAssert critical
            //   2. Make ImperativeAssert safe critical
            //   3. Remove the assert call
            new PermissionSet(PermissionState.Unrestricted).Assert();
        }
    }
}

예제 2

한 가지 옵션으로, 아래 예제에서 SecurityTransparentMethod 메서드의 코드를 검토하고 메서드가 권한 상승에 안전한 것으로 간주되면 SecurityTransparentMethod를 보안에 중요한 것으로 표시할 수 있습니다. 이 경우 Assert 아래의 메서드 내에서 발생하는 콜아웃과 함께 메서드에서 자세하고 완전하며 오류가 없는 보안 감사를 수행해야 합니다.

using System;
using System.Security.Permissions;

namespace SecurityTestClassLibrary
{
    public class SecurityTestClass
    {
        [System.Security.SecurityCritical]
        void SecurityCriticalMethod()
        {
            new FileIOPermission(PermissionState.Unrestricted).Assert();

            // perform I/O operations under Assert
        }
    }
}

또 다른 옵션은 코드에서 Assert를 제거하고 후속 파일 I/O 권한 요청이 SecurityTransparentMethod를 통과하여 호출자에게 전달되도록 하는 것입니다. 이렇게 하면 보안 검사를 사용할 수 있게 됩니다. 이 경우 권한 요청이 호출자 및/또는 애플리케이션 도메인에 전달되기 때문에 보안 감사는 필요 없습니다. 보안 정책, 호스팅 환경, 코드 소스 권한 부여를 통해 권한 요청이 자세히 제어됩니다.

참고 항목

보안 경고