Adds DerivedParameterKnownTypesBinder so that we're following better practice in our tests

This commit is contained in:
Nick Darvey 2019-06-26 10:35:03 +10:00
parent 47bb845d48
commit 3fc4bee7e2
2 changed files with 23 additions and 5 deletions

View File

@ -3,12 +3,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Channels; using System.Threading.Channels;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Serialization;
namespace Microsoft.AspNetCore.SignalR.Tests namespace Microsoft.AspNetCore.SignalR.Tests
{ {
@ -789,6 +791,25 @@ namespace Microsoft.AspNetCore.SignalR.Tests
} }
public class DerivedParameterTestObject : DerivedParameterTestObjectBase { } public class DerivedParameterTestObject : DerivedParameterTestObjectBase { }
public class DerivedParameterKnownTypesBinder : ISerializationBinder
{
private static readonly IEnumerable<Type> _knownTypes = new List<Type>()
{
typeof(DerivedParameterTestObject)
};
public static ISerializationBinder Instance { get; } = new DerivedParameterKnownTypesBinder();
public void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
assemblyName = null;
typeName = serializedType.Name;
}
public Type BindToType(string assemblyName, string typeName) =>
_knownTypes.Single(type => type.Name == typeName);
}
} }
public class SimpleHub : Hub public class SimpleHub : Hub

View File

@ -3595,11 +3595,8 @@ namespace Microsoft.AspNetCore.SignalR.Tests
{ {
PayloadSerializerSettings = new JsonSerializerSettings() PayloadSerializerSettings = new JsonSerializerSettings()
{ {
// The usage of TypeNameHandling.All is a security risk. TypeNameHandling = TypeNameHandling.All,
// If you're implementing this in your own application instead use your own 'type' field and a custom JsonConverter SerializationBinder = StreamingHub.DerivedParameterKnownTypesBinder.Instance
// or ensure you're restricting to only known types with a custom SerializationBinder.
// See https://github.com/aspnet/AspNetCore/issues/11495#issuecomment-505047422
TypeNameHandling = TypeNameHandling.All
} }
}; };
var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider( var serviceProvider = HubConnectionHandlerTestUtils.CreateServiceProvider(