-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Enhance JSON Deserialization Security - Mitigate TypeNameHandling Vulnerability #7423
Conversation
…ing Vulnerability** This commit addresses a potential security vulnerability related to JSON deserialization by enhancing the type handling mechanism. **Issue:** The previous deserialization configuration was using `TypeNameHandling.Auto` in Newtonsoft.Json. This setting, while convenient, introduces a significant security risk. `TypeNameHandling.Auto` allows for automatic deserialization of types based on `$type` metadata embedded in the JSON. If an attacker can control the JSON input, they can potentially inject malicious `$type` properties to instantiate arbitrary types, leading to Remote Code Execution (RCE) vulnerabilities. **Fix Implemented:** To mitigate this risk, the following changes have been made: 1. **Implemented Secure Deserialization with KnownTypes Whitelist:** - Updated TypeNameSerializationBinder binder to utilize a `KnownTypes` whitelist, explicitly defining the set of allowed types that can be deserialized. - The deserializer is now configured to use this `SerializationBinder`, ensuring that only types present in the `KnownTypes` whitelist are permitted for deserialization. This significantly restricts the attack surface and prevents the instantiation of unauthorized or potentially malicious types. - This approach aligns with secure deserialization best practices and follows the guidance outlined in: [https://liquid.microsoft.com/Web/Object/Read/MS.Security/Requirements/Microsoft.Security.SystemsADM.10010#Zguide](https://liquid.microsoft.com/Web/Object/Read/MS.Security/Requirements/Microsoft.Security.SystemsADM.10010#Zguide) 2. **Disabled Automatic Type Name Handling (`TypeNameHandling.None`):** - The `TypeNameHandling` setting in `JsonSerializerSettings` has been explicitly set to `TypeNameHandling.None`. - This is because the serialized JSON in our use case does not intentionally include `$type` metadata. Setting `TypeNameHandling.None` ensures that automatic `$type` processing is completely disabled, further enhancing security and aligning with the explicit type control implemented by the `SerializationBinder`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
edge-agent/test/Microsoft.Azure.Devices.Edge.Agent.Integration.Test/AgentTestsBase.cs
Show resolved
Hide resolved
edge-util/test/Microsoft.Azure.Devices.Edge.Util.Test.Common/TypeNameSerializationBinder.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the PR description, can you make it clear that this is in test code, so the potential security impact does not include production code running on customers' devices?
Bug 30973440 and Bug 30973442
CodeQL issue: https://liquid.microsoft.com/codeql/issues/621b3860-9992-462a-8a9d-0a24593f51a5?copilot_promptid=E91B0CE9-0C1B-4AC2-8A46-33F49B67E058
This commit addresses a potential security vulnerability within our test code and infrastructure related to JSON deserialization by enhancing the type handling mechanism.
Issue:
The previous deserialization configuration was using
TypeNameHandling.Auto
in Newtonsoft.Json.TypeNameHandling.Auto
allows for automatic deserialization of types based on$type
metadata embedded in the JSON. If an attacker can control the JSON input, they can potentially inject malicious$type
properties to instantiate arbitrary types, leading to Remote Code Execution (RCE) vulnerabilities. This is related to our test infrastructure, so the potential security impact does not include production code running on customers' devicesFix Implemented:
To mitigate this risk, the following changes have been made:
Disabled Automatic Type Name Handling (
TypeNameHandling.None
):TypeNameHandling
setting inJsonSerializerSettings
has been explicitly set toTypeNameHandling.None
.$type
metadata. SettingTypeNameHandling.None
ensures that automatic$type
processing is completely disabled, further enhancing security.Implemented Secure Deserialization with KnownTypes Whitelist:
KnownTypes
whitelist, explicitly defining the set of allowed types that can be deserialized.SerializationBinder
, ensuring that only types present in theKnownTypes
whitelist are permitted for deserialization. This significantly restricts the attack surface and prevents the instantiation of unauthorized or potentially malicious types.and recommendation: Solution using custom ISerializationBinder: https://liquid.microsoft.com/Web/Object/Read/ScanningToolWarnings/Requirements/CodeQL.SM02211#Zguide
Tested the changes in the local:
References:
https://liquid.microsoft.com/Web/Object/Read/ScanningToolWarnings/Requirements/CodeQL.SM02211#Zguide
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2326
https://liquid.microsoft.com/Web/Object/Read/MS.Security/Requirements/Microsoft.Security.SystemsADM.10010#Zguide
Azure IoT Edge PR checklist:
This checklist is used to make sure that common guidelines for a pull request are followed.
General Guidelines and Best Practices
Testing Guidelines
Draft PRs
Draft
mode if it is:Note: We use the kodiakhq bot to merge PRs once the necessary checks and approvals are in place. When it merges a PR, kodiakhq converts the PR title to the commit title, PR description to the commit description, and squashes all the commits in the PR to a single commit. The net effect is that entire PR becomes a single commit. Please follow the best practices mentioned here for the PR title and description