|
38 | 38 | * MS 06-09-26 improved performance removing three-times cast |
39 | 39 | * MS 21-10-27 added allowed customized types for JSON deserialization |
40 | 40 | * MS 21-11-22 changed error message when type is not allowed |
41 | | - * |
| 41 | + * MS 21-11-29 added check for custom type deserialization |
42 | 42 | * |
43 | 43 | */ |
44 | 44 | using System; |
45 | 45 | using System.Text; |
46 | 46 | using System.Reflection; |
47 | 47 | using System.Collections; |
| 48 | +using System.Security; |
48 | 49 |
|
49 | 50 | namespace AjaxPro |
50 | 51 | { |
@@ -146,27 +147,7 @@ public static object Deserialize(IJavaScriptObject o, Type type) |
146 | 147 | if (type == null || type.IsAssignableFrom(t)) |
147 | 148 | { |
148 | 149 | type = t; |
149 | | - |
150 | | - if (AjaxPro.Utility.Settings.IsCustomTypesDeserializationDisabled) |
151 | | - { |
152 | | - bool isCustomTypeAllowed = false; |
153 | | - |
154 | | - foreach (var s in AjaxPro.Utility.Settings.JsonDeserializationCustomTypesAllowed) |
155 | | - if ((s.EndsWith("*") && type.FullName.StartsWith(s.Substring(0, s.Length - 1), StringComparison.InvariantCultureIgnoreCase)) || s == type.FullName) |
156 | | - { |
157 | | - isCustomTypeAllowed = true; |
158 | | - break; |
159 | | - } |
160 | | - |
161 | | - if (!isCustomTypeAllowed) |
162 | | - throw new System.Security.SecurityException("This type is not allowed as argument for this method."); |
163 | | - } |
164 | | - else |
165 | | - { |
166 | | - foreach (var s in AjaxPro.Utility.Settings.JsonDeserializationCustomTypesDenied) |
167 | | - if ((s.EndsWith("*") && type.FullName.StartsWith(s.Substring(0, s.Length -1), StringComparison.InvariantCultureIgnoreCase)) || s == type.FullName) |
168 | | - throw new System.Security.SecurityException("This type is not allowed as argument for this method."); |
169 | | - } |
| 150 | + ThrowExceptionIfNotCustomTypeDeserializationAllowed(type); |
170 | 151 | } |
171 | 152 | } |
172 | 153 |
|
@@ -228,6 +209,51 @@ public static object Deserialize(IJavaScriptObject o, Type type) |
228 | 209 |
|
229 | 210 | #region Internal Methods |
230 | 211 |
|
| 212 | + internal static void ThrowExceptionIfNotCustomTypeDeserializationAllowed(Type type) |
| 213 | + { |
| 214 | + SecurityException ex = null; |
| 215 | + if (!IsCustomTypeDeserializationAllowed(type, out ex) && ex != null) |
| 216 | + throw ex; |
| 217 | + } |
| 218 | + |
| 219 | + internal static bool IsCustomTypeDeserializationAllowed(Type type, out SecurityException ex) |
| 220 | + { |
| 221 | + ex = null; |
| 222 | + |
| 223 | + // allow all primitive and basic types |
| 224 | + if (type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) || type == typeof(TimeSpan) || type == typeof(decimal)) |
| 225 | + return true; |
| 226 | + |
| 227 | + if (AjaxPro.Utility.Settings.IsCustomTypesDeserializationDisabled) |
| 228 | + { |
| 229 | + bool isCustomTypeAllowed = false; |
| 230 | + |
| 231 | + foreach (var s in AjaxPro.Utility.Settings.JsonDeserializationCustomTypesAllowed) |
| 232 | + if ((s.EndsWith("*") && type.FullName.StartsWith(s.Substring(0, s.Length - 1), StringComparison.InvariantCultureIgnoreCase)) || s == type.FullName) |
| 233 | + { |
| 234 | + isCustomTypeAllowed = true; |
| 235 | + break; |
| 236 | + } |
| 237 | + |
| 238 | + if (!isCustomTypeAllowed) |
| 239 | + { |
| 240 | + ex = new SecurityException("This type is not allowed as argument for this method."); |
| 241 | + return false; |
| 242 | + } |
| 243 | + } |
| 244 | + else |
| 245 | + { |
| 246 | + foreach (var s in AjaxPro.Utility.Settings.JsonDeserializationCustomTypesDenied) |
| 247 | + if ((s.EndsWith("*") && type.FullName.StartsWith(s.Substring(0, s.Length - 1), StringComparison.InvariantCultureIgnoreCase)) || s == type.FullName) |
| 248 | + { |
| 249 | + ex = new SecurityException("This type is not allowed as argument for this method."); |
| 250 | + return false; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + return true; |
| 255 | + } |
| 256 | + |
231 | 257 | /// <summary> |
232 | 258 | /// Deserializes the custom object. |
233 | 259 | /// </summary> |
|
0 commit comments