Skip to content

Commit 9c5c8bc

Browse files
committed
Offer the possibility to specify a serialization binder for C# binary serialization utilities.
This allow to filter types and prevent security issues.
1 parent 5993fa5 commit 9c5c8bc

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

src/QuikGraph.Serialization/SerializationExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Xml;
33
#if SUPPORTS_GRAPHS_SERIALIZATION
44
using System.IO;
5+
using System.Runtime.Serialization;
56
using System.Runtime.Serialization.Formatters.Binary;
67
using System.Xml.XPath;
78
#endif
@@ -45,9 +46,19 @@ public static void SerializeToBinary<TVertex, TEdge>(
4546
/// <typeparam name="TEdge">Edge type.</typeparam>
4647
/// <typeparam name="TGraph">Graph type.</typeparam>
4748
/// <param name="stream">Stream from which deserializing the graph.</param>
49+
/// <param name="binder">
50+
/// <para>
51+
/// <see cref="SerializationBinder"/> used during deserialization.
52+
/// It can be used to check/filter/replace/upgrade types that are loaded.
53+
/// </para>
54+
/// <para>It is also useful in security scenarios.</para>
55+
/// <para>By default no binder is used.</para>
56+
/// </param>
4857
/// <returns>Deserialized graph.</returns>
4958
[Pure]
50-
public static TGraph DeserializeFromBinary<TVertex, TEdge, TGraph>([NotNull] this Stream stream)
59+
public static TGraph DeserializeFromBinary<TVertex, TEdge, TGraph>(
60+
[NotNull] this Stream stream,
61+
[CanBeNull] SerializationBinder binder = null)
5162
where TGraph : IGraph<TVertex, TEdge>
5263
where TEdge : IEdge<TVertex>
5364
{
@@ -56,7 +67,7 @@ public static TGraph DeserializeFromBinary<TVertex, TEdge, TGraph>([NotNull] thi
5667
if (!stream.CanRead)
5768
throw new ArgumentException("Must be a readable stream", nameof(stream));
5869

59-
var formatter = new BinaryFormatter();
70+
var formatter = new BinaryFormatter { Binder = binder };
6071
object result = formatter.Deserialize(stream);
6172
return (TGraph)result;
6273
}

0 commit comments

Comments
 (0)