Udostępnij za pośrednictwem


CA5374: Nie używaj XslTransform

Właściwości Wartość
Identyfikator reguły CA5374
Stanowisko Nie używaj XslTransform
Kategoria Bezpieczeństwo
Poprawka powodująca niezgodność lub niezgodność Niezgodność
Domyślnie włączone na platformie .NET 8 Nie.

Przyczyna

Utworzenie wystąpienia elementu System.Xml.Xsl.XslTransform, które nie ogranicza potencjalnie niebezpiecznych odwołań zewnętrznych lub uniemożliwia wykonywanie skryptów.

Opis reguły

XslTransform jest podatna na zagrożenia w przypadku działania na niezaufanych danych wejściowych. Atak może wykonać dowolny kod.

Jak naprawić naruszenia

Zamień XslTransform na System.Xml.Xsl.XslCompiledTransform. Aby uzyskać więcej wskazówek, zobacz [/dotnet/standard/data/xml/migrating-from-the-xsltransform-class].

Kiedy pomijać ostrzeżenia

Wszystkie XslTransform obiekty, arkusze stylów XSLT i dane źródła XML pochodzą z zaufanych źródeł.

Pomijanie ostrzeżenia

Jeśli chcesz po prostu pominąć pojedyncze naruszenie, dodaj dyrektywy preprocesora do pliku źródłowego, aby wyłączyć, a następnie ponownie włączyć regułę.

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

Aby wyłączyć regułę dla pliku, folderu lub projektu, ustaw jego ważność na none w pliku konfiguracji.

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

Aby uzyskać więcej informacji, zobacz Jak pominąć ostrzeżenia dotyczące analizy kodu.

Przykłady przykładów kodu przykładowego

Naruszenie

Obecnie poniższy przykład pseudo-kodu ilustruje wzorzec wykryty przez tę regułę.

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);
        }
    }
}

Rozwiązanie

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");
        }
    }
}