1717import com .telerik .metadata .parsing .kotlin .extensions .bytecode .BytecodeExtensionFunctionsCollector ;
1818import com .telerik .metadata .parsing .kotlin .metadata .ClassMetadataParser ;
1919import com .telerik .metadata .parsing .kotlin .metadata .bytecode .BytecodeClassMetadataParser ;
20+ import com .telerik .metadata .security .MetadataSecurityViolationException ;
21+ import com .telerik .metadata .security .classes .SecuredClassRepository ;
22+ import com .telerik .metadata .security .classes .SecuredNativeClassDescriptor ;
2023import com .telerik .metadata .storage .functions .FunctionsStorage ;
2124import com .telerik .metadata .storage .functions .extensions .ExtensionFunctionsStorage ;
2225
@@ -48,26 +51,26 @@ static TreeNode build(List<String> paths) throws Exception {
4851 if (file .isFile ()) {
4952 if (path .endsWith (".jar" )) {
5053 JarFile jar = JarFile .readJar (path );
51- ClassRepo .addToCache (jar );
54+ SecuredClassRepository . INSTANCE .addToCache (jar );
5255 }
5356 } else if (file .isDirectory ()) {
5457 ClassDirectory dir = ClassDirectory .readDirectory (path );
55- ClassRepo .addToCache (dir );
58+ SecuredClassRepository . INSTANCE .addToCache (dir );
5659 }
5760 }
5861 }
5962
6063 TreeNode root = TreeNode .getRoot ();
6164
62- String [] classNames = ClassRepo .getClassNames ();
65+ String [] classNames = SecuredClassRepository . INSTANCE .getClassNames ();
6366
6467 for (String className : classNames ) {
6568 try {
66- NativeClassDescriptor clazz = ClassRepo .findClass (className );
67- if (clazz == null ) {
68- throw new ClassNotFoundException ( "Class " + className + " not found in the input android libraries." );
69+ SecuredNativeClassDescriptor clazz = SecuredClassRepository . INSTANCE .findClass (className );
70+ if (! clazz . isUsageAllowed () ) {
71+ throwMetadataSecurityViolationException ( className );
6972 } else {
70- tryCollectKotlinExtensionFunctions (clazz );
73+ tryCollectKotlinExtensionFunctions (clazz . getNativeDescriptor () );
7174 }
7275 } catch (Throwable e ) {
7376 System .out .println ("Skip " + className );
@@ -86,11 +89,11 @@ static TreeNode build(List<String> paths) throws Exception {
8689 // our class path API 17
8790 // Class<?> clazz = Class.forName(className, false, loader);
8891
89- NativeClassDescriptor clazz = ClassRepo .findClass (className );
90- if (clazz == null ) {
91- throw new ClassNotFoundException ( "Class " + className + " not found in the input android libraries." );
92+ SecuredNativeClassDescriptor clazz = SecuredClassRepository . INSTANCE .findClass (className );
93+ if (! clazz . isUsageAllowed () ) {
94+ throwMetadataSecurityViolationException ( className );
9295 } else {
93- generate (clazz , root );
96+ generate (clazz . getNativeDescriptor () , root );
9497 }
9598 } catch (Throwable e ) {
9699 System .out .println ("Skip " + className );
@@ -103,6 +106,10 @@ static TreeNode build(List<String> paths) throws Exception {
103106 return root ;
104107 }
105108
109+ private static void throwMetadataSecurityViolationException (String className ){
110+ throw new MetadataSecurityViolationException ("Class " + className + " could not be used!" );
111+ }
112+
106113 private static void tryCollectKotlinExtensionFunctions (NativeClassDescriptor classDescriptor ) {
107114 if (classDescriptor instanceof KotlinClassDescriptor ) {
108115 ExtensionFunctionsCollector extensionFunctionsCollector = new BytecodeExtensionFunctionsCollector (new BytecodeClassMetadataParser ());
@@ -319,8 +326,10 @@ private static void getFieldsFromImplementedInterfaces(NativeClassDescriptor cla
319326 String [] implementedInterfacesNames = clazz .getInterfaceNames ();
320327 if (implementedInterfacesNames .length > 0 ) {
321328 for (String currInterface : implementedInterfacesNames ) {
322- interfaceClass = ClassRepo .findClass (currInterface );
323- if (interfaceClass != null ) {
329+ SecuredNativeClassDescriptor securedNativeClassDescriptor = SecuredClassRepository .INSTANCE .findClass (currInterface );
330+
331+ if (securedNativeClassDescriptor .isUsageAllowed ()) {
332+ interfaceClass = securedNativeClassDescriptor .getNativeDescriptor ();
324333 fields = interfaceClass .getFields ();
325334
326335 // If the interface iteself extends other interfaces - add their fields too
@@ -361,14 +370,14 @@ private static TreeNode getOrCreateNode(TreeNode root, NativeTypeDescriptor type
361370 node = createArrayNode (root , typeName );
362371 } else {
363372 String name = ClassUtil .getCanonicalName (type .getSignature ());
364- NativeClassDescriptor clazz = ClassRepo . findClass (name );
373+ SecuredNativeClassDescriptor clazz = SecuredClassRepository . INSTANCE . findNearestAllowedClass (name );
365374
366375 // if clazz is not found in the ClassRepo, the method/field being analyzed will be skipped
367- if (clazz == null ) {
376+ if (! clazz . isUsageAllowed () ) {
368377 return null ;
369378 }
370379
371- node = getOrCreateNode (root , clazz , null );
380+ node = getOrCreateNode (root , clazz . getNativeDescriptor () , null );
372381 }
373382
374383 return node ;
@@ -451,10 +460,11 @@ private static TreeNode getOrCreateNode(TreeNode root, NativeClassDescriptor cla
451460 if (node .baseClassNode == null ) {
452461 NativeClassDescriptor baseClass = null ;
453462 if (predefinedSuperClassname != null ) {
454- baseClass = ClassUtil .getClassByName (predefinedSuperClassname );
463+ SecuredNativeClassDescriptor securedNativeClassDescriptor = SecuredClassRepository .INSTANCE .findNearestAllowedClass (predefinedSuperClassname );
464+ baseClass = securedNativeClassDescriptor .isUsageAllowed () ? securedNativeClassDescriptor .getNativeDescriptor () : null ;
455465 } else {
456466 baseClass = clazz .isInterface ()
457- ? ClassUtil . getClassByName ("java.lang.Object" )
467+ ? SecuredClassRepository . INSTANCE . findClass ("java.lang.Object" ). getNativeDescriptor () // java.lang.Object should always be available
458468 : ClassUtil .getSuperclass (clazz );
459469 }
460470 if (baseClass != null ) {
@@ -505,13 +515,17 @@ private static TreeNode createArrayNode(TreeNode root, String className)
505515 child .nodeType = node .nodeType ;
506516 child .arrayElement = node ;
507517 } else {
508- NativeClassDescriptor clazz = ClassRepo .findClass (name );
509- child .nodeType = clazz .isInterface () ? TreeNode .Interface
510- : TreeNode .Class ;
511- if (clazz .isStatic ()) {
512- child .nodeType |= TreeNode .Static ;
518+ SecuredNativeClassDescriptor securedNativeClassDescriptor = SecuredClassRepository .INSTANCE .findNearestAllowedClass (name );
519+ if (securedNativeClassDescriptor .isUsageAllowed ()) {
520+ NativeClassDescriptor nativeClassDescriptor = securedNativeClassDescriptor .getNativeDescriptor ();
521+ child .nodeType = nativeClassDescriptor .isInterface () ? TreeNode .Interface
522+ : TreeNode .Class ;
523+ if (nativeClassDescriptor .isStatic ()) {
524+ child .nodeType |= TreeNode .Static ;
525+ }
526+ child .arrayElement = getOrCreateNode (root , nativeClassDescriptor , null );
513527 }
514- child . arrayElement = getOrCreateNode ( root , clazz , null );
528+
515529 }
516530 }
517531
0 commit comments