Skip to content

Commit 79bbeb1

Browse files
authored
Merge pull request #6491 from LLLXXXCCC/ca5366
Ca5366
2 parents 0fbb6d7 + 00e512b commit 79bbeb1

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

docs/code-quality/ca5366.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
---
2+
title: "CA5366: Use XmlReader For DataSet Read XML"
3+
description: Provides information about code analysis rule CA5366, including causes, how to fix violations, and when to suppress it.
4+
ms.date: 04/30/2020
5+
ms.topic: reference
6+
author: LLLXXXCCC
7+
ms.author: linche
8+
manager: jillfra
9+
ms.workload:
10+
- "multiple"
11+
f1_keywords:
12+
- "CA5366"
13+
---
14+
# CA5366: Use XmlReader For DataSet Read XML
15+
16+
|||
17+
|-|-|
18+
|CheckId|CA5366|
19+
|Category|Microsoft.Security|
20+
|Breaking change|Non-breaking|
21+
22+
## Cause
23+
24+
A Document Type Definition (DTD) defines the structure and the legal elements and attributes of an XML document. Referring to a DTD from an external resource could cause potential Denial of Service (DoS) attacks. Most readers cannot disable DTD processing and restrict external references loading except for <xref:System.Xml.XmlReader?displayProperty=nameWithType>. Using these other readers to load XML by one of the following methods triggers this rule:
25+
- <xref:System.Data.DataSet.ReadXml%2A>
26+
- <xref:System.Data.DataSet.ReadXmlSchema%2A>
27+
- <xref:System.Data.DataSet.ReadXmlSerializable%2A>
28+
29+
## Rule description
30+
31+
Using a <xref:System.Data.DataSet?displayProperty=nameWithType> to read XML with untrusted data may load dangerous external references, which should be restricted by using an <xref:System.Xml.XmlReader> with a secure resolver or with DTD processing disabled.
32+
33+
## How to fix violations
34+
35+
Use <xref:System.Xml.XmlReader> or its derived classes to read XML.
36+
37+
## When to suppress warnings
38+
39+
Suppress a warning from this rule when dealing with a trusted data source.
40+
41+
## Pseudo-code examples
42+
43+
### Violation
44+
45+
```csharp
46+
using System.Data;
47+
using System.IO;
48+
49+
public class ExampleClass
50+
{
51+
public void ExampleMethod()
52+
{
53+
new DataSet().ReadXml(new FileStream("xmlFilename", FileMode.Open));
54+
}
55+
}
56+
```
57+
58+
### Solution
59+
60+
```csharp
61+
using System.Data;
62+
using System.IO;
63+
using System.Xml;
64+
65+
public class ExampleClass
66+
{
67+
public void ExampleMethod()
68+
{
69+
new DataSet().ReadXml(new XmlTextReader(new FileStream("xmlFilename", FileMode.Open)));
70+
}
71+
}
72+
```

docs/code-quality/code-analysis-warnings-for-managed-code-by-checkid.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,5 @@ The following table lists Code Analysis warnings for managed code by the CheckId
541541
| CA2246 | [CA2246: Do not assign a symbol and its member in the same statement](../code-quality/ca2246.md) | Assigning a symbol and its member, that is, a field or a property, in the same statement is not recommended. It is not clear if the member access was intended to use the symbol's old value prior to the assignment or the new value from the assignment in this statement. |
542542
| CA5122 | [CA5122 P/Invoke declarations should not be safe critical](../code-quality/ca5122.md) | Methods are marked as SecuritySafeCritical when they perform a security sensitive operation, but are also safe to be used by transparent code. Transparent code may never directly call native code through a P/Invoke. Therefore, marking a P/Invoke as security safe critical will not enable transparent code to call it, and is misleading for security analysis. |
543543
| CA5365 | [CA5365 Do Not Disable HTTP Header Checking](../code-quality/ca5365.md) | HTTP header checking enables encoding of the carriage return and newline characters, \r and \n, that are found in response headers. This encoding can help to avoid injection attacks that exploit an application that echoes untrusted data contained by the header. |
544+
| CA5366 | [CA5366 Use XmlReader For DataSet Read XML](../code-quality/ca5366.md) | Using a <xref:System.Data.DataSet> to read XML with untrusted data may load dangerous external references, which should be restricted by using an <xref:System.Xml.XmlReader> with a secure resolver or with DTD processing disabled. |
544545
| CA5374 | [CA5374 Do Not Use XslTransform](../code-quality/ca5374.md) | This rule checks if <xref:System.Xml.Xsl.XslTransform?displayProperty=nameWithType> is instantiated in the code. <xref:System.Xml.Xsl.XslTransform?displayProperty=nameWithType> is now obsolete and shouldn’t be used. |

docs/code-quality/security-warnings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ Security warnings support safer libraries and applications. These warnings help
104104
|[CA5363: Do not disable request validation](../code-quality/ca5363.md)|Request validation is a feature in ASP.NET that examines HTTP requests and determines whether they contain potentially dangerous content that can lead to injection attacks, including cross-site-scripting.|
105105
|[CA5364: Do not use deprecated security protocols](../code-quality/ca5364.md)|Transport Layer Security (TLS) secures communication between computers, most commonly with Hypertext Transfer Protocol Secure (HTTPS). Older protocol versions of TLS are less secure than TLS 1.2 and TLS 1.3 and are more likely to have new vulnerabilities. Avoid older protocol versions to minimize risk.|
106106
|[CA5365: Do Not Disable HTTP Header Checking](../code-quality/ca5365.md)|HTTP header checking enables encoding of the carriage return and newline characters, \r and \n, that are found in response headers. This encoding can help to avoid injection attacks that exploit an application that echoes untrusted data contained by the header.|
107+
|[CA5366: Use XmlReader For DataSet Read XML](../code-quality/ca5366.md)|Using a <xref:System.Data.DataSet> to read XML with untrusted data may load dangerous external references, which should be restricted by using an <xref:System.Xml.XmlReader> with a secure resolver or with DTD processing disabled.|
107108
|[CA5369: Use XmlReader for Deserialize](../code-quality/ca5369.md)|Processing untrusted DTD and XML schemas may enable loading dangerous external references, which should be restricted by using an XmlReader with a secure resolver or with DTD and XML inline schema processing disabled.|
108109
|[CA5370: Use XmlReader for validating reader](../code-quality/ca5370.md)|Processing untrusted DTD and XML schemas may enable loading dangerous external references. This dangerous loading can be restricted by using an XmlReader with a secure resolver or with DTD and XML inline schema processing disabled.|
109110
|[CA5371: Use XmlReader for schema read](../code-quality/ca5371.md)|Processing untrusted DTD and XML schemas may enable loading dangerous external references. Using an XmlReader with a secure resolver or with DTD and XML inline schema processing disabled restricts this.|

docs/code-quality/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,8 @@
754754
href: ca5364.md
755755
- name: "CA5365: Do Not Disable HTTP Header Checking"
756756
href: ca5365.md
757+
- name: "CA5366: Use XmlReader For DataSet Read XML"
758+
href: ca5366.md
757759
- name: "CA5369: Use XmlReader for Deserialize"
758760
href: ca5369.md
759761
- name: "CA5370: Use XmlReader for validating reader"

0 commit comments

Comments
 (0)