Repository navigation
Prevent Unsafe Type Materialization when Deserializing Entity Response Exceptions - #3517
Naiyuan Tian (nytian) wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens ResponseMessage.GetResult exception materialization against attacker-controlled Json.NET type metadata by validating exception types and forcing TypeNameHandling.None during exception deserialization, with regression tests to cover the new behavior.
Changes:
- Guard exception deserialization by ensuring
ExceptionTyperesolves to anException-derived type before materializing. - Disable Json.NET
$typehandling when deserializing exception payloads to avoid nested type metadata instantiation. - Add unit tests validating non-
Exceptiontypes are not deserialized and nested$typemetadata is ignored.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| test/FunctionsV2/Tests/Unit/ResponseMessageTests.cs | Adds regression coverage for unsafe exception-type materialization and nested $type metadata. |
| src/WebJobs.Extensions.DurableTask/EntityScheduler/ResponseMessage.cs | Tightens exception deserialization to only materialize Exception types and disables Json.NET type metadata handling. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (1)
src/WebJobs.Extensions.DurableTask/EntityScheduler/ResponseMessage.cs:74
- Current implementation parses the exception JSON into a JToken and then re-deserializes it via ToObject, which adds an extra full JSON materialization/allocation on an error path that could still be hit frequently (e.g., failed entity operations). You can deserialize directly from the JsonTextReader with the same serializer (still forcing TypeNameHandling=None) to avoid the extra parse while preserving DateParseHandling behavior.
JsonSerializer serializer = JsonSerializer.Create(errorDataConverter.JsonSettings);
serializer.TypeNameHandling = TypeNameHandling.None;
using (var stringReader = new System.IO.StringReader(this.Result))
using (var jsonReader = new JsonTextReader(stringReader) { DateParseHandling = serializer.DateParseHandling })
{
e = (Exception)JToken.Load(jsonReader).ToObject(type, serializer);
}
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (2)
src/WebJobs.Extensions.DurableTask/EntityScheduler/ResponseMessage.cs:74
- The exception deserialization path buffers the entire payload into a JToken and then re-serializes it via ToObject. This adds extra allocations and CPU (especially when exception payloads are large) without providing additional safety compared to directly deserializing with the configured JsonSerializer where TypeNameHandling is already forced to None.
JsonSerializer serializer = JsonSerializer.Create(errorDataConverter.JsonSettings);
serializer.TypeNameHandling = TypeNameHandling.None;
using (var stringReader = new System.IO.StringReader(this.Result))
using (var jsonReader = new JsonTextReader(stringReader) { DateParseHandling = serializer.DateParseHandling })
{
e = (Exception)JToken.Load(jsonReader).ToObject(type, serializer);
}
test/FunctionsV2/Tests/Unit/ResponseMessageTests.cs:40
- The tests validate nested "$type" metadata is ignored, but they don’t currently exercise a root-level "$type" on the exception JSON (which is the most direct TypeNameHandling vector). Adding a root "$type" entry here would cover both root and nested type metadata handling and better match the PR intent.
var result = new JObject
{
[nameof(ExceptionWithPayload.Payload)] = new JObject
{
["$type"] = typeof(DeserializationCallback).AssemblyQualifiedName,
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (1)
src/WebJobs.Extensions.DurableTask/EntityScheduler/ResponseMessage.cs:74
- Exception deserialization now goes through JToken.Load with a JsonTextReader that only copies DateParseHandling. This can change behavior for custom error serializers (e.g., FloatParseHandling or MaxDepth) and adds an avoidable full in-memory JToken parse. Consider deserializing directly from the JsonTextReader and copying the relevant reader settings from the serializer so custom JsonSerializerSettings continue to be honored while still forcing TypeNameHandling=None.
JsonSerializer serializer = JsonSerializer.Create(errorDataConverter.JsonSettings);
serializer.TypeNameHandling = TypeNameHandling.None;
using (var stringReader = new System.IO.StringReader(this.Result))
using (var jsonReader = new JsonTextReader(stringReader) { DateParseHandling = serializer.DateParseHandling })
{
e = (Exception)JToken.Load(jsonReader).ToObject(type, serializer);
}
| if (typeof(Exception).IsAssignableFrom(type)) | ||
| { | ||
| JsonSerializer serializer = JsonSerializer.Create(errorDataConverter.JsonSettings); | ||
| serializer.TypeNameHandling = TypeNameHandling.None; |
There was a problem hiding this comment.
Should we document that we are deserializing using TypeNameHandling.None and serializing with TypeNameHandling.Objects?
There was a problem hiding this comment.
Do we already have a happy path test somewhere? Seems like the three new tests cover blocked types, blocked $type metadata, and DateParseHandling.
| using (var stringReader = new System.IO.StringReader(this.Result)) | ||
| using (var jsonReader = new JsonTextReader(stringReader) { DateParseHandling = serializer.DateParseHandling }) | ||
| { | ||
| e = (Exception)JToken.Load(jsonReader).ToObject(type, serializer); |
There was a problem hiding this comment.
Can we use serializer.Deserialize(jsonReader, type) here instead of JToken.Load().ToObject()?
| WasCreated = true; | ||
| } | ||
|
|
||
| public static bool WasCreated { get; set; } |
There was a problem hiding this comment.
WasCreated is a shared static field so if xUnit parallelism is ever enabled for this class (e.g., via [assembly: CollectionBehavior(MaxParallelThreads = N)]), concurrent tests will silently corrupt this assertion. Consider using an instance counter with Interlocked.Increment instead, or at minimum add a comment noting that these tests must run single-threaded.
|
Close as not needed anymore |
Validates that entity response exception types derive from Exception before deserialization and disables Json.NET type metadata handling. This prevents attacker-controlled types from being instantiated while preserving custom exception propagation. Adds regression tests for root and nested type metadata.