Share via


CA5374: XslTransform을 사용하지 마세요

속성
규칙 ID CA5374
타이틀 XslTransform 사용 안 함
범주 보안
수정 사항이 주요 변경인지 여부 주요 변경 아님
.NET 8에서 기본적으로 사용 아니요

원인

잠재적으로 위험한 외부 참조를 제한하지 않거나 스크립트를 방지할 수 없는 System.Xml.Xsl.XslTransform을 인스턴스화하는 경우.

규칙 설명

XslTransform은 신뢰할 수 없는 입력에서 작동하는 경우에 취약합니다. 공격은 임의의 코드를 실행할 수 있습니다.

위반 문제를 해결하는 방법

XslTransformSystem.Xml.Xsl.XslCompiledTransform로 교체합니다. 자세한 내용은 [/dotnet/standard/data/xml/migrating-from-the-xsltransform-class]를 참조하세요.

경고를 표시하지 않는 경우

XslTransform 개체, XSLT 스타일 시트, XML 소스 데이터는 모두 신뢰할 수 있는 출처에서 가져온 것입니다.

경고 표시 안 함

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

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

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

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

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

의사 코드 예제

위반

다음 의사 코드 샘플에서는 이 규칙에 의해 탐지되는 패턴을 보여 줍니다.

using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace TestForXslTransform
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new XslTransform object.
            XslTransform xslt = new XslTransform();

            // Load the stylesheet.
            xslt.Load("https://server/favorite.xsl");

            // Create a new XPathDocument and load the XML data to be transformed.
            XPathDocument mydata = new XPathDocument("inputdata.xml");

            // Create an XmlTextWriter which outputs to the console.
            XmlWriter writer = new XmlTextWriter(Console.Out);

            // Transform the data and send the output to the console.
            xslt.Transform(mydata, null, writer, null);
        }
    }
}

해결 방법

using System.Xml;
using System.Xml.Xsl;

namespace TestForXslTransform
{
    class Program
    {
        static void Main(string[] args)
        {
            // Default XsltSettings constructor disables the XSLT document() function
            // and embedded script blocks.
            XsltSettings settings = new XsltSettings();

            // Execute the transform.
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load("https://server/favorite.xsl", settings, new XmlUrlResolver());
            xslt.Transform("inputdata.xml", "outputdata.html");
        }
    }
}