Skip to content

Commit 8944c41

Browse files
committed
AMQNET-844 Allow, deny types fix
1 parent c458814 commit 8944c41

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

src/Commands/TrustedClassFilter.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ public override Type BindToType(string assemblyName, string typeName)
3737
var name = new AssemblyName(assemblyName);
3838
var assembly = Assembly.Load(name);
3939
var type = FormatterServices.GetTypeFromAssembly(assembly, typeName);
40+
if (type == null)
41+
{
42+
throw new SerializationException($"Type {typeName} not found in assembly {assemblyName}");
43+
}
44+
4045
if (deserializationPolicy.IsTrustedType(destination, type))
4146
{
4247
return type;
4348
}
4449

45-
var message = $"Forbidden {type.FullName}! " +
50+
var message = $"Forbidden {type.FullName ?? typeName}! " +
4651
"This type is not trusted to be deserialized under the current configuration. " +
4752
"Please refer to the documentation for more information on how to configure trusted types.";
4853
throw new SerializationException(message);

src/NmsDefaultDeserializationPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public class NmsDefaultDeserializationPolicy : INmsDeserializationPolicy
4848

4949
public bool IsTrustedType(IDestination destination, Type type)
5050
{
51-
var typeName = type?.FullName;
51+
var typeName = type.FullName;
5252
if (typeName == null)
5353
{
54-
return true;
54+
return false;
5555
}
5656

5757
foreach (var denyListEntry in denyList)

test/MessageConsumerTest.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,70 @@ public void TestShouldUseCustomDeserializationPolicy()
370370
});
371371
}
372372

373+
// https://codewhitesec.blogspot.com/2022/06/bypassing-dotnet-serialization-binders.html
374+
[Test, Timeout(20_000)]
375+
public void TestShouldNotDeserializeMaliciousType()
376+
{
377+
string uri = "activemq:tcp://${{activemqhost}}:61616" + $"?nms.deserializationPolicy.allowList={typeof(TrustedType).FullName}";
378+
var factory = new ConnectionFactory(ReplaceEnvVar(uri));
379+
using var connection = factory.CreateConnection("", "");
380+
381+
connection.Start();
382+
var session = connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
383+
var queue = session.GetQueue(Guid.NewGuid().ToString());
384+
var consumer = session.CreateConsumer(queue);
385+
var producer = session.CreateProducer(queue);
386+
387+
var message = producer.CreateObjectMessage(new MaliciousSerializable());
388+
producer.Send(message);
389+
390+
var receivedMessage = consumer.Receive();
391+
var objectMessage = receivedMessage as IObjectMessage;
392+
Assert.NotNull(objectMessage);
393+
Assert.Throws<SerializationException>(() =>
394+
{
395+
_ = objectMessage.Body;
396+
});
397+
}
398+
373399
[Serializable]
374400
public class UntrustedType
375401
{
376402
public string Prop1 { get; set; }
377403
}
404+
405+
[Serializable]
406+
public class TrustedType
407+
{
408+
// ReSharper disable once UnusedMember.Global
409+
public string Prop1 { get; set; }
410+
}
411+
412+
[Serializable]
413+
public class MaliciousSerializable : ISerializable
414+
{
415+
private readonly string _payloadData = "Injected Payload";
416+
417+
public MaliciousSerializable() { }
418+
419+
protected MaliciousSerializable(SerializationInfo info, StreamingContext context)
420+
{
421+
_payloadData = info.GetString("InjectedValue");
422+
}
423+
424+
public void GetObjectData(SerializationInfo info, StreamingContext context)
425+
{
426+
Type type = typeof(TrustedType);
427+
428+
// Manipulate serialization info to trick deserialization
429+
info.SetType(type);
430+
info.FullTypeName = type.AssemblyQualifiedName; // This should result in null
431+
info.AssemblyName = "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
432+
433+
// Inject a fake property
434+
info.AddValue("InjectedValue", _payloadData);
435+
}
436+
}
378437

379438
private class CustomDeserializationPolicy : INmsDeserializationPolicy
380439
{

0 commit comments

Comments
 (0)