Skip to content

Prevent Unsafe Type Materialization when Deserializing Entity Response Exceptions - #3517

Closed
Naiyuan Tian (nytian) wants to merge 4 commits into
devfrom
nytian/excep-security
Closed

Naiyuan Tian (nytian) wants to merge 4 commits into
devfrom
nytian/excep-security

Conversation

@nytian

Copy link
Copy Markdown
Contributor

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.

Copilot AI lite review requested due to automatic review settings August 14, 2026 15:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ExceptionType resolves to an Exception-derived type before materializing.
  • Disable Json.NET $type handling when deserializing exception payloads to avoid nested type metadata instantiation.
  • Add unit tests validating non-Exception types are not deserialized and nested $type metadata 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.

Copilot AI review requested due to automatic review settings August 14, 2026 17:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
                        }

Copilot AI review requested due to automatic review settings August 19, 2026 17:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,

Copilot AI review requested due to automatic review settings August 20, 2026 16:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we document that we are deserializing using TypeNameHandling.None and serializing with TypeNameHandling.Objects?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use serializer.Deserialize(jsonReader, type) here instead of JToken.Load().ToObject()?

WasCreated = true;
}

public static bool WasCreated { get; set; }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@nytian

Copy link
Copy Markdown
Contributor Author

Close as not needed anymore

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants