This repository was archived by the owner on Dec 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed
tests/ServiceStack.Text.Tests Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,16 @@ public static void AssertAllowedRuntimeType(Type type)
161
161
if ( JsConfig . AllowRuntimeType ? . Invoke ( type ) == true )
162
162
return ;
163
163
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
+
164
174
var allowAttributesNamed = JsConfig . AllowRuntimeTypeWithAttributesNamed ;
165
175
if ( allowAttributesNamed ? . Count > 0 )
166
176
{
Original file line number Diff line number Diff line change @@ -921,6 +921,8 @@ public static string[] IgnoreAttributesNamed
921
921
922
922
public static HashSet < string > AllowRuntimeTypeWithInterfacesNamed { get ; set ; }
923
923
924
+ public static HashSet < string > DenyRuntimeTypeInNamespaces { get ; set ; }
925
+
924
926
public static HashSet < string > AllowRuntimeTypeInTypesWithNamespaces { get ; set ; }
925
927
926
928
public static Func < Type , bool > AllowRuntimeType { get ; set ; }
@@ -999,6 +1001,10 @@ public static void Reset()
999
1001
{
1000
1002
"ServiceStack.Messaging" ,
1001
1003
} ;
1004
+ DenyRuntimeTypeInNamespaces = new HashSet < string >
1005
+ {
1006
+ "System.CodeDom.Compiler" ,
1007
+ } ;
1002
1008
PlatformExtensions . ClearRuntimeAttributes ( ) ;
1003
1009
ReflectionExtensions . Reset ( ) ;
1004
1010
JsState . Reset ( ) ;
Original file line number Diff line number Diff line change @@ -205,5 +205,32 @@ public void Does_allow_Unknown_Type_in_MQ_Messages()
205
205
var fromJson = json . FromJson < Message > ( ) ;
206
206
Assert . That ( fromJson . Body . GetType ( ) , Is . EqualTo ( typeof ( AType ) ) ) ;
207
207
}
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
+ }
208
235
}
209
236
}
You can’t perform that action at this time.
0 commit comments