diff --git a/exploration/treesitter.py b/exploration/treesitter.py index 185278ead2c59f45fa99e3ac234f69b5a2323bd4..1e2958473d685d0ba662f2e35dc46b59e1653a49 100644 --- a/exploration/treesitter.py +++ b/exploration/treesitter.py @@ -433,107 +433,16 @@ query = PY_LANGUAGE.query(""" captures = query.captures(tree.root_node) print("Capt",captures) -src = """ -@Interfacelogin -def parse_library(ctx:ElementContext)->Node|None: - # Tentukan jenis import nya - try: - import_type,import_name = ctx.name.split(" ") - if import_type == 'from': - # Import from statement - pass - except ValueErrror: - pass - -class A(): - def __init__(self) -> None: - pass - """ -tree = parser.parse(bytes(""" -@Interface.login -def parse_library(ctx:ElementContext)->Node|None: - # Tentukan jenis import nya - try: - import_type,import_name = ctx.name.split(" ") - if import_type == 'from': - # Import from statement - pass - except ValueErrror: - pass - -class A(): - def __init__(self) -> None: - pass - """,'utf-8')) -print(src[captures[0][0].start_byte:captures[0][0].end_byte]) -print(src[captures[1][0].start_byte:captures[1][0].end_byte]) -print(tree.root_node.children[0].children[0].children) -print(tree.root_node.children[0].children[0].children[-1].type) -print(tree.root_node.children[0].children[0].children[-1].text) - - -tree = parser.parse(bytes(""" -while asu(): - print("asu") - self.acl_data = ACReader(self.acl_path).read(1,a,c.fun()) - """,'utf-8')) - -print() -cursor = tree.walk() -cursor.goto_first_child() -print(cursor.node) -print(cursor.node.children) -cursor.goto_first_child() -cursor.goto_next_sibling() -print("j") -print(cursor.node) -print(cursor.node.children) -cursor.goto_parent() -cursor.goto_last_child() -print("j") -print(cursor.node) -print(cursor.node.children) -cursor.goto_first_child() -print(cursor.node) -print(cursor.node.children) -cursor.goto_next_sibling() -print(cursor.node) -print(cursor.node.children) -cursor.goto_first_child() -print(cursor.node) -print(cursor.node.children) -# self.acl_data = ACReader(self.acl_path).read() -print("Data: ACReader(self.acl_path).read()") -cursor.goto_last_child() -print(cursor.node) -print(cursor.node.children) -cursor.goto_first_child() -print("Data: self") -print(cursor.node) -print(cursor.node.children) -# cursor.goto_last_child() -# print(cursor.node) -# print(cursor.node.children) -# print(cursor.node.text) -# print(cursor.node.children[0].children) -# print(cursor.node.children[0].children[0]) -# print(cursor.node.children[0].children[0].children) tree = parser.parse(bytes(""" -def get_admin_mail(control): - if control=="admin": - data="admin@cybersecurity.intra" - import logging - logging.basicConfig(filename="restapi.log", filemode='w', level=logging.DEBUG) - logging.debug(data) - return jsonify(data=data),200 - else: - return jsonify(data="Control didn't set admin"), 200 +@login_required +def profile(): + return render_template('profile.html') """,'utf-8')) cursor = tree.walk() cursor.goto_first_child() cursor.goto_last_child() -cursor.goto_first_child() +# cursor.goto_first_child() print("kimi") print(cursor.node) print(cursor.node.children) \ No newline at end of file diff --git a/src/lib/ACLAnalyzer.py b/src/lib/ACLAnalyzer.py index 320615e436a75aa08cd26303625633e521a37985..8a34e046e08ac255c3634542326c8dab953b6964 100644 --- a/src/lib/ACLAnalyzer.py +++ b/src/lib/ACLAnalyzer.py @@ -64,7 +64,14 @@ class ACLAnalyzer(): node:Node = route.cfg.traverse() if(node.parent.type=='decorated_definition'): # Cari fungsi dekorator yang dipanggil + # if "profile" in route.name: + # print("NODA",node,node.parent) + # print(principal_list) + # print("A",set(principal_list)) + # print(node.parent.children) for dec in node.parent.children: + # if "profile" in route.name: + # print("PRINCIP",principal_list) if dec.type=='decorator': if dec.children[-1].type=='call': dekorator = dec.children[-1].children[0] @@ -74,14 +81,19 @@ class ACLAnalyzer(): acl_result = None if is_route: acl_result = self.check_acl_list(route,dekorator.text.decode().split("(")[0].replace("@","").strip()) + # if "profile" in route.name: + # print(is_route,dekorator,dekorator.text.decode().split("(")[0].replace("@","").strip()) + # print("RES",acl_result) + # if acl_result!=None: + # print(set(acl_result)) if(acl_result!=None): # Ini memang fungsi acl + # if "profile" in route.name: + # print("inyong",list(set(principal_list) & set(acl_result)),principal_list,set(principal_list)) principal_list = list(set(principal_list) & set(acl_result)) # Gak ada dekorator/dekorator gak cocok # Traverse fungsinya while node: - # if "get_admin_mail" in route.name: - # print(node) # Cek detail untuk beberapa tipe node if node.type in ['call','assignment']: # Cek call method @@ -320,8 +332,8 @@ class ACLAnalyzer(): components += flatten_node(child.children) # Cek tiap componentnya fun_name = "" - # if "admin_mail" in route.name: - # print("KOMP",components) + # if "reasult" in route.name: + # print("KOMP",components) for i in range(len(components)): if components[i] in ['and','or','not']: continue @@ -396,12 +408,23 @@ class ACLAnalyzer(): else: # Langsung cari yg terkecil principal_list = list(set(principal_list) & set(self.acl_info.acl_context[left_var_name])) + elif components[i] in ['in']: + # Cek apakah variaabel kiri merupakan key yang di kanan + fun_name = fun_name.replace('\"',"").replace("\'","") + # print(f"{components[i+1]}.{fun_name}") + acl_key = self.search_acl_var(f"{components[i+1]}.{fun_name}",route) + # print(acl_key) + if acl_key: + #Langsung cari yg terkecil + principal_list = list(set(principal_list) & set(self.acl_info.acl_context[acl_key])) elif components[i] in ['>','<','>=','<=']: continue else: # Tambahkan nama fungsinya fun_name += components[i] node = route.cfg.traverse() + # if "profile" in route.name: + # print("PRINCIPAL",principal_list) return principal_list def analyze_module(self,route:ElementContext)->list[list[str,list[str]]]: @@ -436,15 +459,20 @@ class ACLAnalyzer(): #TODO handle variabel dan kelas dan import file_method_list = self.project_info.dependency_manager.get(route.location).method_list # Format name: fun atau self.fun - acl_list = (role for role in self.acl_info.principal_list) + acl_list = [role for role in self.acl_info.principal_list] for acl_class in self.project_info.acl_class: + # if "profile" in route.name: + # print("NOW",set(acl_list)) acl_class.cfg.reset() if(acl_class.type=='library' and acl_class.location==route.location): + # if "profile" in route.name: + # print("a",acl_class.context) if(acl_class.context): # Cek apakah fungsi yang merupakan acl digunakan # context berisi daftar fungsi yang merupakan fungsi untuk cek ACL while True: acl_node:Node|None = acl_class.cfg.traverse() + # print("tuduh",acl_node) if not acl_node: break # Handle kasus antara gak ada context fungsi mana yang acl maupun ada @@ -454,12 +482,17 @@ class ACLAnalyzer(): # Cek apakah dia manggil fungsi yang diimport di acl elmt = acl_class.get_base_element_name().split(" ") formatted_key = f"lib:{elmt[-1]}::{name}" + # if "profile" in route.name: + # print(formatted_key,self.acl_info.acl_context) if formatted_key not in self.acl_info.acl_context: acl_list = None else: acl_list = self.acl_info.acl_context[formatted_key] + # print("SEKARANG",acl_list) # Kasus kalau acl nya kelas atau fungsi elif acl_class.type in ['class','function']: + # if "profile" in route.name: + # print("b") # Cek apakah ada parent context parent_class = "" for k in acl_class.context: @@ -494,6 +527,8 @@ class ACLAnalyzer(): elif(function['type']=='class_method' and function['name']==acl_class.get_base_element_name() and (name==f"{function['parent']}.{function['name']}" or function['parent']==parent_class)): acl_list = list(set(acl_list) & set(self.analyze_function(acl_class,False))) else: + # if "profile" in route.name: + # print("c") # Beda file, cek di import ada gak # Kali aja ada di module ini dependency_lists = self.project_info.dependency_manager.get(route.location).dependency_list @@ -555,6 +590,8 @@ class ACLAnalyzer(): if(tmp_name==tmp_comparator): # Cek apakah dia manggil fungsi yang diimport di acl acl_list = list(set(acl_list) & set(self.analyze_function(acl_class,False))) + # if "profile" in route.name: + # print("baiknya",acl_list) return acl_list def search_acl_var(self,var_name:str,route:ElementContext)->str: @@ -566,9 +603,14 @@ class ACLAnalyzer(): # Jika ini library for dep in self.project_info.dependency_manager.get(route.location).dependency_list: lib_name = dep[1]['original'] - format_lib = f'lib:{lib_name}::{".".join(components[1:])}' + format_lib = f'lib:{lib_name}::{check_var}' + # if "reasult" in route.name: + # print("ASU",components,check_var) + # print(format_lib) if (components[0]==dep[1]['rename'] or components[0]==dep[1]['original']) and format_lib in self.acl_info.acl_context: return format_lib + elif format_lib in self.acl_info.acl_context: + return format_lib else: # Kasus gak ada namespace for method in dep[2]: