Static analysis (SAST) check · C#

BinaryFormatter use (C#)

BinaryFormatter (and SoapFormatter, NetDataContractSerializer, LosFormatter) instantiate whatever type the serialised data names.

Why it matters

Deserialising data an attacker can influence runs their code.

How to fix it

Replace the formatter with System.Text.Json or a contract-based serialiser bound to fixed types. Microsoft has removed BinaryFormatter from .NET 9.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (C#)
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class Cache
{
    public static object Load(Stream s)
    {
        var formatter = new BinaryFormatter();
        return formatter.Deserialize(s);
    }
}
Fixed: not reported (C#)
using System.IO;
using System.Text.Json;

public static class Cache
{
    public static CacheEntry Load(Stream s)
    {
        return JsonSerializer.Deserialize<CacheEntry>(s);
    }
}

What TrustIvan reads

Runs on repository scans of C# source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.