Share via


CA2302: Kontrollera att BinaryFormatter.Binder har angetts innan du anropar BinaryFormatter.Deserialize

Property Värde
Regel-ID CA2302
Title Kontrollera att BinaryFormatter.Binder har angetts innan du anropar BinaryFormatter.Deserialize
Kategori Säkerhet
Korrigeringen är icke-bakåtkompatibel Icke-icke-bryta
Aktiverad som standard i .NET 8 Nej

Orsak

En System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserialiseringsmetod anropades eller refererades till och egenskapen Binder kan vara null.

Den här regeln liknar CA2301, men analysen kan inte avgöra om är Binder definitivt null.

Som standard analyserar den här regeln hela kodbasen, men detta kan konfigureras.

Varning

Att begränsa typer med en SerializationBinder kan inte förhindra alla attacker. Mer information finns i säkerhetsguiden för BinaryFormatter.

Regelbeskrivning

Osäkra deserialiserare är sårbara när deserialisera ej betrodda data. En angripare kan ändra serialiserade data så att de innehåller oväntade typer för att mata in objekt med skadliga biverkningar. En attack mot en osäker deserialiserare kan till exempel köra kommandon på det underliggande operativsystemet, kommunicera via nätverket eller ta bort filer.

Den här regeln hittar System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserialiseringsmetodanrop eller referenser när kan Binder vara null. Om du vill neka all deserialisering med BinaryFormatter oavsett Binder egenskap inaktiverar du den här regeln och CA2301 och aktiverar regeln CA2300.

Så här åtgärdar du överträdelser

  • Använd en säker serialiserare i stället och tillåt inte att en angripare anger en godtycklig typ att deserialisera. Mer information finns i Föredragna alternativ.
  • Gör serialiserade data manipuleringssäkra. Efter serialiseringen signerar kryptografiskt serialiserade data. Verifiera den kryptografiska signaturen innan deserialiseringen. Skydda den kryptografiska nyckeln från att avslöjas och utformas för nyckelrotationer.
  • Det här alternativet gör koden sårbar för överbelastningsattacker och möjliga fjärrkörningsattacker för kod i framtiden. Mer information finns i säkerhetsguiden för BinaryFormatter. Begränsa deserialiserade typer. Implementera en anpassad System.Runtime.Serialization.SerializationBinder. Innan du deserialiserar anger du Binder egenskapen till en instans av din anpassade SerializationBinder i alla kodsökvägar. Om typen är oväntad i den åsidosatta BindToType metoden genererar du ett undantag för att stoppa deserialiseringen.

När du ska ignorera varningar

BinaryFormatter är osäker och kan inte göras säker.

Konfigurera kod för analys

Använd följande alternativ för att konfigurera vilka delar av kodbasen som regeln ska köras på.

Du kan konfigurera dessa alternativ för bara den här regeln, för alla regler som den gäller för eller för alla regler i den här kategorin (säkerhet) som den gäller för. Mer information finns i Konfigurationsalternativ för kodkvalitetsregel.

Exkludera specifika symboler

Du kan exkludera specifika symboler, till exempel typer och metoder, från analys. Om du till exempel vill ange att regeln inte ska köras på någon kod inom typer med namnet MyTypelägger du till följande nyckel/värde-par i en .editorconfig-fil i projektet:

dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType

Tillåtna symbolnamnformat i alternativvärdet (avgränsade med |):

  • Endast symbolnamn (innehåller alla symboler med namnet, oavsett vilken typ eller namnrymd som innehåller).
  • Fullständigt kvalificerade namn i symbolens dokumentations-ID-format. Varje symbolnamn kräver ett symboltypprefix, till exempel M: för metoder, T: för typer och N: för namnområden.
  • .ctor för konstruktorer och .cctor statiska konstruktorer.

Exempel:

Alternativvärde Sammanfattning
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType Matchar alla symboler med namnet MyType.
dotnet_code_quality.CAXXXX.excluded_symbol_names = MyType1|MyType2 Matchar alla symboler med namnet antingen MyType1 eller MyType2.
dotnet_code_quality.CAXXXX.excluded_symbol_names = M:NS.MyType.MyMethod(ParamType) Matchar en specifik metod MyMethod med den angivna fullständigt kvalificerade signaturen.
dotnet_code_quality.CAXXXX.excluded_symbol_names = M:NS1.MyType1.MyMethod1(ParamType)|M:NS2.MyType2.MyMethod2(ParamType) Matchar specifika metoder MyMethod1 och MyMethod2 med respektive fullständigt kvalificerade signaturer.

Exkludera specifika typer och deras härledda typer

Du kan exkludera specifika typer och deras härledda typer från analys. Om du till exempel vill ange att regeln inte ska köras på några metoder inom typer som heter MyType och deras härledda typer lägger du till följande nyckel/värde-par i en .editorconfig-fil i projektet:

dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType

Tillåtna symbolnamnformat i alternativvärdet (avgränsade med |):

  • Skriv endast namn (innehåller alla typer med namnet, oavsett vilken typ eller namnrymd som innehåller).
  • Fullständigt kvalificerade namn i symbolens dokumentations-ID-format, med ett valfritt T: prefix.

Exempel:

Alternativvärde Sammanfattning
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType Matchar alla typer med namnet MyType och alla deras härledda typer.
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = MyType1|MyType2 Matchar alla typer med namnet antingen MyType1 eller MyType2 och alla deras härledda typer.
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = M:NS.MyType Matchar en specifik typ MyType med ett angivet fullständigt kvalificerat namn och alla dess härledda typer.
dotnet_code_quality.CAXXXX.excluded_type_names_with_derived_types = M:NS1.MyType1|M:NS2.MyType2 Matchar specifika typer MyType1 och MyType2 med respektive fullständigt kvalificerade namn och alla deras härledda typer.

Exempel på pseudokod

Överträdelse 1

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class BookRecordSerializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        // One way to discover expected types is through testing deserialization
        // of **valid** data and logging the types used.

        ////Console.WriteLine($"BindToType('{assemblyName}', '{typeName}')");

        if (typeName == "BookRecord")
        {
            return typeof(BookRecord);
        }
        else if (typeName == "AisleLocation")
        {
            return typeof(AisleLocation);
        }
        else
        {
            throw new ArgumentException("Unexpected type", nameof(typeName));
        }
    }
}

[Serializable]
public class BookRecord
{
    public string Title { get; set; }
    public AisleLocation Location { get; set; }
}

[Serializable]
public class AisleLocation
{
    public char Aisle { get; set; }
    public byte Shelf { get; set; }
}

public class Binders
{
    public static SerializationBinder BookRecord =
        new BookRecordSerializationBinder();
}

public class ExampleClass
{
    public BookRecord DeserializeBookRecord(byte[] bytes)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Binder = Binders.BookRecord;
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            return (BookRecord)formatter.Deserialize(ms);    // CA2302 violation
        }
    }
}
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class BookRecordSerializationBinder
    Inherits SerializationBinder

    Public Overrides Function BindToType(assemblyName As String, typeName As String) As Type
        ' One way to discover expected types is through testing deserialization
        ' of **valid** data and logging the types used.

        'Console.WriteLine($"BindToType('{assemblyName}', '{typeName}')")

        If typeName = "BinaryFormatterVB.BookRecord" Then
            Return GetType(BookRecord)
        Else If typeName = "BinaryFormatterVB.AisleLocation" Then
            Return GetType(AisleLocation)
        Else
            Throw New ArgumentException("Unexpected type", NameOf(typeName))
        End If
    End Function
End Class

<Serializable()>
Public Class BookRecord
    Public Property Title As String
    Public Property Location As AisleLocation
End Class

<Serializable()>
Public Class AisleLocation
    Public Property Aisle As Char
    Public Property Shelf As Byte
End Class

Public Class Binders
    Public Shared Property BookRecord As SerializationBinder = New BookRecordSerializationBinder()
End Class

Public Class ExampleClass
    Public Function DeserializeBookRecord(bytes As Byte()) As BookRecord
        Dim formatter As BinaryFormatter = New BinaryFormatter()
        formatter.Binder = Binders.BookRecord
        Using ms As MemoryStream = New MemoryStream(bytes)
            Return CType(formatter.Deserialize(ms), BookRecord)    ' CA2302 violation
        End Using
    End Function
End Class

Lösning 1

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class BookRecordSerializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        // One way to discover expected types is through testing deserialization
        // of **valid** data and logging the types used.

        ////Console.WriteLine($"BindToType('{assemblyName}', '{typeName}')");

        if (typeName == "BookRecord")
        {
            return typeof(BookRecord);
        }
        else if (typeName == "AisleLocation")
        {
            return typeof(AisleLocation);
        }
        else
        {
            throw new ArgumentException("Unexpected type", nameof(typeName));
        }
    }
}

[Serializable]
public class BookRecord
{
    public string Title { get; set; }
    public AisleLocation Location { get; set; }
}

[Serializable]
public class AisleLocation
{
    public char Aisle { get; set; }
    public byte Shelf { get; set; }
}

public class Binders
{
    public static SerializationBinder BookRecord =
        new BookRecordSerializationBinder();
}

public class ExampleClass
{
    public BookRecord DeserializeBookRecord(byte[] bytes)
    {
        BinaryFormatter formatter = new BinaryFormatter();

        // Ensure that Binder is always non-null before deserializing
        formatter.Binder = Binders.BookRecord ?? throw new Exception("Expected non-null binder");

        using (MemoryStream ms = new MemoryStream(bytes))
        {
            return (BookRecord)formatter.Deserialize(ms);
        }
    }
}
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class BookRecordSerializationBinder
    Inherits SerializationBinder

    Public Overrides Function BindToType(assemblyName As String, typeName As String) As Type
        ' One way to discover expected types is through testing deserialization
        ' of **valid** data and logging the types used.

        'Console.WriteLine($"BindToType('{assemblyName}', '{typeName}')")

        If typeName = "BinaryFormatterVB.BookRecord" Then
            Return GetType(BookRecord)
        Else If typeName = "BinaryFormatterVB.AisleLocation" Then
            Return GetType(AisleLocation)
        Else
            Throw New ArgumentException("Unexpected type", NameOf(typeName))
        End If
    End Function
