Home >Backend Development >C++ >Is Automatic JSON Deserialization with Json.Net's `TypeNameHandling.Auto` Secure, Even When Limiting Deserialization to a Specific Type?

Is Automatic JSON Deserialization with Json.Net's `TypeNameHandling.Auto` Secure, Even When Limiting Deserialization to a Specific Type?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-07 14:16:41886browse

Is Automatic JSON Deserialization with Json.Net's `TypeNameHandling.Auto` Secure, Even When Limiting Deserialization to a Specific Type?

Can External JSON Be Vulnerable Due to Json.Net TypeNameHandling Auto?

Problem:

In website applications where users upload custom JSON objects, it is imperative to be aware of potential threats arising from automated JSON type deserialization. The question is whether automatic type deserialization is susceptible to vulnerabilities if the only type deserialized is a specific type (e.g., MyObject) and none of MyObject's members have the type System.Object or dynamic.

Answer:

While adhering to these conditions significantly reduces the risk, it does not guarantee complete protection. Json.Net's TypeNameHandling setting, when set to Auto, can potentially create objects based on "$type" information even when no corresponding field exists in MyObject.

Detailed Explanation:

Attacks targeting Json.Net exploit the TypeNameHandling setting to construct "attack gadgets" - objects designed to compromise the receiving system. Json.Net's protection mechanisms include ignoring unknown properties and checking for type compatibility. However, there are scenarios where an attack gadget can be constructed even without any obvious untyped members:

  • Deserialization of untyped collections (e.g., ArrayList, List)
  • Deserialization of semi-typed collections (e.g., CollectionBase)
  • Deserialization of types that implement ISerializable (e.g., Exception)
  • Deserialization of types with members conditional serialization (e.g., public object tempData; public bool ShouldSerializeTempData() { return false; })
  • Recommendations:

    • Use caution: TypeNameHandling should be used prudently when deserializing external JSON, and a custom SerializationBinder is recommended for validation.
    • Review Data Model: Ensure that no member types are object, dynamic, or compatible with attack gadgets.
    • Consider Serialization Binder: Implement a custom SerializationBinder to strictly control which types are deserialized.

    In conclusion, while the provided conditions significantly mitigate risk, it is important to note that it does not guarantee complete security. Json.Net's TypeNameHandling Auto setting may still potentially facilitate the creation of attack gadgets, necessitating additional precautions such as custom serialization binders.

    The above is the detailed content of Is Automatic JSON Deserialization with Json.Net's `TypeNameHandling.Auto` Secure, Even When Limiting Deserialization to a Specific Type?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn