-
Notifications
You must be signed in to change notification settings - Fork 327
Add a serialization binder for Service Fabric provider proxy #1363
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
Open
AnatoliB
wants to merge
4
commits into
main
Choose a base branch
from
anatolib/servicefabric-serialization-binder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
468b8a0
Add a serialization binder for Service Fabric provider
AnatoliB 81e1ca0
Enhance AllowedTypesSerializationBinder to restrict deserialization t…
AnatoliB 31b32b4
Address Copilot's comments
AnatoliB 350a44d
Add test for unresolvable type in AllowedTypesSerializationBinder and…
AnatoliB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
242 changes: 242 additions & 0 deletions
242
Test/DurableTask.AzureServiceFabric.Tests/AllowedTypesSerializationBinderTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| namespace DurableTask.AzureServiceFabric.Tests | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
|
AnatoliB marked this conversation as resolved.
|
||
| using System.Reflection; | ||
| using DurableTask.AzureServiceFabric.Models; | ||
| using DurableTask.AzureServiceFabric.Service; | ||
| using DurableTask.Core; | ||
| using DurableTask.Core.History; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Newtonsoft.Json; | ||
|
|
||
| [TestClass] | ||
| public class AllowedTypesSerializationBinderTests | ||
| { | ||
| readonly AllowedTypesSerializationBinder binder = new AllowedTypesSerializationBinder(); | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsDurableTaskCoreTypes() | ||
| { | ||
| var type = typeof(TaskMessage); | ||
| var result = this.binder.BindToType(type.Assembly.GetName().Name, type.FullName); | ||
| Assert.AreEqual(type, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsHistoryEventSubclasses() | ||
| { | ||
| var type = typeof(ExecutionStartedEvent); | ||
| var result = this.binder.BindToType(type.Assembly.GetName().Name, type.FullName); | ||
| Assert.AreEqual(type, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsServiceFabricProxyTypes() | ||
| { | ||
| var type = typeof(CreateTaskOrchestrationParameters); | ||
| var result = this.binder.BindToType(type.Assembly.GetName().Name, type.FullName); | ||
| Assert.AreEqual(type, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsMscorlibTypes() | ||
| { | ||
| var type = typeof(Dictionary<string, string>); | ||
| var result = this.binder.BindToType("mscorlib", type.FullName); | ||
| Assert.AreEqual(type, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsQualifiedAssemblyName() | ||
| { | ||
| var type = typeof(TaskMessage); | ||
| string qualifiedName = type.Assembly.FullName; // e.g. "DurableTask.Core, Version=..." | ||
| var result = this.binder.BindToType(qualifiedName, type.FullName); | ||
| Assert.AreEqual(type, result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsNullAssemblyName() | ||
| { | ||
| // Null/empty assembly name should pass through to the default binder | ||
| var result = this.binder.BindToType(null, typeof(string).FullName); | ||
| Assert.IsNotNull(result); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_RejectsArbitraryAssembly() | ||
| { | ||
| Assert.ThrowsException<InvalidOperationException>(() => | ||
| this.binder.BindToType("Evil.Assembly", "Evil.PwnedDescriptor")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_RejectsQualifiedArbitraryAssembly() | ||
| { | ||
| Assert.ThrowsException<InvalidOperationException>(() => | ||
| this.binder.BindToType("Evil.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Evil.PwnedDescriptor")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_RejectsSystemDiagnosticsProcess() | ||
| { | ||
| // A common gadget type — must be rejected | ||
| var type = typeof(System.Diagnostics.Process); | ||
| Assert.ThrowsException<InvalidOperationException>(() => | ||
| this.binder.BindToType(type.Assembly.GetName().Name, type.FullName)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_RejectsNonAllowlistedMscorlibType() | ||
| { | ||
| // System.Type is from mscorlib but not in the type allowlist | ||
| Assert.ThrowsException<InvalidOperationException>(() => | ||
| this.binder.BindToType("mscorlib", typeof(Type).FullName)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_RejectsUnresolvableType() | ||
| { | ||
| // A type name that cannot be resolved should throw a controlled exception, not NullReferenceException | ||
| var ex = Assert.ThrowsException<JsonSerializationException>(() => | ||
| this.binder.BindToType("DurableTask.Core", "DurableTask.Core.NonExistentType")); | ||
| StringAssert.Contains(ex.Message, "NonExistentType"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_RejectsNonAllowlistedDurableTaskCoreType() | ||
| { | ||
| // TaskOrchestration is a DurableTask.Core type but not in the proxy endpoint allowlist | ||
| var type = typeof(TaskOrchestration); | ||
| Assert.ThrowsException<InvalidOperationException>(() => | ||
| this.binder.BindToType(type.Assembly.GetName().Name, type.FullName)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToType_AllowsAllHistoryEventKnownTypes() | ||
| { | ||
| IEnumerable<Type> knownTypes; | ||
| try | ||
| { | ||
| knownTypes = HistoryEvent.KnownTypes(); | ||
| } | ||
| catch (ReflectionTypeLoadException ex) | ||
| { | ||
| // In test environments, not all types may be loadable | ||
| knownTypes = ex.Types.Where(t => t != null && !t.IsAbstract && typeof(HistoryEvent).IsAssignableFrom(t)); | ||
| } | ||
|
|
||
| foreach (Type knownType in knownTypes) | ||
| { | ||
| var result = this.binder.BindToType(knownType.Assembly.GetName().Name, knownType.FullName); | ||
| Assert.AreEqual(knownType, result, $"HistoryEvent subclass {knownType.Name} should be allowed"); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void BindToName_DelegatesToDefaultBinder() | ||
| { | ||
| this.binder.BindToName(typeof(TaskMessage), out string assemblyName, out string typeName); | ||
| // DefaultSerializationBinder delegates to the runtime; just verify it doesn't throw | ||
| // and returns consistent results for a known type. | ||
| this.binder.BindToName(typeof(TaskMessage), out string assemblyName2, out string typeName2); | ||
| Assert.AreEqual(assemblyName, assemblyName2); | ||
| Assert.AreEqual(typeName, typeName2); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void RoundTrip_TaskMessageWithHistoryEvent_Succeeds() | ||
| { | ||
| var message = new TaskMessage | ||
| { | ||
| SequenceNumber = 42, | ||
| OrchestrationInstance = new OrchestrationInstance { InstanceId = "test-1", ExecutionId = "exec-1" }, | ||
| Event = new ExecutionStartedEvent(-1, "input-data") | ||
| { | ||
| Tags = new Dictionary<string, string> { { "key", "value" } }, | ||
| Name = "TestOrchestration", | ||
| Version = "1.0" | ||
| } | ||
| }; | ||
|
|
||
| var settings = new JsonSerializerSettings | ||
| { | ||
| TypeNameHandling = TypeNameHandling.All, | ||
| SerializationBinder = this.binder | ||
| }; | ||
|
|
||
| string json = JsonConvert.SerializeObject(message, settings); | ||
| var deserialized = JsonConvert.DeserializeObject<TaskMessage>(json, settings); | ||
|
|
||
| Assert.IsNotNull(deserialized); | ||
| Assert.AreEqual(42, deserialized.SequenceNumber); | ||
| Assert.AreEqual("test-1", deserialized.OrchestrationInstance.InstanceId); | ||
| Assert.IsInstanceOfType(deserialized.Event, typeof(ExecutionStartedEvent)); | ||
|
|
||
| var startedEvent = (ExecutionStartedEvent)deserialized.Event; | ||
| Assert.AreEqual("TestOrchestration", startedEvent.Name); | ||
| Assert.AreEqual("value", startedEvent.Tags["key"]); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Deserialize_MaliciousPayload_IsRejected() | ||
| { | ||
| string maliciousJson = @"{ | ||
| ""$type"": ""System.Diagnostics.Process, System"", | ||
| ""StartInfo"": { ""FileName"": ""cmd.exe"" } | ||
| }"; | ||
|
|
||
| var settings = new JsonSerializerSettings | ||
| { | ||
| TypeNameHandling = TypeNameHandling.All, | ||
| SerializationBinder = this.binder | ||
| }; | ||
|
|
||
| // Newtonsoft wraps the binder's InvalidOperationException in a JsonSerializationException | ||
| var ex = Assert.ThrowsException<JsonSerializationException>(() => | ||
| JsonConvert.DeserializeObject<object>(maliciousJson, settings)); | ||
| Assert.IsInstanceOfType(ex.InnerException, typeof(InvalidOperationException)); | ||
| StringAssert.Contains(ex.InnerException.Message, "is not allowed"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Settings_DefaultBinderIsAllowedTypes() | ||
| { | ||
| var providerSettings = new FabricOrchestrationProviderSettings(); | ||
| Assert.IsNotNull(providerSettings.JsonSerializationBinder); | ||
| Assert.IsInstanceOfType(providerSettings.JsonSerializationBinder, typeof(AllowedTypesSerializationBinder)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Settings_BinderCanBeSetToNull() | ||
| { | ||
| var providerSettings = new FabricOrchestrationProviderSettings(); | ||
| providerSettings.JsonSerializationBinder = null; | ||
| Assert.IsNull(providerSettings.JsonSerializationBinder); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Settings_BinderCanBeOverridden() | ||
| { | ||
| var customBinder = new Newtonsoft.Json.Serialization.DefaultSerializationBinder(); | ||
| var providerSettings = new FabricOrchestrationProviderSettings(); | ||
| providerSettings.JsonSerializationBinder = customBinder; | ||
| Assert.AreSame(customBinder, providerSettings.JsonSerializationBinder); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.