End Class

<Serializable()>
Public Class BookRecord
    Public Property Title As String
    Public Property Location As AisleLocation
End Class

<Serializable()>
Public Class AisleLocation
    Public Property Aisle As Char
    Public Property Shelf As Byte
End Class

Public Class Binders
    Public Shared Property BookRecord As SerializationBinder = New BookRecordSerializationBinder()
End Class

Public Class ExampleClass
    Public Function DeserializeBookRecord(bytes As Byte()) As BookRecord
        Dim formatter As BinaryFormatter = New BinaryFormatter()

        ' Ensure that Binder is always non-null before deserializing
        formatter.Binder = If(Binders.BookRecord, New Exception("Expected non-null"))

        Using ms As MemoryStream = New MemoryStream(bytes)
            Return CType(formatter.Deserialize(ms), BookRecord)
        End Using
    End Function
End Class

Överträdelse 2

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class BookRecord
{
    public string Title { get; set; }
    public AisleLocation Location { get; set; }
}

[Serializable]
public class AisleLocation
{
    public char Aisle { get; set; }
    public byte Shelf { get; set; }
}

public class ExampleClass
{
    public BinaryFormatter Formatter { get; set; }

    public BookRecord DeserializeBookRecord(byte[] bytes)
    {
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            return (BookRecord) this.Formatter.Deserialize(ms);    // CA2302 violation
        }
    }
}
Imports System
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

<Serializable()>
Public Class BookRecord
    Public Property Title As String
    Public Property Location As AisleLocation
End Class

<Serializable()>
Public Class AisleLocation
    Public Property Aisle As Char
    Public Property Shelf As Byte
End Class

Public Class ExampleClass
    Public Property Formatter As BinaryFormatter

    Public Function DeserializeBookRecord(bytes As Byte()) As BookRecord
        Using ms As MemoryStream = New MemoryStream(bytes)
            Return CType(Me.Formatter.Deserialize(ms), BookRecord)    ' CA2302 violation
        End Using
    End Function
End Class

Lösning 2

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class BookRecordSerializationBinder : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        // One way to discover expected types is through testing deserialization
        // of **valid** data and logging the types used.

        ////Console.WriteLine($"BindToType('{assemblyName}', '{typeName}')");

        if (typeName == "BookRecord")
        {
            return typeof(BookRecord);
        }
        else if (typeName == "AisleLocation")
        {
            return typeof(AisleLocation);
        }
        else
        {
            throw new ArgumentException("Unexpected type", nameof(typeName));
        }
    }
}

[Serializable]
public class BookRecord
{
    public string Title { get; set; }
    public AisleLocation Location { get; set; }
}

[Serializable]
public class AisleLocation
{
    public char Aisle { get; set; }
    public byte Shelf { get; set; }
}

public class ExampleClass
{
    public BookRecord DeserializeBookRecord(byte[] bytes)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Binder = new BookRecordSerializationBinder();
        using (MemoryStream ms = new MemoryStream(bytes))
        {
            return (BookRecord) formatter.Deserialize(ms);
        }
    }
}
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary

Public Class BookRecordSerializationBinder
    Inherits SerializationBinder

    Public Overrides Function BindToType(assemblyName As String, typeName As String) As Type
        ' One way to discover expected types is through testing deserialization
        ' of **valid** data and logging the types used.

        'Console.WriteLine($"BindToType('{assemblyName}', '{typeName}')")

        If typeName = "BinaryFormatterVB.BookRecord" Then
            Return GetType(BookRecord)
        Else If typeName = "BinaryFormatterVB.AisleLocation" Then
            Return GetType(AisleLocation)
        Else
            Throw New ArgumentException("Unexpected type", NameOf(typeName))
        End If
    End Function
End Class

<Serializable()>
Public Class BookRecord
    Public Property Title As String
    Public Property Location As AisleLocation
End Class

<Serializable()>
Public Class AisleLocation
    Public Property Aisle As Char
    Public Property Shelf As Byte
End Class

Public Class ExampleClass
    Public Function DeserializeBookRecord(bytes As Byte()) As BookRecord
        Dim formatter As BinaryFormatter = New BinaryFormatter()
        formatter.Binder = New BookRecordSerializationBinder()
        Using ms As MemoryStream = New MemoryStream(bytes)
            Return CType(formatter.Deserialize(ms), BookRecord)
        End Using
    End Function
End Class