Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit eb58322

Browse files
committed
Disable deserializing [Serializable] types in System.CodeDom.Compiler
1 parent 3323314 commit eb58322

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/ServiceStack.Text/Common/JsWriter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ public static void AssertAllowedRuntimeType(Type type)
161161
if (JsConfig.AllowRuntimeType?.Invoke(type) == true)
162162
return;
163163

164+
var denyTypesInNamespaces = JsConfig.DenyRuntimeTypeInNamespaces;
165+
if (denyTypesInNamespaces?.Count > 0)
166+
{
167+
foreach (var ns in denyTypesInNamespaces)
168+
{
169+
if (type.Namespace == ns)
170+
throw new NotSupportedException($"{type.Name} is not an allowed Runtime Type. Denied in JsConfig.DenyRuntimeTypeInNamespaces");
171+
}
172+
}
173+
164174
var allowAttributesNamed = JsConfig.AllowRuntimeTypeWithAttributesNamed;
165175
if (allowAttributesNamed?.Count > 0)
166176
{

src/ServiceStack.Text/JsConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ public static string[] IgnoreAttributesNamed
921921

922922
public static HashSet<string> AllowRuntimeTypeWithInterfacesNamed { get; set; }
923923

924+
public static HashSet<string> DenyRuntimeTypeInNamespaces { get; set; }
925+
924926
public static HashSet<string> AllowRuntimeTypeInTypesWithNamespaces { get; set; }
925927

926928
public static Func<Type, bool> AllowRuntimeType { get; set; }
@@ -999,6 +1001,10 @@ public static void Reset()
9991001
{
10001002
"ServiceStack.Messaging",
10011003
};
1004+
DenyRuntimeTypeInNamespaces = new HashSet<string>
1005+
{
1006+
"System.CodeDom.Compiler",
1007+
};
10021008
PlatformExtensions.ClearRuntimeAttributes();
10031009
ReflectionExtensions.Reset();
10041010
JsState.Reset();

tests/ServiceStack.Text.Tests/RuntimeSerializtionTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,32 @@ public void Does_allow_Unknown_Type_in_MQ_Messages()
205205
var fromJson = json.FromJson<Message>();
206206
Assert.That(fromJson.Body.GetType(), Is.EqualTo(typeof(AType)));
207207
}
208+
209+
[Test]
210+
public void Does_not_allow_Types_in_DenyRuntimeTypeInTypesWithNamespaces()
211+
{
212+
//Uses JsConfig.DenyRuntimeTypeInNamespaces
213+
214+
var types = new Type[]
215+
{
216+
#if NET45
217+
typeof(System.CodeDom.Compiler.TempFileCollection)
218+
#endif
219+
};
220+
221+
foreach (var type in types)
222+
{
223+
var json = CreateJson(type);
224+
try
225+
{
226+
var instance = json.FromJson<RuntimeObject>();
227+
Assert.Fail("Should throw " + type.Name);
228+
}
229+
catch (NotSupportedException ex)
230+
{
231+
ex.Message.Print();
232+
}
233+
}
234+
}
208235
}
209236
}

0 commit comments

Comments
 (0)