Share via


CA5359: 인증서 유효성 검사를 사용하지 않도록 설정하지 마세요.

속성
규칙 ID CA5359
타이틀 인증서 유효성 검사를 비활성화하지 마세요.
범주 보안
수정 사항이 주요 변경인지 여부 주요 변경 아님
.NET 8에서 기본적으로 사용 아니요

원인

ServicePointManager.ServerCertificateValidationCallback에 할당된 콜백은 항상 true를 반환합니다.

규칙 설명

인증서는 서버의 ID를 인증하는 데 도움이 될 수 있습니다. 클라이언트는 서버 인증서의 유효성을 검사하여 요청이 의도한 서버로 전송되도록 해야 합니다. ServicePointManager.ServerCertificateValidationCallback이 항상 true를 반환하는 경우 기본적으로 모든 인증서는 모든 보내는 HTTPS 요청에 대한 유효성 검사를 통과합니다.

위반 문제를 해결하는 방법

  • 전역 ServicePointManager.ServerCertificateValidationCallback을 재정의하는 대신 사용자 지정 인증서 유효성 검사가 필요한 특정 보내는 HTTPS 요청에서 인증서 유효성 검사 논리를 재정의하는 것을 고려합니다.
  • 특정 호스트 이름 및 인증서에만 사용자 지정 유효성 검사 논리를 적용하고 그러지 않은 경우 SslPolicyErrors 열거형 값이 None인지 확인합니다.

경고를 표시하지 않는 경우

여러 대리자가 ServerCertificateValidationCallback에 연결된 경우에는 마지막 대리자의 값만 적용되므로 다른 대리자의 경고는 표시하지 않아도 됩니다. 그러나 사용하지 않는 대리자를 완전히 제거하는 것이 좋습니다.

경고 표시 안 함

단일 위반만 표시하지 않으려면 원본 파일에 전처리기 지시문을 추가하여 규칙을 사용하지 않도록 설정한 후 다시 사용하도록 설정합니다.

#pragma warning disable CA5359
// The code that's violating the rule is on this line.
#pragma warning restore CA5359

파일, 폴더 또는 프로젝트에 대한 규칙을 사용하지 않도록 설정하려면 구성 파일에서 심각도를 none으로 설정합니다.

[*.{cs,vb}]
dotnet_diagnostic.CA5359.severity = none

자세한 내용은 방법: 코드 분석 경고 표시 안 함을 참조하세요.

의사 코드 예제

위반

using System.Net;

class ExampleClass
{
    public void ExampleMethod()
    {
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, error) => { return true; };
    }
}

해결 방법

using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

class ExampleClass
{
    public void ExampleMethod()
    {
        ServicePointManager.ServerCertificateValidationCallback += SelfSignedForLocalhost;
    }

    private static bool SelfSignedForLocalhost(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        if (sslPolicyErrors == SslPolicyErrors.None)
        {
            return true;
        }

        // For HTTPS requests to this specific host, we expect this specific certificate.
        // In practice, you'd want this to be configurable and allow for multiple certificates per host, to enable
        // seamless certificate rotations.
        return sender is HttpWebRequest httpWebRequest
                && httpWebRequest.RequestUri.Host == "localhost"
                && certificate is X509Certificate2 x509Certificate2
                && x509Certificate2.Thumbprint == "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
                && sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors;
    }
}