C#idiomatic C#
ca2315fundamentals/code-analysis/quality-rules

Code analysis

CA2315: Do not use insecure deserializer ObjectStateFormatter

Learn about code analysis rule CA2315: Do not use insecure deserializer ObjectStateFormatter

CA2315 Security enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

A System.Web.UI.ObjectStateFormatter deserialization method was called or referenced.

Rule description


author: dotpaul ms.author: paulming ms.date: 04/05/2019 ms.topic: include

Insecure deserializers are vulnerable when deserializing untrusted data. An attacker could modify the serialized data to include unexpected types to inject objects with malicious side effects. An attack against an insecure deserializer could, for example, execute commands on the underlying operating system, communicate over the network, or delete files.

This rule finds System.Web.UI.ObjectStateFormatter deserialization method calls or references.

How to fix violations


author: dotpaul ms.author: paulming ms.date: 05/01/2019 ms.topic: include

When to suppress warnings


author: dotpaul ms.author: paulming ms.date: 04/17/2019 ms.topic: include

It's safe to suppress a warning from this rule if:

  • You know the input is trusted. Consider that your application's trust boundary and data flows may change over time.
  • You've taken one of the precautions in How to fix violations.

Pseudo-code examples

Violation

csharp
using System.IO;
using System.Web.UI;

public class ExampleClass
{
    public object MyDeserialize(byte[] bytes)
    {
        ObjectStateFormatter formatter = new ObjectStateFormatter();
        return formatter.Deserialize(new MemoryStream(bytes));
    }
}
vb
Imports System.IO
Imports System.Web.UI

Public Class ExampleClass
    Public Function MyDeserialize(bytes As Byte()) As Object
        Dim formatter As ObjectStateFormatter = New ObjectStateFormatter()
        Return formatter.Deserialize(New MemoryStream(bytes))
    End Function
End Class