55import fnmatch
66import os
77
8+ from Permissions import Permissions
9+
810class Analyze :
911 """Analyze object that scrapes project source looking for permissions matches."""
1012
11- def __init__ (self , project_root , package_name , permissions ):
13+ def __init__ (self , project_root , package_name , permissions , ignore ):
1214 """Init method of Analyze."""
1315 self .project_root = project_root
1416 self .package_name = package_name
1517 self .permissions = permissions
1618 self .report_file_name = "reports/source_report_" + self .package_name + ".txt"
1719 self .source_files = []
1820 self .lines = []
21+ self .ignore = ignore
1922
2023 def search_project_root (self ):
2124 """Looks in the source root for matching files with permissions."""
2225 print ("Analyzing from project root...." )
23- search_string = "permission"
26+
2427 source_root = self .project_root + "/app/src/"
2528 matches = []
2629
30+ # Add any ignored group permissions to the set of individual perms
31+ dangerous_permissions = Permissions ().dangerous_permissions
32+ if len (self .ignore ['groups' ]) > 0 :
33+ for group in self .ignore ['groups' ]:
34+
35+ # Get the specific list of permission group and permissions
36+ ignored_permissions = dangerous_permissions [group ]
37+ for permission in ignored_permissions :
38+ dangerous_permission = "android.permission." + permission
39+ self .ignore ['individual' ].add (dangerous_permission )
40+
41+ # Ignore specific permissions
42+ if len (self .ignore ['individual' ]) > 0 :
43+ print ("Based on config, ignoring the following permissions:" )
44+ for permission in self .ignore ['individual' ]:
45+ print ("Ignoring: " + permission )
46+
2747 # Search for matching java files
2848 for root , dirnames , filenames in os .walk (source_root ):
2949 for filename in fnmatch .filter (filenames , "*.java" ):
@@ -32,12 +52,19 @@ def search_project_root(self):
3252 current_file = ""
3353 with open (file ) as java_file :
3454 for index , line in enumerate (java_file ):
35- if search_string in line :
36- if current_file is not java_file .name :
37- current_file = java_file .name
38- self .lines .append (('{} {:>4}\n ' .format ("\n File: " , current_file )))
39- self .source_files .append (current_file )
40- self .lines .append (('{:>4} {}' .format (index , line .rstrip ())))
55+ if "permission" in line :
56+
57+ # Ignore the line if it has an ignored permission,
58+ # otherwise add the line to the source_lines list
59+ for ignored_permission in self .ignore ['individual' ]:
60+ if ignored_permission in line :
61+ break
62+ else :
63+ if current_file is not java_file .name :
64+ current_file = java_file .name
65+ self .lines .append (('{} {:>4}\n ' .format ("\n File: " , current_file )))
66+ self .source_files .append (current_file )
67+ self .lines .append (('{:>4} {}' .format (index , line .rstrip ())))
4168 print ("Analyzing finished!" )
4269
4370 # Print the source report
0 commit comments