Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/EPiServer.Amazon/Events/EventMessageSerializationBinder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using EPiServer.Events;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;

namespace EPiServer.Amazon.Events
{
/// <summary>
/// Restricts <see cref="TypeNameHandling"/> deserialization of <see cref="EventMessage"/> payloads to a
/// known safe set of types, to prevent arbitrary type instantiation (CWE-502) from untrusted SNS/SQS messages.
/// </summary>
internal class EventMessageSerializationBinder : DefaultSerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
var type = base.BindToType(assemblyName, typeName);

if (!IsAllowed(type))
{
throw new JsonSerializationException($"Deserialization of type '{type}' is not allowed.");
}

return type;
}

private static bool IsAllowed(Type type)
{
if (type == typeof(BoxedValue<Guid>))
{
return true;
}

// Only types owned by EPiServer/Optimizely assemblies are trusted for polymorphic deserialization.
var assemblyName = type.Assembly.GetName().Name ?? string.Empty;
return assemblyName.Equals("EPiServer.Events", StringComparison.Ordinal)
|| assemblyName.StartsWith("EPiServer.Events.", StringComparison.Ordinal);
}
}
}
3 changes: 2 additions & 1 deletion src/EPiServer.Amazon/Events/MessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class MessageSerializer
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new MessageContractResolver()
ContractResolver = new MessageContractResolver(),
SerializationBinder = new EventMessageSerializationBinder()
};

/// <summary>
Expand Down
Loading