Repository navigation
Prevent Unsafe Type Materialization when Deserializing Entity Response Exceptions #3517
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,8 +62,17 @@ public T GetResult<T>(MessagePayloadDataConverter messageDataConverter, MessageP | |
| // do a best-effort attempt at deserializing this exception | ||
| try | ||
| { | ||
| var type = Type.GetType(this.ExceptionType, true); | ||
| e = (Exception)errorDataConverter.Deserialize(this.Result, type); | ||
| Type type = Type.GetType(this.ExceptionType, true); | ||
| if (typeof(Exception).IsAssignableFrom(type)) | ||
| { | ||
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
| } | ||
| } | ||
| } | ||
| catch | ||
| { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| // Copyright (c) .NET Foundation. All rights reserved. | ||
| // Licensed under the MIT License. See LICENSE in the project root for license information. | ||
|
|
||
| using System; | ||
| using Newtonsoft.Json; | ||
| using Newtonsoft.Json.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Azure.WebJobs.Extensions.DurableTask.Tests | ||
| { | ||
| public class ResponseMessageTests | ||
| { | ||
| [Fact] | ||
| [Trait("Category", PlatformSpecificHelpers.TestCategory)] | ||
| public void GetResult_DoesNotDeserializeNonExceptionType() | ||
| { | ||
| var response = new ResponseMessage | ||
| { | ||
| ExceptionType = typeof(DeserializationCallback).AssemblyQualifiedName, | ||
| Result = "{}", | ||
| }; | ||
|
|
||
| DeserializationCallback.WasCreated = false; | ||
| MessagePayloadDataConverter dataConverter = CreateDataConverter(); | ||
|
|
||
| Assert.Throws<FunctionFailedException>(() => response.GetResult<object>(dataConverter, dataConverter)); | ||
| Assert.False(DeserializationCallback.WasCreated); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Category", PlatformSpecificHelpers.TestCategory)] | ||
| public void GetResult_IgnoresNestedTypeMetadataInException() | ||
| { | ||
| var result = new JObject | ||
| { | ||
| [nameof(ExceptionWithPayload.Payload)] = new JObject | ||
| { | ||
| ["$type"] = typeof(DeserializationCallback).AssemblyQualifiedName, | ||
| }, | ||
| }; | ||
| var response = new ResponseMessage | ||
| { | ||
| ExceptionType = typeof(ExceptionWithPayload).AssemblyQualifiedName, | ||
| Result = result.ToString(Formatting.None), | ||
| }; | ||
|
|
||
| DeserializationCallback.WasCreated = false; | ||
| MessagePayloadDataConverter dataConverter = CreateDataConverter(); | ||
|
|
||
| Assert.Throws<ExceptionWithPayload>(() => response.GetResult<object>(dataConverter, dataConverter)); | ||
| Assert.False(DeserializationCallback.WasCreated); | ||
| } | ||
|
|
||
| [Fact] | ||
| [Trait("Category", PlatformSpecificHelpers.TestCategory)] | ||
| public void GetResult_HonorsConfiguredDateParseHandling() | ||
| { | ||
| const string Timestamp = "2026-08-14T12:34:56Z"; | ||
| var response = new ResponseMessage | ||
| { | ||
| ExceptionType = typeof(ExceptionWithPayload).AssemblyQualifiedName, | ||
| Result = new JObject | ||
| { | ||
| [nameof(ExceptionWithPayload.Payload)] = Timestamp, | ||
| }.ToString(Formatting.None), | ||
| }; | ||
| MessagePayloadDataConverter dataConverter = CreateDataConverter(DateParseHandling.None); | ||
|
|
||
| ExceptionWithPayload exception = Assert.Throws<ExceptionWithPayload>( | ||
| () => response.GetResult<object>(dataConverter, dataConverter)); | ||
|
|
||
| Assert.Equal(Timestamp, Assert.IsType<string>(exception.Payload)); | ||
| } | ||
|
|
||
| private static MessagePayloadDataConverter CreateDataConverter( | ||
| DateParseHandling dateParseHandling = DateParseHandling.DateTime) | ||
| { | ||
| return new MessagePayloadDataConverter( | ||
| new JsonSerializerSettings | ||
| { | ||
| TypeNameHandling = TypeNameHandling.Objects, | ||
| DateParseHandling = dateParseHandling, | ||
| }, | ||
| isDefault: true); | ||
| } | ||
|
|
||
| public class DeserializationCallback | ||
| { | ||
| public DeserializationCallback() | ||
| { | ||
| WasCreated = true; | ||
| } | ||
|
|
||
| public static bool WasCreated { get; set; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| public class ExceptionWithPayload : Exception | ||
| { | ||
| public object Payload { get; set; } | ||
| } | ||
| } | ||
| } | ||
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.
Should we document that we are deserializing using
TypeNameHandling.Noneand serializing withTypeNameHandling.Objects?