def get_min_sdk_from_files(files, apk_constants=None): """ Get the min_sdk from either the `apk_constants` if it exists, or the manifest file in `files` if it exists. If neither exists, return 1 as the default minimum SDK :param Iterable files: file names that should be iterated over :param Dict apk_constants: dictionary that can have constants throughout the apk :return: min_sdk if it exists or 1 :rtype: int """ try: # int conversion to change it if it is a NoneType, which will throw TypeError return int(apk_constants["min_sdk"]) except (KeyError, TypeError): for decompiled_file in files: if decompiled_file.lower().endswith("{separator}androidmanifest.xml".format(separator=os.sep)): return get_min_sdk(decompiled_file) return 1
def update_manifest(cls, path_to_manifest): """Users of this class should call this method instead of changing class attributes directly""" cls.manifest_path = path_to_manifest try: cls.manifest_xml = minidom.parse(path_to_manifest) except Exception: # path_to_manifest is None or has bad XML cls.manifest_xml = None log.debug("Failed to update manifest for file %s", path_to_manifest) return try: cls.min_sdk = get_min_sdk(cls.manifest_path) cls.target_sdk = get_target_sdk(cls.manifest_path) except AttributeError: # manifest path is not set, assume min_sdk and target_sdk cls.min_sdk = cls.target_sdk = 1 try: cls.package_name = get_package_from_manifest(cls.manifest_path) except IOError: cls.package_name = "PACKAGE_NOT_FOUND"
def test_get_min_sdk(vulnerable_manifest_path): assert 9 == get_min_sdk(minidom.parse(vulnerable_manifest_path))