@@ -19,6 +19,9 @@ public class FastjsonCheck implements SinkSafeChecker {
1919
2020 private String policySignature ;
2121
22+ private static ClassLoader JSON_CLASS_LOADER ;
23+ private static ClassLoader PARSE_CONFIG_CLASS_LOADER ;
24+
2225 @ Override
2326 public boolean match (MethodEvent event , SinkNode sinkNode ) {
2427 if (sinkNode .getMethodMatcher () instanceof SignatureMethodMatcher ) {
@@ -31,7 +34,12 @@ public boolean match(MethodEvent event, SinkNode sinkNode) {
3134 @ Override
3235 public boolean isSafe (MethodEvent event , SinkNode sinkNode ) {
3336 try {
34- Class <?> cls = Class .forName ("com.alibaba.fastjson.JSON" );
37+ Class <?> cls ;
38+ if (JSON_CLASS_LOADER == null ) {
39+ cls = Class .forName ("com.alibaba.fastjson.JSON" );
40+ } else {
41+ cls = Class .forName ("com.alibaba.fastjson.JSON" , false , JSON_CLASS_LOADER );
42+ }
3543 Field f = cls .getDeclaredField ("VERSION" );
3644 Class <?> t = f .getType ();
3745 if (t != String .class ) {
@@ -51,14 +59,28 @@ public boolean isSafe(MethodEvent event, SinkNode sinkNode) {
5159 }
5260
5361 // https://github.com/alibaba/fastjson/wiki/fastjson_safemode
54- Class <?> cfgClass = Class .forName ("com.alibaba.fastjson.parser.ParserConfig" );
62+ Class <?> cfgClass ;
63+ if (PARSE_CONFIG_CLASS_LOADER == null ) {
64+ cfgClass = Class .forName ("com.alibaba.fastjson.parser.ParserConfig" );
65+ } else {
66+ cfgClass = Class .forName ("com.alibaba.fastjson.parser.ParserConfig" , false , PARSE_CONFIG_CLASS_LOADER );
67+ }
5568 Object cfg = cfgClass .getMethod ("getGlobalInstance" ).invoke (null );
5669 Object isSafeMode = cfg .getClass ().getMethod ("isSafeMode" ).invoke (cfg );
5770 return isSafeMode != null && (Boolean ) isSafeMode ;
5871 } catch (Throwable e ) {
5972 DongTaiLog .debug ("fastjson version and safe mode check failed: {}, {}" ,
60- e .getMessage (), e .getCause () != null ? e .getCause ().getMessage () : "" );
73+ e .getClass ().getName () + ": " + e .getMessage (),
74+ e .getCause () != null ? e .getCause ().getMessage () : "" );
6175 return true ;
6276 }
6377 }
78+
79+ public static void setJsonClassLoader (ClassLoader jsonClassLoader ) {
80+ JSON_CLASS_LOADER = jsonClassLoader ;
81+ }
82+
83+ public static void setParseConfigClassLoader (ClassLoader parseConfigClassLoader ) {
84+ PARSE_CONFIG_CLASS_LOADER = parseConfigClassLoader ;
85+ }
6486}
0 commit comments