"""Github vulnerabilty logic module.""" from os.path import exists from pyarn import lockfile import json import re from xml.etree import ElementTree def pom_file_parser(file_path, file_content_str): final_dict = {} namespaces = {'xmlns' : 'http://maven.apache.org/POM/4.0.0'} tree = ElementTree.fromstring(file_content_str) root = tree.getroot() deps = root.findall(".//xmlns:dependency", namespaces=namespaces) currentPackageList = {} for d in deps: groupId = d.find("xmlns:groupId", namespaces=namespaces) artifactId = d.find("xmlns:artifactId", namespaces=namespaces) version = d.find("xmlns:version", namespaces=namespaces) key = groupId.text + ':' + artifactId.text currentPackageList[key] = version.text final_dict[file_path] = currentPackageList return final_dict def req_file_dict(file_path, file_content_str): """convert requirement.txt to dictionary""" dependencies_in_file = {} data = file_content_str new_list = [] for value in data.split('\n'): val = value.replace(' ', '').replace('"', '').replace('.post1', '').lower() split_string = re.split(r',|>=|==|<=|~=|<|~|>|=', val) if len(split_string) > 1: new_list.extend(split_string) it = iter(new_list) currentPackageList = dict(zip(it, it)) dependencies_in_file[file_path] = currentPackageList return dependencies_in_file def composer_file_dict(file_path, file_content_str): """convert composer.json to dictionary""" final_dict = {} data = json.loads(file_content_str) all_packages = [] all_packages.append(data["require"]) currentPackageList = {} for element in all_packages: for k, v in element.items(): currentPackageList[k] = re.sub('[~^v]', '', v) final_dict[file_path] = currentPackageList return final_dict def package_file_dict(file_path, file_content_str): """convert package.json to dictionary""" final_dict = {} data = json.loads(file_content_str) all_packages = [] if "dependencies" in data: all_packages.append(data["dependencies"]) if "devDependencies" in data: all_packages.append(data["devDependencies"]) currentPackageList = {} for element in all_packages: for k, v in element.items(): currentPackageList[k] = re.sub('[~^]', '', v) final_dict[file_path] = currentPackageList return final_dict def yarn_file_dict(file_path, file_content_str): """convert yarn.lock to dictionary""" final_dict = {} my_lockfile = lockfile.Lockfile.from_str(file_content_str) currentPackageList = {} data = my_lockfile.data for packages in data: temp = packages.split(",")[0] if temp.startswith('@'): key = '@' + temp.split('@')[1] else: key = temp.split('@')[0] value = data[packages]['version'] currentPackageList[key] = value final_dict[file_path] = currentPackageList return final_dict def gem_file_dict(file_path, file_content_str): """convert Gemfile.lock to dictionary""" final_dict = {} new_list = [] data = file_content_str for i, value in enumerate(data.split('\n')): val = value.replace(')', '').replace(" ", '').replace('(', ' ') split_string = re.split(r',|>=|==|<=|~=|<|~|>|=| ', val) if len(split_string) > 1: for i in split_string: if i != '': if len(split_string) == 2: new_list.extend(split_string) package = [] for key in new_list: if key != '': package.append(key) it = iter(new_list) currentPackageList = dict(zip(it, it)) final_dict[file_path] = currentPackageList return final_dict