Skip to content

Commit 9f23e04

Browse files
authored
Add specs to test disallow-unsafe-type (#5746)
* Add spec to test disallow-unsafe-type * Fix Hyperion disallow-unsafe-type spec
1 parent a42fa87 commit 9f23e04

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

src/contrib/serializers/Akka.Serialization.Hyperion.Tests/HyperionConfigTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.IO;
1011
using System.Linq;
1112
using System.Runtime.Serialization;
13+
using System.Threading.Tasks;
1214
using Akka.Actor;
1315
using Akka.Configuration;
1416
using FluentAssertions;
@@ -246,6 +248,71 @@ public void Hyperion_serializer_should_allow_to_setup_surrogates()
246248
FooHyperionSurrogate.Surrogated[0].Should().BeEquivalentTo(expected);
247249
}
248250
}
251+
252+
[Fact]
253+
public async Task CanDeserializeANaughtyTypeWhenAllowed()
254+
{
255+
var config = ConfigurationFactory.ParseString(@"
256+
akka {
257+
serialize-messages = on
258+
actor {
259+
serializers {
260+
hyperion = ""Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion""
261+
}
262+
serialization-bindings {
263+
""System.Object"" = hyperion
264+
}
265+
serialization-settings.hyperion.disallow-unsafe-type = false
266+
}
267+
}");
268+
var system = ActorSystem.Create("unsafeSystem", config);
269+
270+
try
271+
{
272+
var serializer = system.Serialization.FindSerializerForType(typeof(DirectoryInfo));
273+
var di = new DirectoryInfo(@"c:\");
274+
275+
var serialized = serializer.ToBinary(di);
276+
var deserialized = serializer.FromBinary<DirectoryInfo>(serialized);
277+
}
278+
finally
279+
{
280+
await system.Terminate();
281+
}
282+
}
283+
284+
[Fact]
285+
public async Task CantDeserializeANaughtyTypeByDefault()
286+
{
287+
var config = ConfigurationFactory.ParseString(@"
288+
akka {
289+
serialize-messages = on
290+
actor {
291+
serializers {
292+
hyperion = ""Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion""
293+
}
294+
serialization-bindings {
295+
""System.Object"" = hyperion
296+
}
297+
}
298+
}");
299+
var system = ActorSystem.Create("unsafeSystem", config);
300+
301+
try
302+
{
303+
var serializer = system.Serialization.FindSerializerForType(typeof(DirectoryInfo));
304+
var di = new DirectoryInfo(@"c:\");
305+
306+
var serialized = serializer.ToBinary(di);
307+
var ex = Assert.Throws<SerializationException>(() => serializer.FromBinary<DirectoryInfo>(serialized));
308+
ex.InnerException.Should().BeOfType<EvilDeserializationException>();
309+
}
310+
finally
311+
{
312+
await system.Terminate();
313+
}
314+
}
315+
249316

250317
public static IEnumerable<object[]> TypeFilterObjectFactory()
251318
{

src/contrib/serializers/Akka.Serialization.Hyperion.Tests/HyperionSerializerSetupSpec.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,12 @@ private static Config Config
3535
}
3636
");
3737

38+
private readonly ITestOutputHelper _output;
39+
3840
public HyperionSerializerSetupSpec(ITestOutputHelper output) : base (Config, output)
39-
{ }
41+
{
42+
_output = output;
43+
}
4044

4145
[Fact]
4246
public void Setup_should_be_converted_to_settings_correctly()
@@ -128,14 +132,22 @@ public void Setup_surrogate_should_work()
128132

129133
[Theory]
130134
[MemberData(nameof(DangerousObjectFactory))]
131-
public void Setup_disallow_unsafe_type_should_work(object dangerousObject, Type type)
135+
public void Setup_disallow_unsafe_type_should_work_by_default(byte[] dangerousObject, Type type)
132136
{
137+
_output.WriteLine($"Dangerous type: [{type}]");
133138
var deserializer = new HyperionSerializer((ExtendedActorSystem)Sys, HyperionSerializerSettings.Default);
134-
var serializer = new HyperionSerializer((ExtendedActorSystem)Sys, deserializer.Settings.WithDisallowUnsafeType(false));
135-
var serialized = serializer.ToBinary(dangerousObject);
136-
deserializer.Invoking(s => s.FromBinary(serialized, type)).Should().Throw<SerializationException>();
139+
deserializer.Invoking(s => s.FromBinary(dangerousObject, type)).Should().Throw<SerializationException>();
137140
}
138141

142+
[Theory]
143+
[MemberData(nameof(DangerousObjectFactory))]
144+
public void Setup_should_deserialize_unsafe_type_if_allowed(byte[] dangerousObject, Type type)
145+
{
146+
_output.WriteLine($"Dangerous type: [{type}]");
147+
var deserializer = new HyperionSerializer((ExtendedActorSystem)Sys, HyperionSerializerSettings.Default.WithDisallowUnsafeType(false));
148+
deserializer.FromBinary(dangerousObject, type); // should not throw
149+
}
150+
139151
[Theory]
140152
[MemberData(nameof(TypeFilterObjectFactory))]
141153
public void Setup_TypeFilter_should_filter_types_properly(object sampleObject, bool shouldSucceed)
@@ -170,17 +182,23 @@ public static IEnumerable<object[]> DangerousObjectFactory()
170182
{
171183
var isWindow = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
172184

173-
yield return new object[]{ new FileInfo("C:\\Windows\\System32"), typeof(FileInfo) };
174-
yield return new object[]{ new ClaimsIdentity(), typeof(ClaimsIdentity)};
185+
yield return new object[]{ Serialize(new FileInfo("C:\\Windows\\System32")), typeof(FileInfo) };
186+
yield return new object[]{ Serialize(new ClaimsIdentity()), typeof(ClaimsIdentity)};
175187
if (isWindow)
176188
{
177-
yield return new object[]{ WindowsIdentity.GetAnonymous(), typeof(WindowsIdentity) };
178-
yield return new object[]{ new WindowsPrincipal(WindowsIdentity.GetAnonymous()), typeof(WindowsPrincipal)};
189+
yield return new object[]{ Serialize(WindowsIdentity.GetAnonymous()), typeof(WindowsIdentity) };
190+
yield return new object[]{ Serialize(new WindowsPrincipal(WindowsIdentity.GetAnonymous())), typeof(WindowsPrincipal)};
179191
}
180192
#if NET471
181-
yield return new object[]{ new Process(), typeof(Process)};
193+
yield return new object[]{ Serialize(new Process()), typeof(Process)};
182194
#endif
183-
yield return new object[]{ new ClaimsIdentity(), typeof(ClaimsIdentity)};
195+
yield return new object[]{ Serialize(new ClaimsIdentity()), typeof(ClaimsIdentity)};
196+
}
197+
198+
private static byte[] Serialize(object obj)
199+
{
200+
var serializer = new HyperionSerializer(null, HyperionSerializerSettings.Default.WithDisallowUnsafeType(false));
201+
return serializer.ToBinary(obj);
184202
}
185203

186204
public static IEnumerable<object[]> TypeFilterObjectFactory()

0 commit comments

Comments
 (0)