Static analysis (SAST) check · C#

Json type name handling (C#)

Json.NET is told to honour type names written in the JSON ($type).

Why it matters

A payload can then name a gadget type whose constructor or setters run code.

How to fix it

Leave TypeNameHandling at None. If polymorphism is needed, use a SerializationBinder that allows only your own types, or System.Text.Json's JsonDerivedType attributes.

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 Newtonsoft.Json;

public static class Messages
{
    public static object Read(string json)
    {
        var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
        return JsonConvert.DeserializeObject(json, settings);
    }
}
Fixed: not reported (C#)
using Newtonsoft.Json;

public static class Messages
{
    public static Message Read(string json)
    {
        var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.None };
        return JsonConvert.DeserializeObject<Message>(json, settings);
    }
}

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.