I'm trying to figure out how "safe" this C# server-side code is against a maliciously crafted "text" string:
using Newtonsoft.Json;
...
public class FooController {
private class FooData { ... }
...
var fooObject = (FooData)JsonConvert.Deserialize(text, typeof(FooData));
...
}
Looking at the documentation for Json.NET Schema, it looks like JSchemaGenerator can be used to generate a JSON schema for a given C# class, which may then be used by JSchemaValidatingReader to validate inbound text prior to using JsonSerlializer to deserialize it.
I would be reassured to know that these steps are all done automatically when invoking JsonConvert.Deserialize() with a type as in the above code snippet. But unfortunately, I don't see this documented explicitly on newtonsoft.com, and simply throwing a few invalid JSON examples at the Deserialize() method doesn't convince me that all the security angles are covered.
My nightmare scenario is using code like this (perhaps together with an upstream check on the length of the text input) in an API endpoint and being owned by an injection attack.