-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Guard against (de)serializing pointers & ref structs #34777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d58cd9a
Guard against (de)serializing pointers & ref structs
layomia 1008989
Address review feedback
layomia 6f2863c
Update isbyreflike check
layomia 439941b
Merge remote-tracking branch 'upstream/master' into invalid_type
layomia d0b6b3f
Address review feedback; fetch IsByRefLike with reflection
layomia 4a71ddb
Address review feedback
layomia cf89d52
Remove extra whitespace
layomia 528ec1a
Add debug assert
layomia a471cb2
Add attribute-based fallback
layomia a91ebec
Ensure that property look-up with reflection is useful
layomia 7b6ab29
Remove IsByRefLike property reflection prob
layomia a078047
Move runtime type debug validation upstream
layomia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
167 changes: 167 additions & 0 deletions
167
src/libraries/System.Text.Json/tests/Serialization/InvalidTypeTests.cs
This file contains hidden or 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,167 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using Xunit; | ||
|
|
||
| namespace System.Text.Json.Serialization.Tests | ||
| { | ||
| public class InvalidTypeTests_Span : InvalidTypeTests | ||
| { | ||
| public InvalidTypeTests_Span() : base(SerializationWrapper.SpanSerializer) { } | ||
| } | ||
|
|
||
| public class InvalidTypeTests_String : InvalidTypeTests | ||
| { | ||
| public InvalidTypeTests_String() : base(SerializationWrapper.StringSerializer) { } | ||
| } | ||
|
|
||
| public class InvalidTypeTests_Stream : InvalidTypeTests | ||
| { | ||
| public InvalidTypeTests_Stream() : base(SerializationWrapper.StreamSerializer) { } | ||
| } | ||
|
|
||
| public class InvalidTypeTests_StreamWithSmallBuffer : InvalidTypeTests | ||
| { | ||
| public InvalidTypeTests_StreamWithSmallBuffer() : base(SerializationWrapper.StreamSerializerWithSmallBuffer) { } | ||
| } | ||
|
|
||
| public class InvalidTypeTests_Writer : InvalidTypeTests | ||
| { | ||
| public InvalidTypeTests_Writer() : base(SerializationWrapper.WriterSerializer) { } | ||
| } | ||
|
|
||
| public abstract class InvalidTypeTests | ||
| { | ||
| private SerializationWrapper Serializer { get; } | ||
|
|
||
| public InvalidTypeTests(SerializationWrapper serializer) | ||
| { | ||
| Serializer = serializer; | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(OpenGenericTypes))] | ||
| [MemberData(nameof(RefStructTypes))] | ||
| [MemberData(nameof(PointerTypes))] | ||
| public void DeserializeInvalidType(Type type) | ||
|
layomia marked this conversation as resolved.
|
||
| { | ||
| InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize("", type)); | ||
| Assert.Contains(type.ToString(), ex.ToString()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(TypesWithInvalidMembers_WithMembers))] | ||
| public void TypeWithInvalidMember(Type classType, Type invalidMemberType, string invalidMemberName) | ||
| { | ||
| static void ValidateException(InvalidOperationException ex, Type classType, Type invalidMemberType, string invalidMemberName) | ||
| { | ||
| string exAsStr = ex.ToString(); | ||
| Assert.Contains(invalidMemberType.ToString(), exAsStr); | ||
| Assert.Contains(invalidMemberName, exAsStr); | ||
| Assert.Contains(classType.ToString(), exAsStr); | ||
| } | ||
|
|
||
| object obj = Activator.CreateInstance(classType); | ||
| InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => Serializer.Serialize(obj, classType)); | ||
| ValidateException(ex, classType, invalidMemberType, invalidMemberName); | ||
|
|
||
| ex = Assert.Throws<InvalidOperationException>(() => Serializer.Serialize(null, classType)); | ||
| ValidateException(ex, classType, invalidMemberType, invalidMemberName); | ||
|
|
||
| ex = Assert.Throws<InvalidOperationException>(() => JsonSerializer.Deserialize("", classType)); | ||
| ValidateException(ex, classType, invalidMemberType, invalidMemberName); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(OpenGenericTypes_ToSerialize))] | ||
| public void SerializeOpenGeneric(Type type) | ||
| { | ||
| object obj; | ||
|
|
||
| if (type.GetGenericArguments().Length == 1) | ||
| { | ||
| obj = Activator.CreateInstance(type.MakeGenericType(typeof(int))); | ||
| } | ||
| else | ||
| { | ||
| obj = Activator.CreateInstance(type.MakeGenericType(typeof(string), typeof(int))); | ||
| } | ||
|
|
||
| Assert.Throws<ArgumentException>(() => Serializer.Serialize(obj, type)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(OpenGenericTypes))] | ||
| public void SerializeInvalidTypes_NullValue(Type type) | ||
| { | ||
| InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => Serializer.Serialize(null, type)); | ||
| Assert.Contains(type.ToString(), ex.ToString()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SerializeOpenGeneric_NullableOfT() | ||
| { | ||
| Type openNullableType = typeof(Nullable<>); | ||
| object obj = Activator.CreateInstance(openNullableType.MakeGenericType(typeof(int))); | ||
|
|
||
| InvalidOperationException ex = Assert.Throws<InvalidOperationException>(() => Serializer.Serialize(obj, openNullableType)); | ||
| Assert.Contains(openNullableType.ToString(), ex.ToString()); | ||
| } | ||
|
|
||
| private class Test<T> { } | ||
|
|
||
| private static IEnumerable<object[]> OpenGenericTypes() | ||
| { | ||
| yield return new object[] { typeof(Test<>) }; | ||
| yield return new object[] { typeof(Nullable<>) }; | ||
| yield return new object[] { typeof(IList<>) }; | ||
| yield return new object[] { typeof(List<>) }; | ||
| yield return new object[] { typeof(List<>).MakeGenericType(typeof(Test<>)) }; | ||
| yield return new object[] { typeof(Test<>).MakeGenericType(typeof(List<>)) }; | ||
| yield return new object[] { typeof(Dictionary<,>) }; | ||
| yield return new object[] { typeof(Dictionary<,>).MakeGenericType(typeof(string), typeof(Nullable<>)) }; | ||
| yield return new object[] { typeof(Dictionary<,>).MakeGenericType(typeof(Nullable<>), typeof(string)) }; | ||
| } | ||
|
|
||
| private static IEnumerable<object[]> OpenGenericTypes_ToSerialize() | ||
| { | ||
| yield return new object[] { typeof(Test<>) }; | ||
| yield return new object[] { typeof(List<>) }; | ||
| yield return new object[] { typeof(Dictionary<,>) }; | ||
| } | ||
|
|
||
| private static IEnumerable<object[]> RefStructTypes() | ||
| { | ||
| yield return new object[] { typeof(Span<int>) }; | ||
| yield return new object[] { typeof(Utf8JsonReader) }; | ||
| yield return new object[] { typeof(MyRefStruct) }; | ||
| } | ||
|
|
||
| private static readonly Type s_intPtrType = typeof(int*); // Unsafe code may not appear in iterators. | ||
|
|
||
| private static IEnumerable<object[]> PointerTypes() | ||
| { | ||
| yield return new object[] { s_intPtrType }; | ||
| } | ||
|
|
||
| // Instances of the types of the invalid members cannot be passed directly | ||
| // to the serializer on serialization due to type constraints, | ||
| // e.g. int* can't be boxed and passed to the non-generic overload, | ||
| // and typeof(int*) can't be a generic parameter to the generic overload. | ||
| private static IEnumerable<object[]> TypesWithInvalidMembers_WithMembers() | ||
| { | ||
| yield return new object[] { typeof(Memory<byte>), typeof(Span<byte>), "Span" }; // Contains Span<byte> property. | ||
|
|
||
| yield return new object[] { typeof(ClassWithIntPtr), s_intPtrType, "IntPtr" }; | ||
| } | ||
|
|
||
| private class ClassWithIntPtr | ||
| { | ||
| public unsafe int* IntPtr { get; } | ||
| } | ||
|
|
||
| private ref struct MyRefStruct { } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.