-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for a few known ysoserial.net exploits.
- Loading branch information
Showing
8 changed files
with
232 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Security; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace SafeDeserializationHelpers.Tests | ||
{ | ||
[TestClass] | ||
public class TestBaseTests : TestBase | ||
{ | ||
[TestMethod, ExpectedException(typeof(AssertFailedException))] | ||
public void AssertDoesntThrowFailsIfExceptionWasThrown() | ||
{ | ||
Assert_DoesNotThrow(() => { throw new SecurityException(); }); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AssertFailedException))] | ||
public void AssertThrowsFailsIfNoExceptionWasThrown() | ||
{ | ||
Assert_Throws<SecurityException>(() => { }); | ||
} | ||
|
||
[TestMethod, ExpectedException(typeof(AssertFailedException))] | ||
public void AssertThrowsFailsIfUnexpectedExceptionWasThrown() | ||
{ | ||
Assert_Throws<SecurityException>(() => { new ArgumentNullException(); }); | ||
} | ||
|
||
[TestMethod] | ||
public void AssertAreEqualWorksOnLotsOfDataTypes() | ||
{ | ||
Assert_DoesNotThrow(() => | ||
{ | ||
Assert_AreEqual(1, 1); | ||
Assert_AreEqual("Hello", "Hello"); | ||
Assert_AreEqual(new DataTable("Test"), new DataTable("Test")); | ||
Assert_AreEqual(new Func<string, string, Process>(Process.Start), new Func<string, string, Process>(Process.Start)); | ||
}); | ||
|
||
Assert_Throws<AssertFailedException>(() => | ||
{ | ||
Assert_AreEqual(1, 2); | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void RoundtripIsExpectedToSucceedOnPrimitives() | ||
{ | ||
Assert_DoesNotThrow(() => | ||
{ | ||
Roundtrip(1, false); | ||
Roundtrip(1, true); | ||
Roundtrip("Hello", false); | ||
Roundtrip("Hello", true); | ||
}); | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
namespace SafeDeserializationHelpers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Serialization; | ||
using System.Security.Permissions; | ||
using System.Text; | ||
|
||
/// <summary> | ||
/// Custom <see cref="DataSet"/> descendant controlling the deserialization. | ||
/// </summary> | ||
[Serializable] | ||
public sealed class CustomDataSetDeserializer : DataSet, IObjectReference | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CustomDataSetDeserializer"/> class. | ||
/// </summary> | ||
/// <param name="info">Serialization info.</param> | ||
/// <param name="context">Streaming context</param> | ||
private CustomDataSetDeserializer(SerializationInfo info, StreamingContext context) | ||
{ | ||
Info = info; | ||
Context = context; | ||
} | ||
|
||
private static ConstructorInfo Constructor { get; } = typeof(DataSet).GetConstructor( | ||
BindingFlags.Instance | BindingFlags.NonPublic, | ||
null, | ||
new[] { typeof(SerializationInfo), typeof(StreamingContext) }, | ||
null); | ||
|
||
private SerializationInfo Info { get; } | ||
|
||
private StreamingContext Context { get; } | ||
|
||
/// <inheritdoc cref="IObjectReference" /> | ||
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)] | ||
public object GetRealObject(StreamingContext context) | ||
{ | ||
Validate(Info); | ||
return Constructor.Invoke(new object[] { Info, context }); | ||
} | ||
|
||
private void Validate(SerializationInfo info) | ||
{ | ||
var remotingFormat = SerializationFormat.Xml; | ||
var schemaSerializationMode = SchemaSerializationMode.IncludeSchema; | ||
|
||
var e = info.GetEnumerator(); | ||
while (e.MoveNext()) | ||
{ | ||
switch (e.Name) | ||
{ | ||
case "DataSet.RemotingFormat": // DataSet.RemotingFormat does not exist in V1/V1.1 versions | ||
remotingFormat = (SerializationFormat)e.Value; | ||
break; | ||
|
||
case "SchemaSerializationMode.DataSet": // SchemaSerializationMode.DataSet does not exist in V1/V1.1 versions | ||
schemaSerializationMode = (SchemaSerializationMode)e.Value; | ||
break; | ||
} | ||
} | ||
|
||
if (remotingFormat == SerializationFormat.Xml) | ||
{ | ||
// XML dataset serialization isn't vulnerable | ||
return; | ||
} | ||
|
||
throw new UnsafeDeserializationException("Serialized DataSet probably includes malicious data."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters