17
17
import com .telerik .metadata .parsing .kotlin .extensions .bytecode .BytecodeExtensionFunctionsCollector ;
18
18
import com .telerik .metadata .parsing .kotlin .metadata .ClassMetadataParser ;
19
19
import 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 ;
20
23
import com .telerik .metadata .storage .functions .FunctionsStorage ;
21
24
import com .telerik .metadata .storage .functions .extensions .ExtensionFunctionsStorage ;
22
25
@@ -48,26 +51,26 @@ static TreeNode build(List<String> paths) throws Exception {
48
51
if (file .isFile ()) {
49
52
if (path .endsWith (".jar" )) {
50
53
JarFile jar = JarFile .readJar (path );
51
- ClassRepo .addToCache (jar );
54
+ SecuredClassRepository . INSTANCE .addToCache (jar );
52
55
}
53
56
} else if (file .isDirectory ()) {
54
57
ClassDirectory dir = ClassDirectory .readDirectory (path );
55
- ClassRepo .addToCache (dir );
58
+ SecuredClassRepository . INSTANCE .addToCache (dir );
56
59
}
57
60
}
58
61
}
59
62
60
63
TreeNode root = TreeNode .getRoot ();
61
64
62
- String [] classNames = ClassRepo .getClassNames ();
65
+ String [] classNames = SecuredClassRepository . INSTANCE .getClassNames ();
63
66
64
67
for (String className : classNames ) {
65
68
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 );
69
72
} else {
70
- tryCollectKotlinExtensionFunctions (clazz );
73
+ tryCollectKotlinExtensionFunctions (clazz . getNativeDescriptor () );
71
74
}
72
75
} catch (Throwable e ) {
73
76
System .out .println ("Skip " + className );
@@ -86,11 +89,11 @@ static TreeNode build(List<String> paths) throws Exception {
86
89
// our class path API 17
87
90
// Class<?> clazz = Class.forName(className, false, loader);
88
91
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 );
92
95
} else {
93
- generate (clazz , root );
96
+ generate (clazz . getNativeDescriptor () , root );
94
97
}
95
98
} catch (Throwable e ) {
96
99
System .out .println ("Skip " + className );
@@ -103,6 +106,10 @@ static TreeNode build(List<String> paths) throws Exception {
103
106
return root ;
104
107
}
105
108
109
+ private static void throwMetadataSecurityViolationException (String className ){
110
+ throw new MetadataSecurityViolationException ("Class " + className + " could not be used!" );
111
+ }
112
+
106
113
private static void tryCollectKotlinExtensionFunctions (NativeClassDescriptor classDescriptor ) {
107
114
if (classDescriptor instanceof KotlinClassDescriptor ) {
108
115
ExtensionFunctionsCollector extensionFunctionsCollector = new BytecodeExtensionFunctionsCollector (new BytecodeClassMetadataParser ());
@@ -319,8 +326,10 @@ private static void getFieldsFromImplementedInterfaces(NativeClassDescriptor cla
319
326
String [] implementedInterfacesNames = clazz .getInterfaceNames ();
320
327
if (implementedInterfacesNames .length > 0 ) {
321
328
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 ();
324
333
fields = interfaceClass .getFields ();
325
334
326
335
// If the interface iteself extends other interfaces - add their fields too
@@ -361,14 +370,14 @@ private static TreeNode getOrCreateNode(TreeNode root, NativeTypeDescriptor type
361
370
node = createArrayNode (root , typeName );
362
371
} else {
363
372
String name = ClassUtil .getCanonicalName (type .getSignature ());
364
- NativeClassDescriptor clazz = ClassRepo . findClass (name );
373
+ SecuredNativeClassDescriptor clazz = SecuredClassRepository . INSTANCE . findNearestAllowedClass (name );
365
374
366
375
// if clazz is not found in the ClassRepo, the method/field being analyzed will be skipped
367
- if (clazz == null ) {
376
+ if (! clazz . isUsageAllowed () ) {
368
377
return null ;
369
378
}
370
379
371
- node = getOrCreateNode (root , clazz , null );
380
+ node = getOrCreateNode (root , clazz . getNativeDescriptor () , null );
372
381
}
373
382
374
383
return node ;
@@ -451,10 +460,11 @@ private static TreeNode getOrCreateNode(TreeNode root, NativeClassDescriptor cla
451
460
if (node .baseClassNode == null ) {
452
461
NativeClassDescriptor baseClass = null ;
453
462
if (predefinedSuperClassname != null ) {
454
- baseClass = ClassUtil .getClassByName (predefinedSuperClassname );
463
+ SecuredNativeClassDescriptor securedNativeClassDescriptor = SecuredClassRepository .INSTANCE .findNearestAllowedClass (predefinedSuperClassname );
464
+ baseClass = securedNativeClassDescriptor .isUsageAllowed () ? securedNativeClassDescriptor .getNativeDescriptor () : null ;
455
465
} else {
456
466
baseClass = clazz .isInterface ()
457
- ? ClassUtil . getClassByName ("java.lang.Object" )
467
+ ? SecuredClassRepository . INSTANCE . findClass ("java.lang.Object" ). getNativeDescriptor () // java.lang.Object should always be available
458
468
: ClassUtil .getSuperclass (clazz );
459
469
}
460
470
if (baseClass != null ) {
@@ -505,13 +515,17 @@ private static TreeNode createArrayNode(TreeNode root, String className)
505
515
child .nodeType = node .nodeType ;
506
516
child .arrayElement = node ;
507
517
} 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 );
513
527
}
514
- child . arrayElement = getOrCreateNode ( root , clazz , null );
528
+
515
529
}
516
530
}
517
531
0 commit comments