|
| 1 | +--- |
| 2 | +title: "CA2310: Do not use insecure deserializer NetDataContractSerializer" |
| 3 | +ms.date: 05/01/2019 |
| 4 | +ms.topic: reference |
| 5 | +author: dotpaul |
| 6 | +ms.author: paulming |
| 7 | +manager: jillfra |
| 8 | +dev_langs: |
| 9 | + - CSharp |
| 10 | + - VB |
| 11 | +ms.workload: |
| 12 | + - "multiple" |
| 13 | +f1_keywords: |
| 14 | + - "CA2310" |
| 15 | + - "DoNotUseInsecureDeserializerNetDataContractSerializer" |
| 16 | +--- |
| 17 | +# CA2310: Do not use insecure deserializer NetDataContractSerializer |
| 18 | + |
| 19 | +||| |
| 20 | +|-|-| |
| 21 | +|TypeName|DoNotUseInsecureDeserializerNetDataContractSerializer| |
| 22 | +|CheckId|CA2310| |
| 23 | +|Category|Microsoft.Security| |
| 24 | +|Breaking Change|Non Breaking| |
| 25 | + |
| 26 | +## Cause |
| 27 | + |
| 28 | +A <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=nameWithType> deserialization method was called or referenced. |
| 29 | + |
| 30 | +## Rule description |
| 31 | + |
| 32 | +[!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)] |
| 33 | + |
| 34 | +This rule finds <xref:System.Runtime.Serialization.NetDataContractSerializer?displayProperty=nameWithType> deserialization method calls or references. If you want to deserialize only when the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property is set to restrict types, disable this rule and enable rules [CA2311](ca2311-do-not-deserialize-without-first-setting-netdatacontractserializer-binder.md) and [CA2312](ca2312-ensure-netdatacontractserializer-binder-is-set-before-deserializing.md) instead. |
| 35 | + |
| 36 | +## How to fix violations |
| 37 | + |
| 38 | +- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include: |
| 39 | + - <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType> |
| 40 | + - <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType> |
| 41 | + - <xref:System.Web.Script.Serialization.JavaScriptSerializer?displayProperty=nameWithType> - Never use <xref:System.Web.Script.Serialization.SimpleTypeResolver?displayProperty=nameWithType>. If you must use a type resolver, restrict deserialized types to an expected list. |
| 42 | + - <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType> |
| 43 | + - Newtonsoft Json.NET - Use TypeNameHandling.None. If you must use another value for TypeNameHandling, restrict deserialized types to an expected list with a custom ISerializationBinder. |
| 44 | + - Protocol Buffers |
| 45 | +- Make the serialized data tamper-proof. After serialization, cryptographically sign the serialized data. Before deserialization, validate the cryptographic signature. Protect the cryptographic key from being disclosed and design for key rotations. |
| 46 | +- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.NetDataContractSerializer>, set the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property to an instance of your custom <xref:System.Runtime.Serialization.SerializationBinder>. In the overridden <xref:System.Runtime.Serialization.SerializationBinder.BindToType%2A> method, if the type is unexpected, throw an exception. |
| 47 | +- If you restrict deserialized types, you may want to disable this rule and enable rules [CA2311](ca2311-do-not-deserialize-without-first-setting-netdatacontractserializer-binder.md) and [CA2312](ca2312-ensure-netdatacontractserializer-binder-is-set-before-deserializing.md). Rules [CA2311](ca2311-do-not-deserialize-without-first-setting-netdatacontractserializer-binder.md) and [CA2312](ca2312-ensure-netdatacontractserializer-binder-is-set-before-deserializing.md) help to ensure that the <xref:System.Runtime.Serialization.NetDataContractSerializer.Binder> property is always set before deserializing. |
| 48 | + |
| 49 | +## When to suppress warnings |
| 50 | + |
| 51 | +[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)] |
| 52 | + |
| 53 | +## Pseudo-code examples |
| 54 | + |
| 55 | +### Violation |
| 56 | + |
| 57 | +```csharp |
| 58 | +using System.IO; |
| 59 | +using System.Runtime.Serialization; |
| 60 | + |
| 61 | +public class ExampleClass |
| 62 | +{ |
| 63 | + public object MyDeserialize(byte[] bytes) |
| 64 | + { |
| 65 | + NetDataContractSerializer serializer = new NetDataContractSerializer(); |
| 66 | + return serializer.Deserialize(new MemoryStream(bytes)); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +```vb |
| 72 | +Imports System.IO |
| 73 | +Imports System.Runtime.Serialization |
| 74 | + |
| 75 | +Public Class ExampleClass |
| 76 | + Public Function MyDeserialize(bytes As Byte()) As Object |
| 77 | + Dim serializer As NetDataContractSerializer = New NetDataContractSerializer() |
| 78 | + Return serializer.Deserialize(New MemoryStream(bytes)) |
| 79 | + End Function |
| 80 | +End Class |
| 81 | +``` |
| 82 | + |
| 83 | +## Related rules |
| 84 | + |
| 85 | +[CA2311: Do not deserialize without first setting NetDataContractSerializer.Binder](ca2311-do-not-deserialize-without-first-setting-netdatacontractserializer-binder.md) |
| 86 | + |
| 87 | +[CA2312: Ensure NetDataContractSerializer.Binder is set before deserializing](ca2312-ensure-netdatacontractserializer-binder-is-set-before-deserializing.md) |
0 commit comments