다음을 통해 공유


CA5386: SecurityProtocolType 값 하드코딩 방지

속성
규칙 ID CA5386
제목 SecurityProtocolType 값을 하드 코딩하지 마세요.
범주 보안
수정 사항이 호환성을 깨뜨리는지 여부 또는 무중단인지 여부 주요 변경 아님
.NET 10에서 기본적으로 사용하도록 설정 아니요
적용 가능한 언어 C# 및 Visual Basic

원인

이 규칙은 다음 조건 중 하나가 충족될 때 발동합니다.

안전한 값:

  • Tls12
  • Tls13

규칙 설명

TLS(전송 계층 보안)는 가장 일반적으로 HTTPS(Hypertext Transfer Protocol Secure)를 사용하여 컴퓨터 간의 통신을 보호합니다. 프로토콜 버전 TLS 1.0 및 TLS 1.1은 사용되지 않으며 TLS 1.2 및 TLS 1.3은 최신 상태입니다. 나중에는 TLS 1.2 및 TLS 1.3도 사용되지 않을 수 있습니다. 애플리케이션을 안전하게 유지하려면 프로토콜 버전을 하드 코드하지 말고 .NET Framework v4.7.1 이상을 대상으로 지정합니다. 자세한 내용은 .NET Framework를 사용한 TLS(전송 계층 보안) 모범 사례를 참조하세요.

위반 문제를 해결하는 방법

TLS 프로토콜 버전을 하드 코드하지 마세요.

경고를 표시하지 않는 경우

애플리케이션이 .NET Framework v4.6.2 이전 버전을 대상으로 하고 안전하지 않은 기본값이 있는 컴퓨터에서 실행될 수 있는 경우 이 경고를 표시하지 않아도 됩니다.

경고 표시 안 함

단일 위반을 억제하려면 원본 파일에 전처리기 지시문을 추가하여 규칙을 비활성화한 후 다시 활성화하십시오.

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

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

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

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

의사 코드 예제

열거형 이름 위반

using System;
using System.Net;

public class ExampleClass
{
    public void ExampleMethod()
    {
        // CA5386 violation
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    }
}
Imports System
Imports System.Net

Public Class TestClass
    Public Sub ExampleMethod()
        ' CA5386 violation
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    End Sub
End Class

정수 값 위반

using System;
using System.Net;

public class ExampleClass
{
    public void ExampleMethod()
    {
        // CA5386 violation
        ServicePointManager.SecurityProtocol = (SecurityProtocolType) 3072;    // TLS 1.2
    }
}
Imports System
Imports System.Net

Public Class TestClass
    Public Sub ExampleMethod()
        ' CA5386 violation
        ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)   ' TLS 1.2
    End Sub
End Class

솔루션

using System;
using System.Net;

public class TestClass
{
    public void TestMethod()
    {
        // Let the operating system decide what TLS protocol version to use.
        // See https://learn.microsoft.com/dotnet/framework/network-programming/tls
    }
}
Imports System
Imports System.Net

Public Class TestClass
    Public Sub ExampleMethod()
        ' Let the operating system decide what TLS protocol version to use.
        ' See https://learn.microsoft.com/dotnet/framework/network-programming/tls
    End Sub
End Class

CA5364: 사용되지 않는 보안 프로토콜을 사용하지 마세요.

CA5397: 사용되지 않는 SslProtocols 값을 사용하지 마세요.

CA5398: 하드 코딩된 SslProtocols 값 방지