|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// Copyright Microsoft Corporation |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | +// ---------------------------------------------------------------------------------- |
| 13 | + |
| 14 | +namespace DurableTask.ServiceBus.Tests |
| 15 | +{ |
| 16 | + using System; |
| 17 | + using System.Collections.Generic; |
| 18 | + using DurableTask.Core; |
| 19 | + using DurableTask.Core.History; |
| 20 | + using DurableTask.ServiceBus.Tracking; |
| 21 | + using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 22 | + using Newtonsoft.Json; |
| 23 | + |
| 24 | + [TestClass] |
| 25 | + public class HistoryEventSerializationBinderTests |
| 26 | + { |
| 27 | + // Mirrors the production read settings on AzureTableOrchestrationHistoryEventEntity. |
| 28 | + static readonly JsonSerializerSettings ReadJsonSettings = new JsonSerializerSettings |
| 29 | + { |
| 30 | + TypeNameHandling = TypeNameHandling.Objects, |
| 31 | + SerializationBinder = new HistoryEventSerializationBinder() |
| 32 | + }; |
| 33 | + |
| 34 | + // Settings used to *produce* the JSON exactly as the production code does. |
| 35 | + static readonly JsonSerializerSettings WriteJsonSettings = new JsonSerializerSettings |
| 36 | + { |
| 37 | + TypeNameHandling = TypeNameHandling.Objects |
| 38 | + }; |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void RoundTripsExecutionStartedEventWithTags() |
| 42 | + { |
| 43 | + var original = new ExecutionStartedEvent(eventId: -1, input: "input") |
| 44 | + { |
| 45 | + OrchestrationInstance = new OrchestrationInstance |
| 46 | + { |
| 47 | + InstanceId = "instance-1", |
| 48 | + ExecutionId = "execution-1", |
| 49 | + }, |
| 50 | + Name = "OrchestrationName", |
| 51 | + Version = "1.0", |
| 52 | + Tags = new Dictionary<string, string> { ["tag1"] = "value1" }, |
| 53 | + }; |
| 54 | + |
| 55 | + string json = JsonConvert.SerializeObject(original, WriteJsonSettings); |
| 56 | + var deserialized = JsonConvert.DeserializeObject<HistoryEvent>(json, ReadJsonSettings); |
| 57 | + |
| 58 | + Assert.IsInstanceOfType(deserialized, typeof(ExecutionStartedEvent)); |
| 59 | + var deserializedStarted = (ExecutionStartedEvent)deserialized; |
| 60 | + Assert.AreEqual("OrchestrationName", deserializedStarted.Name); |
| 61 | + Assert.AreEqual("input", deserializedStarted.Input); |
| 62 | + Assert.AreEqual("value1", deserializedStarted.Tags["tag1"]); |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public void AllowsAllConcreteHistoryEventTypes() |
| 67 | + { |
| 68 | + // Sanity check that every concrete HistoryEvent subclass declared in DurableTask.Core |
| 69 | + // is accepted by the binder's BindToType. |
| 70 | + var binder = new HistoryEventSerializationBinder(); |
| 71 | + foreach (Type concreteType in HistoryEvent.KnownTypes()) |
| 72 | + { |
| 73 | + Type bound = binder.BindToType(concreteType.Assembly.GetName().Name, concreteType.FullName); |
| 74 | + Assert.AreEqual(concreteType, bound); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + [TestMethod] |
| 79 | + public void RejectsNonAllowlistedRootType() |
| 80 | + { |
| 81 | + // System.IO.FileInfo is a classic gadget-chain probe. The binder must reject it |
| 82 | + // even though the BCL would happily resolve it via Type.GetType. |
| 83 | + string json = "{\"$type\":\"System.IO.FileInfo, System.Private.CoreLib\",\"OriginalPath\":\"c:\\\\evil\"}"; |
| 84 | + Assert.ThrowsException<JsonSerializationException>( |
| 85 | + () => JsonConvert.DeserializeObject<HistoryEvent>(json, ReadJsonSettings)); |
| 86 | + } |
| 87 | + |
| 88 | + [TestMethod] |
| 89 | + public void RejectsNonAllowlistedNestedType() |
| 90 | + { |
| 91 | + // Embed a malicious $type inside an otherwise valid ExecutionStartedEvent's Tags |
| 92 | + // member. The Tags member is IDictionary<string,string>, so the $type token is |
| 93 | + // honored by Newtonsoft.Json when reading. Anything other than |
| 94 | + // Dictionary<string,string> must be rejected. |
| 95 | + string json = "{\"$type\":\"DurableTask.Core.History.ExecutionStartedEvent, DurableTask.Core\"," |
| 96 | + + "\"Tags\":{\"$type\":\"System.Collections.Generic.SortedDictionary`2[[System.String, System.Private.CoreLib],[System.String, System.Private.CoreLib]], System.Collections\"}}"; |
| 97 | + Assert.ThrowsException<JsonSerializationException>( |
| 98 | + () => JsonConvert.DeserializeObject<HistoryEvent>(json, ReadJsonSettings)); |
| 99 | + } |
| 100 | + |
| 101 | + [TestMethod] |
| 102 | + public void AllowsDictionaryStringStringForTags() |
| 103 | + { |
| 104 | + // The exact concrete type Newtonsoft.Json emits for IDictionary<string,string> Tags. |
| 105 | + string json = "{\"$type\":\"DurableTask.Core.History.ExecutionStartedEvent, DurableTask.Core\"," |
| 106 | + + "\"Tags\":{\"$type\":\"System.Collections.Generic.Dictionary`2[[System.String, " + typeof(string).Assembly.GetName().Name + "],[System.String, " + typeof(string).Assembly.GetName().Name + "]], " + typeof(Dictionary<string, string>).Assembly.GetName().Name + "\"," |
| 107 | + + "\"tag1\":\"v1\"}}"; |
| 108 | + |
| 109 | + var deserialized = (ExecutionStartedEvent)JsonConvert.DeserializeObject<HistoryEvent>(json, ReadJsonSettings); |
| 110 | + Assert.AreEqual("v1", deserialized.Tags["tag1"]); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments