Skip to content

Commit 58b6571

Browse files
Michael SchwarzMichael Schwarz
authored andcommitted
added check for custom type deserialization
1 parent 1b74b05 commit 58b6571

File tree

6 files changed

+71
-33
lines changed

6 files changed

+71
-33
lines changed

AjaxPro/AssemblyInfo.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
* MR Matthew Raymer
3030
*
3131
*
32-
*
33-
*
3432
* MS 06-04-03 fixed missing http error status code in core.js
3533
* MS 06-04-04 added AjaxPro.onError, onTimeout, onStateChanged, onLoading to core.js
3634
* MS 06-04-05 removed Object.prototype.extend from prototype.js and all othere files using this
@@ -41,7 +39,7 @@
4139
* MS 06-07-19 removed ReflectionPermission attribute (why did we add it?)
4240
* MS 21-10-30 added contentSecurityPolicy to specify a nonce for all scripts
4341
* MS 21-11-22 changed default behavior of passing types during deserialization to deny
44-
*
42+
* MS 21-11-29 added check for custom type deserialization
4543
*
4644
*/
4745
using System;
@@ -93,7 +91,7 @@
9391
// You can specify all the values or you can default the Revision and Build Numbers
9492
// by using the '*' as shown below:
9593

96-
[assembly: AssemblyVersion("21.11.22.1")] // do not remove the blanks!!!!
94+
[assembly: AssemblyVersion("21.11.29.1")] // do not remove the blanks!!!!
9795

9896
//
9997
// In order to sign your assembly you must specify a key to use. Refer to the

AjaxPro/JSON/Converters/DataTableConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* MS 06-09-26 improved performance using StringBuilder
3434
* MS 07-04-24 added renderJsonCompliant serialization
3535
* MS 08-03-21 fixed DataTable client-side script
36-
*
36+
* MS 21-11-29 added check for custom type deserialization
3737
*
3838
*
3939
*/
@@ -147,7 +147,10 @@ public override object Deserialize(IJavaScriptObject o, Type t)
147147
for (int i = 0; i < columns.Count; i++)
148148
{
149149
column = (JavaScriptArray)columns[i];
150+
150151
colType = Type.GetType(column[1].ToString(), true);
152+
JavaScriptDeserializer.ThrowExceptionIfNotCustomTypeDeserializationAllowed(colType);
153+
151154
dt.Columns.Add(column[0].ToString(), colType);
152155
}
153156

AjaxPro/JSON/Converters/HashtableConverter.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* MS 06-09-24 use QuoteString instead of Serialize
3333
* MS 06-09-26 improved performance using StringBuilder
3434
* MS 07-04-24 added renderJsonCompliant serialization
35-
*
35+
* MS 21-11-29 added check for custom type deserialization
3636
*
3737
*/
3838
using System;
@@ -86,8 +86,15 @@ public override object Deserialize(IJavaScriptObject o, Type t)
8686
for (int i = 0; i < a.Count; i++)
8787
{
8888
aa = (JavaScriptArray)a[i];
89-
key = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[0], Type.GetType(((JavaScriptString)aa[2]).ToString()));
90-
value = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[1], Type.GetType(((JavaScriptString)aa[3]).ToString()));
89+
90+
Type keyType = Type.GetType(((JavaScriptString)aa[2]).ToString());
91+
Type valueType = Type.GetType(((JavaScriptString)aa[3]).ToString());
92+
93+
JavaScriptDeserializer.ThrowExceptionIfNotCustomTypeDeserializationAllowed(keyType);
94+
JavaScriptDeserializer.ThrowExceptionIfNotCustomTypeDeserializationAllowed(valueType);
95+
96+
key = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[0], keyType);
97+
value = JavaScriptDeserializer.Deserialize((IJavaScriptObject)aa[1], valueType);
9198

9299
d.Add(key, value);
93100
}

AjaxPro/JSON/Converters/HtmlControlConverter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
/*
2727
* MS 06-05-23 using local variables instead of "new Type()" for get De-/SerializableTypes
2828
* MS 06-09-26 improved performance using StringBuilder
29+
* MS 21-11-29 added check for custom type deserialization
30+
*
2931
*
3032
*
3133
*/
@@ -171,12 +173,14 @@ internal static HtmlControl HtmlControlFromString(string html, Type type)
171173
if(!typeof(HtmlControl).IsAssignableFrom(type))
172174
throw new InvalidCastException("The target type is not a HtmlControlType");
173175

176+
JavaScriptDeserializer.ThrowExceptionIfNotCustomTypeDeserializationAllowed(type);
177+
174178
html = AddRunAtServer(html, (Activator.CreateInstance(type) as HtmlControl).TagName);
175179

176180
if(type.IsAssignableFrom(typeof(HtmlSelect)))
177181
html = CorrectAttributes(html);
178182

179-
Control o = HtmlControlConverterHelper.Parse(html);;
183+
Control o = HtmlControlConverterHelper.Parse(html);
180184

181185
if(o.GetType() == type)
182186
return (o as HtmlControl);

AjaxPro/JSON/JavaScriptDeserializer.cs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@
3838
* MS 06-09-26 improved performance removing three-times cast
3939
* MS 21-10-27 added allowed customized types for JSON deserialization
4040
* MS 21-11-22 changed error message when type is not allowed
41-
*
41+
* MS 21-11-29 added check for custom type deserialization
4242
*
4343
*/
4444
using System;
4545
using System.Text;
4646
using System.Reflection;
4747
using System.Collections;
48+
using System.Security;
4849

4950
namespace AjaxPro
5051
{
@@ -146,27 +147,7 @@ public static object Deserialize(IJavaScriptObject o, Type type)
146147
if (type == null || type.IsAssignableFrom(t))
147148
{
148149
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);
170151
}
171152
}
172153

@@ -228,6 +209,51 @@ public static object Deserialize(IJavaScriptObject o, Type type)
228209

229210
#region Internal Methods
230211

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+
231257
/// <summary>
232258
/// Deserializes the custom object.
233259
/// </summary>

AjaxPro/Utilities/Utility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* using new AjaxSecurityProvider
4949
* MS 09-02-17 fixed memory problem
5050
* MS 21-10-30 added contentSecurityPolicy to specify a nonce for all scripts
51-
*
51+
* MS 21-11-29 removed HtmlControlConverter from default converters
5252
*
5353
*/
5454
using System;
@@ -254,7 +254,7 @@ internal static void AddDefaultConverter(AjaxSettings settings)
254254
AddConverter(settings, new IEnumerableConverter());
255255

256256
AddConverter(settings, new DataRowConverter());
257-
AddConverter(settings, new HtmlControlConverter());
257+
//AddConverter(settings, new HtmlControlConverter());
258258

259259
#endregion
260260
}

0 commit comments

Comments
 (0)