|
| 1 | +--- |
| 2 | +title: "CA5374: Do Not Use XslTransform" |
| 3 | +description: Provides information about code analysis rule CA5374, including causes, how to fix violations, and when to suppress it. |
| 4 | +ms.date: 04/28/2020 |
| 5 | +ms.topic: reference |
| 6 | +author: LLLXXXCCC |
| 7 | +ms.author: linche |
| 8 | +manager: jillfra |
| 9 | +dev_langs: |
| 10 | + - CSharp |
| 11 | + - VB |
| 12 | +ms.workload: |
| 13 | + - "multiple" |
| 14 | +f1_keywords: |
| 15 | + - "CA5374" |
| 16 | + - "DoNotUseXslTransform" |
| 17 | +--- |
| 18 | +# CA5374: Do Not Use XslTransform |
| 19 | + |
| 20 | +||| |
| 21 | +|-|-| |
| 22 | +|CheckId|CA5374| |
| 23 | +|Category|Microsoft.Security| |
| 24 | +|Breaking change|Non-breaking| |
| 25 | + |
| 26 | +## Cause |
| 27 | + |
| 28 | +Instantiating an <xref:System.Xml.Xsl.XslTransform?displayProperty=nameWithType>, which doesn't restrict potentially dangerous external references. |
| 29 | + |
| 30 | +## Rule description |
| 31 | + |
| 32 | +<xref:System.Xml.Xsl.XslTransform> is vulnerable when operating on untrusted input. An attack could execute arbitrary code. |
| 33 | + |
| 34 | +## How to fix violations |
| 35 | + |
| 36 | +Replace <xref:System.Xml.Xsl.XslTransform> with <xref:System.Xml.Xsl.XslCompiledTransform?displayProperty=nameWithType>. For more guidance, see [/dotnet/standard/data/xml/migrating-from-the-xsltransform-class]. |
| 37 | + |
| 38 | +## When to suppress warnings |
| 39 | + |
| 40 | +The <xref:System.Xml.Xsl.XslTransform> object, XSLT style sheets, and XML source data are all from trusted sources. |
| 41 | + |
| 42 | +## Pseudo-code examples |
| 43 | + |
| 44 | +### Violation |
| 45 | + |
| 46 | +At present, the following pseudo-code sample illustrates the pattern detected by this rule. |
| 47 | + |
| 48 | +```csharp |
| 49 | +using System; |
| 50 | +using System.Xml; |
| 51 | +using System.Xml.XPath; |
| 52 | +using System.Xml.Xsl; |
| 53 | + |
| 54 | +namespace TestForXslTransform |
| 55 | +{ |
| 56 | + class Program |
| 57 | + { |
| 58 | + static void Main(string[] args) |
| 59 | + { |
| 60 | + // Create a new XslTransform object. |
| 61 | + XslTransform xslt = new XslTransform(); |
| 62 | + |
| 63 | + // Load the stylesheet. |
| 64 | + xslt.Load("http://server/favorite.xsl"); |
| 65 | + |
| 66 | + // Create a new XPathDocument and load the XML data to be transformed. |
| 67 | + XPathDocument mydata = new XPathDocument("inputdata.xml"); |
| 68 | + |
| 69 | + // Create an XmlTextWriter which outputs to the console. |
| 70 | + XmlWriter writer = new XmlTextWriter(Console.Out); |
| 71 | + |
| 72 | + // Transform the data and send the output to the console. |
| 73 | + xslt.Transform(mydata, null, writer, null); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Solution |
| 80 | + |
| 81 | +```csharp |
| 82 | +using System.Xml; |
| 83 | +using System.Xml.Xsl; |
| 84 | + |
| 85 | +namespace TestForXslTransform |
| 86 | +{ |
| 87 | + class Program |
| 88 | + { |
| 89 | + static void Main(string[] args) |
| 90 | + { |
| 91 | + // Create the XsltSettings object with script enabled. |
| 92 | + XsltSettings settings = new XsltSettings(false, true); |
| 93 | + |
| 94 | + // Execute the transform. |
| 95 | + XslCompiledTransform xslt = new XslCompiledTransform(); |
| 96 | + xslt.Load("http://server/favorite.xsl", settings, new XmlUrlResolver()); |
| 97 | + xslt.Transform("inputdata.xml", "outputdata.html"); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +``` |
0 commit comments