|
1 | 1 | --- |
2 | 2 | title: "CA3009: Review code for XML injection vulnerabilities (code analysis)" |
3 | 3 | description: "Learn about code analysis rule CA3009: Review code for XML injection vulnerabilities" |
4 | | -ms.date: 07/21/2020 |
| 4 | +ms.date: 07/19/2023 |
5 | 5 | ms.topic: reference |
6 | 6 | author: dotpaul |
7 | 7 | ms.author: paulming |
@@ -38,139 +38,58 @@ This rule attempts to find input from HTTP requests reaching a raw XML write. |
38 | 38 |
|
39 | 39 | ## How to fix violations |
40 | 40 |
|
41 | | -Don't write raw XML. Instead, use methods or properties that XML-encode their input. |
| 41 | +To fix a violation, use one of the following techniques: |
42 | 42 |
|
43 | | -Or, XML-encode input before writing raw XML. |
44 | | - |
45 | | -Or, validate user input by using sanitizers for primitive type conversion and XML encoding. |
| 43 | +- Don't write raw XML. Instead, use methods or properties that XML-encode their input. |
| 44 | +- XML-encode input before writing raw XML. |
| 45 | +- Validate user input by using sanitizers for primitive type conversion and XML encoding. |
46 | 46 |
|
47 | 47 | ## When to suppress warnings |
48 | 48 |
|
49 | 49 | Don't suppress warnings from this rule. |
50 | 50 |
|
51 | | -## Configure code to analyze |
52 | | - |
53 | | -Use the following options to configure which parts of your codebase to run this rule on. |
54 | | - |
55 | | -- [Exclude specific symbols](#exclude-specific-symbols) |
56 | | -- [Exclude specific types and their derived types](#exclude-specific-types-and-their-derived-types) |
| 51 | +## Pseudo-code examples |
57 | 52 |
|
58 | | -You can configure these options for just this rule, for all rules it applies to, or for all rules in this category ([Security](security-warnings.md)) that it applies to. For more information, see [Code quality rule configuration options](../code-quality-rule-options.md). |
| 53 | +### Violation |
59 | 54 |
|
60 | | -[!INCLUDE[excluded-symbol-names](../includes/excluded-symbol-names.md)] |
| 55 | +In this example, the input is set to the <xref:System.Xml.XmlElement.InnerXml> property of the root element. Given input that contains valid XML, a malicious user can then completely alter the document. Notice that `alice` is no longer an allowed user after the user input is added to the document. |
61 | 56 |
|
62 | | -[!INCLUDE[excluded-type-names-with-derived-types](../includes/excluded-type-names-with-derived-types.md)] |
| 57 | +:::code language="csharp" source="snippets/csharp/netfx/ca3009.cs" id="violation" highlight="12"::: |
63 | 58 |
|
64 | | -## Pseudo-code examples |
| 59 | +:::code language="vb" source="snippets/vb/netfx/ca3009.vb" id="violation" highlight="11"::: |
65 | 60 |
|
66 | | -### Violation |
| 61 | +If an attacker uses this for input: `some text<allowedUser>oscar</allowedUser>`, then the XML document will be: |
67 | 62 |
|
68 | | -```csharp |
69 | | -using System; |
70 | | -using System.Xml; |
71 | | - |
72 | | -public partial class WebForm : System.Web.UI.Page |
73 | | -{ |
74 | | - protected void Page_Load(object sender, EventArgs e) |
75 | | - { |
76 | | - string input = Request.Form["in"]; |
77 | | - XmlDocument d = new XmlDocument(); |
78 | | - XmlElement root = d.CreateElement("root"); |
79 | | - d.AppendChild(root); |
80 | | - |
81 | | - XmlElement allowedUser = d.CreateElement("allowedUser"); |
82 | | - root.AppendChild(allowedUser); |
83 | | - |
84 | | - allowedUser.InnerXml = "alice"; |
85 | | - |
86 | | - // If an attacker uses this for input: |
87 | | - // some text<allowedUser>oscar</allowedUser> |
88 | | - // Then the XML document will be: |
89 | | - // <root>some text<allowedUser>oscar</allowedUser></root> |
90 | | - root.InnerXml = input; |
91 | | - } |
92 | | -} |
| 63 | +```xml |
| 64 | +<root>some text<allowedUser>oscar</allowedUser> |
| 65 | +</root> |
93 | 66 | ``` |
94 | 67 |
|
95 | | -```vb |
96 | | -Imports System |
97 | | -Imports System.Xml |
| 68 | +### Solution |
98 | 69 |
|
99 | | -Public Partial Class WebForm |
100 | | - Inherits System.Web.UI.Page |
| 70 | +To fix this violation, set the input to the <xref:System.Xml.XmlElement.InnerText> property of the root element instead of the <xref:System.Xml.XmlElement.InnerXml> property. |
101 | 71 |
|
102 | | - Sub Page_Load(sender As Object, e As EventArgs) |
103 | | - Dim input As String = Request.Form("in") |
104 | | - Dim d As XmlDocument = New XmlDocument() |
105 | | - Dim root As XmlElement = d.CreateElement("root") |
106 | | - d.AppendChild(root) |
| 72 | +:::code language="csharp" source="snippets/csharp/netfx/ca3009.cs" id="fix" highlight="12"::: |
107 | 73 |
|
108 | | - Dim allowedUser As XmlElement = d.CreateElement("allowedUser") |
109 | | - root.AppendChild(allowedUser) |
| 74 | +:::code language="vb" source="snippets/vb/netfx/ca3009.vb" id="fix" highlight="11"::: |
110 | 75 |
|
111 | | - allowedUser.InnerXml = "alice" |
| 76 | +If an attacker uses this for input: `some text<allowedUser>oscar</allowedUser>`, then the XML document will be: |
112 | 77 |
|
113 | | - ' If an attacker uses this for input: |
114 | | - ' some text<allowedUser>oscar</allowedUser> |
115 | | - ' Then the XML document will be: |
116 | | - ' <root>some text<allowedUser>oscar</allowedUser></root> |
117 | | - root.InnerXml = input |
118 | | - End Sub |
119 | | -End Class |
| 78 | +```xml |
| 79 | +<root>some text<allowedUser>oscar</allowedUser> |
| 80 | +<allowedUser>alice</allowedUser> |
| 81 | +</root> |
120 | 82 | ``` |
121 | 83 |
|
122 | | -### Solution |
123 | | - |
124 | | -```csharp |
125 | | -using System; |
126 | | -using System.Xml; |
127 | | - |
128 | | -public partial class WebForm : System.Web.UI.Page |
129 | | -{ |
130 | | - protected void Page_Load(object sender, EventArgs e) |
131 | | - { |
132 | | - string input = Request.Form["in"]; |
133 | | - XmlDocument d = new XmlDocument(); |
134 | | - XmlElement root = d.CreateElement("root"); |
135 | | - d.AppendChild(root); |
136 | | - |
137 | | - XmlElement allowedUser = d.CreateElement("allowedUser"); |
138 | | - root.AppendChild(allowedUser); |
139 | | - |
140 | | - allowedUser.InnerText = "alice"; |
141 | | - |
142 | | - // If an attacker uses this for input: |
143 | | - // some text<allowedUser>oscar</allowedUser> |
144 | | - // Then the XML document will be: |
145 | | - // <root><allowedUser>oscar</allowedUser>some text<allowedUser>alice</allowedUser></root> |
146 | | - root.InnerText = input; |
147 | | - } |
148 | | -} |
149 | | -``` |
150 | | - |
151 | | -```vb |
152 | | -Imports System |
153 | | -Imports System.Xml |
| 84 | +## Configure code to analyze |
154 | 85 |
|
155 | | -Public Partial Class WebForm |
156 | | - Inherits System.Web.UI.Page |
| 86 | +Use the following options to configure which parts of your codebase to run this rule on. |
157 | 87 |
|
158 | | - Sub Page_Load(sender As Object, e As EventArgs) |
159 | | - Dim input As String = Request.Form("in") |
160 | | - Dim d As XmlDocument = New XmlDocument() |
161 | | - Dim root As XmlElement = d.CreateElement("root") |
162 | | - d.AppendChild(root) |
| 88 | +- [Exclude specific symbols](#exclude-specific-symbols) |
| 89 | +- [Exclude specific types and their derived types](#exclude-specific-types-and-their-derived-types) |
163 | 90 |
|
164 | | - Dim allowedUser As XmlElement = d.CreateElement("allowedUser") |
165 | | - root.AppendChild(allowedUser) |
| 91 | +You can configure these options for just this rule, for all rules it applies to, or for all rules in this category ([Security](security-warnings.md)) that it applies to. For more information, see [Code quality rule configuration options](../code-quality-rule-options.md). |
166 | 92 |
|
167 | | - allowedUser.InnerText = "alice" |
| 93 | +[!INCLUDE[excluded-symbol-names](../includes/excluded-symbol-names.md)] |
168 | 94 |
|
169 | | - ' If an attacker uses this for input: |
170 | | - ' some text<allowedUser>oscar</allowedUser> |
171 | | - ' Then the XML document will be: |
172 | | - ' <root><allowedUser>oscar</allowedUser>some text<allowedUser>alice</allowedUser></root> |
173 | | - root.InnerText = input |
174 | | - End Sub |
175 | | -End Class |
176 | | -``` |
| 95 | +[!INCLUDE[excluded-type-names-with-derived-types](../includes/excluded-type-names-with-derived-types.md)] |
0 commit comments