@@ -46,6 +46,31 @@ internal static class TypeEx
46
46
public static readonly Type TypeType = typeof ( Type ) ;
47
47
public static readonly Type RuntimeType = Type . GetType ( "System.RuntimeType" ) ;
48
48
49
+ private static readonly ReadOnlyCollection < string > UnsafeTypesDenySet =
50
+ new ReadOnlyCollection < string > ( new [ ]
51
+ {
52
+ "System.Security.Claims.ClaimsIdentity" ,
53
+ "System.Windows.Forms.AxHost.State" ,
54
+ "System.Windows.Data.ObjectDataProvider" ,
55
+ "System.Management.Automation.PSObject" ,
56
+ "System.Web.Security.RolePrincipal" ,
57
+ "System.IdentityModel.Tokens.SessionSecurityToken" ,
58
+ "SessionViewStateHistoryItem" ,
59
+ "TextFormattingRunProperties" ,
60
+ "ToolboxItemContainer" ,
61
+ "System.Security.Principal.WindowsClaimsIdentity" ,
62
+ "System.Security.Principal.WindowsIdentity" ,
63
+ "System.Security.Principal.WindowsPrincipal" ,
64
+ "System.CodeDom.Compiler.TempFileCollection" ,
65
+ "System.IO.FileSystemInfo" ,
66
+ "System.Activities.Presentation.WorkflowDesigner" ,
67
+ "System.Windows.ResourceDictionary" ,
68
+ "System.Windows.Forms.BindingSource" ,
69
+ "Microsoft.Exchange.Management.SystemManager.WinForms.ExchangeSettingsProvider" ,
70
+ "System.Diagnostics.Process" ,
71
+ "System.Management.IWbemClassObjectFreeThreaded"
72
+ } ) ;
73
+
49
74
public static bool IsHyperionPrimitive ( this Type type )
50
75
{
51
76
return type == Int32Type ||
@@ -69,8 +94,15 @@ public static bool IsHyperionPrimitive(this Type type)
69
94
}
70
95
71
96
#if NETSTANDARD16
72
- //HACK: the GetUnitializedObject actually exists in .NET Core, its just not public
73
- private static readonly Func < Type , object > getUninitializedObjectDelegate = ( Func < Type , object > )
97
+ //HACK: IsValueType does not exist for netstandard1.6
98
+ private static bool IsValueType ( this Type type )
99
+ => type . IsSubclassOf ( typeof ( ValueType ) ) ;
100
+
101
+ private static bool IsSubclassOf ( this Type p , Type c )
102
+ => c . IsAssignableFrom ( p ) ;
103
+
104
+ //HACK: the GetUnitializedObject actually exists in .NET Core, its just not public
105
+ private static readonly Func < Type , object > GetUninitializedObjectDelegate = ( Func < Type , object > )
74
106
typeof ( string )
75
107
. GetTypeInfo ( )
76
108
. Assembly
@@ -81,7 +113,7 @@ public static bool IsHyperionPrimitive(this Type type)
81
113
82
114
public static object GetEmptyObject ( this Type type )
83
115
{
84
- return getUninitializedObjectDelegate ( type ) ;
116
+ return GetUninitializedObjectDelegate ( type ) ;
85
117
}
86
118
#else
87
119
public static object GetEmptyObject ( this Type type ) => System . Runtime . Serialization . FormatterServices . GetUninitializedObject ( type ) ;
@@ -130,57 +162,39 @@ private static Type GetTypeFromManifestName(Stream stream, DeserializerSession s
130
162
break ;
131
163
}
132
164
133
- return LoadTypeByName ( shortName ) ;
165
+ return LoadTypeByName ( shortName , session . Serializer . Options . DisallowUnsafeTypes ) ;
134
166
} ) ;
135
167
}
136
168
137
- public static bool disallowUnsafeTypes = true ;
138
-
139
- private static ReadOnlyCollection < string > unsafeTypesDenySet =
140
- new ReadOnlyCollection < string > ( new [ ]
141
- {
142
- "System.Security.Claims.ClaimsIdentity" ,
143
- "System.Windows.Forms.AxHost.State" ,
144
- "System.Windows.Data.ObjectDataProvider" ,
145
- "System.Management.Automation.PSObject" ,
146
- "System.Web.Security.RolePrincipal" ,
147
- "System.IdentityModel.Tokens.SessionSecurityToken" ,
148
- "SessionViewStateHistoryItem" ,
149
- "TextFormattingRunProperties" ,
150
- "ToolboxItemContainer" ,
151
- "System.Security.Principal.WindowsClaimsIdentity" ,
152
- "System.Security.Principal.WindowsIdentity" ,
153
- "System.Security.Principal.WindowsPrincipal" ,
154
- "System.CodeDom.Compiler.TempFileCollection" ,
155
- "System.IO.FileSystemInfo" ,
156
- "System.Activities.Presentation.WorkflowDesigner" ,
157
- "System.Windows.ResourceDictionary" ,
158
- "System.Windows.Forms.BindingSource" ,
159
- "Microsoft.Exchange.Management.SystemManager.WinForms.ExchangeSettingsProvider" ,
160
- "System.Diagnostics.Process" ,
161
- "System.Management.IWbemClassObjectFreeThreaded"
162
- } ) ;
163
-
164
- #if ! NETSTANDARD1_6
165
- public static bool UnsafeInheritanceCheck ( Type type )
169
+ private static bool UnsafeInheritanceCheck ( Type type )
166
170
{
171
+ #if NETSTANDARD1_6
172
+ if ( type . IsValueType ( ) )
173
+ return false ;
174
+ var currentBase = type . DeclaringType ;
175
+ #else
167
176
if ( type . IsValueType )
168
177
return false ;
169
178
var currentBase = type . BaseType ;
179
+ #endif
180
+
170
181
while ( currentBase != null )
171
182
{
172
- if ( unsafeTypesDenySet . Any ( r => currentBase . FullName ? . Contains ( r ) ?? false ) )
183
+ if ( UnsafeTypesDenySet . Any ( r => currentBase . FullName ? . Contains ( r ) ?? false ) )
173
184
return true ;
185
+ #if NETSTANDARD1_6
186
+ currentBase = currentBase . DeclaringType ;
187
+ #else
174
188
currentBase = currentBase . BaseType ;
189
+ #endif
175
190
}
176
191
177
192
return false ;
178
193
}
179
- #endif
180
194
181
- public static Type LoadTypeByName ( string name )
195
+ public static Type LoadTypeByName ( string name , bool disallowUnsafeTypes )
182
196
{
183
- if ( disallowUnsafeTypes && unsafeTypesDenySet . Any ( r => name . Contains ( r ) ) )
197
+ if ( disallowUnsafeTypes && UnsafeTypesDenySet . Any ( name . Contains ) )
184
198
{
185
199
throw new EvilDeserializationException (
186
200
"Unsafe Type Deserialization Detected!" , name ) ;
@@ -191,24 +205,18 @@ public static Type LoadTypeByName(string name)
191
205
// i.e. if there are different version available in GAC and locally
192
206
var typename = ToQualifiedAssemblyName ( name , ignoreAssemblyVersion : false ) ;
193
207
var type = Type . GetType ( typename , true ) ;
194
- #if NETSTANDARD1_6
195
- #else
196
208
if ( UnsafeInheritanceCheck ( type ) )
197
209
throw new EvilDeserializationException (
198
210
"Unsafe Type Deserialization Detected!" , name ) ;
199
- #endif
200
211
return type ;
201
212
}
202
213
catch ( FileLoadException )
203
214
{
204
215
var typename = ToQualifiedAssemblyName ( name , ignoreAssemblyVersion : true ) ;
205
216
var type = Type . GetType ( typename , true ) ;
206
- #if NETSTANDARD1_6
207
- #else
208
217
if ( UnsafeInheritanceCheck ( type ) )
209
218
throw new EvilDeserializationException (
210
219
"Unsafe Type Deserialization Detected!" , name ) ;
211
- #endif
212
220
return type ;
213
221
}
214
222
}
@@ -398,6 +406,11 @@ public class T
398
406
/// </summary>
399
407
private static bool IsSimilarType ( this Type thisType , Type type )
400
408
{
409
+ if ( thisType == null && type == null )
410
+ return true ;
411
+ if ( thisType == null || type == null )
412
+ return false ;
413
+
401
414
// Ignore any 'ref' types
402
415
if ( thisType . IsByRef )
403
416
thisType = thisType . GetElementType ( ) ;
0 commit comments