Share via


CA5393: 안전하지 않은 DllImportSearchPath 값을 사용하지 마세요.

속성
규칙 ID CA5393
타이틀 안전하지 않은 DllImportSearchPath 값을 사용하지 마세요.
범주 보안
수정 사항이 주요 변경인지 여부 주요 변경 아님
.NET 8에서 기본적으로 사용 아니요

원인

xref:System.Runtime.InteropServices.DllImportSearchPath?displayProperty=fullName의 <안전하지 않은 값 중 하나를 사용합니다.

  • AssemblyDirectory
  • UseDllDirectoryForDependencies
  • ApplicationDirectory
  • LegacyBehavior

규칙 설명

기본 DLL 검색 디렉터리 및 어셈블리 디렉터리에 악성 DLL이 있을 수 있습니다. 또는 애플리케이션이 실행되는 위치에 따라 애플리케이션의 디렉터리에 악성 DLL이 있을 수 있습니다.

자세한 내용은 Load Library Safely(라이브러리를 안전하게 로드)를 참조하세요.

위반 문제를 해결하는 방법

안전 값을 DllImportSearchPath 사용하여 명시적 검색 경로를 대신 지정합니다.

  • SafeDirectories
  • System32
  • UserDirectories

경고를 표시하지 않는 경우

다음 경우에는 이 규칙을 표시하지 않아도 됩니다.

  • 원하는 어셈블리가 로드되었습니다.
  • 가져온 어셈블리가 일반적으로 사용되는 시스템 어셈블리(예: user32.dll)이며 검색 경로 전략이 Known DLLs mechanism(알려진 DLL 메커니즘)을 따릅니다.

경고 표시 안 함

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

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

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

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

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

분석할 코드 구성

다음 옵션을 사용하여 이 규칙이 실행될 코드베이스 부분을 구성합니다.

이 규칙, 적용되는 모든 규칙 또는 적용되는 이 범주(보안)의 모든 규칙에 대해 이 옵션을 구성할 수 있습니다. 자세한 내용은 코드 품질 규칙 구성 옵션을 참조하세요.

안전하지 않은 DllImportSearchPath 비트

분석에 안전하지 않은 DllImportSearchPath의 값을 구성할 수 있습니다. 예를 들어 코드가 AssemblyDirectory, UseDllDirectoryForDependencies 또는 ApplicationDirectory를 사용하지 않도록 지정하려면 프로젝트의 .editorconfig 파일에 다음 키-값 쌍을 추가합니다.

dotnet_code_quality.CA5393.unsafe_DllImportSearchPath_bits = 770

열거형 값의 비트 조합에 정수 값을 지정해야 합니다.

의사 코드 예제

using System;
using System.Runtime.InteropServices;

class ExampleClass
{
    [DllImport("The3rdAssembly.dll")]
    [DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

    public void ExampleMethod()
    {
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}

솔루션

using System;
using System.Runtime.InteropServices;

class ExampleClass
{
    [DllImport("The3rdAssembly.dll")]
    [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

    public void ExampleMethod()
    {
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}

CA5392: P/Invokes에 DefaultDllImportSearchPaths 특성 사용