|
1 | 1 | ---
|
2 | 2 | title: "CA2302: Ensure BinaryFormatter.Binder is set before calling BinaryFormatter.Deserialize"
|
3 |
| -ms.date: 04/05/2019 |
| 3 | +ms.date: 07/15/2020 |
4 | 4 | ms.topic: reference
|
5 | 5 | author: dotpaul
|
6 | 6 | ms.author: paulming
|
@@ -30,28 +30,23 @@ This rule is similar to [CA2301](ca2301.md), but analysis can't determine if the
|
30 | 30 |
|
31 | 31 | By default, this rule analyzes the entire codebase, but this is [configurable](#configurability).
|
32 | 32 |
|
| 33 | +> [!WARNING] |
| 34 | +> Restricting types with a SerializationBinder can't prevent all attacks. For more information, see the [BinaryFormatter security guide](/dotnet/standard/serialization/binaryformatter-security-guide). |
| 35 | +
|
33 | 36 | ## Rule description
|
34 | 37 |
|
35 | 38 | [!INCLUDE[insecure-deserializers-description](includes/insecure-deserializers-description-md.md)]
|
36 | 39 |
|
37 | 40 | This rule finds <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter?displayProperty=nameWithType> deserialization method calls or references when the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> might be null. If you want to disallow any deserialization with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> regardless of the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property, disable this rule and [CA2301](ca2301.md), and enable rule [CA2300](ca2300.md).
|
38 | 41 |
|
| 42 | + |
39 | 43 | ## How to fix violations
|
40 | 44 |
|
41 |
| -- If possible, use a secure serializer instead, and **don't allow an attacker to specify an arbitrary type to deserialize**. Some safer serializers include: |
42 |
| - - <xref:System.Runtime.Serialization.DataContractSerializer?displayProperty=nameWithType> |
43 |
| - - <xref:System.Runtime.Serialization.Json.DataContractJsonSerializer?displayProperty=nameWithType> |
44 |
| - - <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. |
45 |
| - - <xref:System.Xml.Serialization.XmlSerializer?displayProperty=nameWithType> |
46 |
| - - 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. |
47 |
| - - Protocol Buffers |
48 |
| -- 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. |
49 |
| -- Restrict deserialized types. Implement a custom <xref:System.Runtime.Serialization.SerializationBinder?displayProperty=nameWithType>. Before deserializing with <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter>, set the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.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 to stop deserialization. |
50 |
| - - Ensure that all code paths have the <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Binder> property set. |
| 45 | +[!INCLUDE[fix-binaryformatter-serializationbinder](includes/fix-binaryformatter-serializationbinder.md)] |
51 | 46 |
|
52 | 47 | ## When to suppress warnings
|
53 | 48 |
|
54 |
| -[!INCLUDE[insecure-deserializers-common-safe-to-suppress](includes/insecure-deserializers-common-safe-to-suppress-md.md)] |
| 49 | +[!INCLUDE[cannot-secure-binaryformatter](includes/cannot-secure-binaryformatter.md)] |
55 | 50 |
|
56 | 51 | ## Configurability
|
57 | 52 |
|
|
0 commit comments