# SPDX-License-Identifier: BSD-2-Clause # SPDX-FileCopyrightText: 2023 KDE e.V. # SPDX-FileContributor: Ingo Klöcker import logging import re from sftpnotary import config, util from sftpnotary.exceptions import Error log = logging.getLogger(__name__) def getApplicationId(apkOrAabFilePath): if apkOrAabFilePath.suffix == ".aab": return getApplicationIdFromAab(apkOrAabFilePath) elif apkOrAabFilePath.suffix == ".apk": return getApplicationIdFromApk(apkOrAabFilePath) raise Error(f"Unsupported file format {apkOrAabFilePath.suffix}") def getApplicationIdFromAab(aabFilePath): workingDirectory = aabFilePath.parent aabFileName = aabFilePath.name bundletoolJarPath = config.settings.getPath("ApkSigning", "BundletoolJarPath") command = ["java", "-jar", bundletoolJarPath, "dump", "manifest", "--bundle", aabFileName] result = util.runCommand(command, cwd=workingDirectory) if not result.stdout: raise Error("bundletool did not print the AndroidManifest.xml of the base module") # XML processing is dangerous; therefore, simply parse the XML as text # https://docs.python.org/3/library/xml.html#xml-vulnerabilities for line in result.stdout.splitlines(): # look for a line like .yml re.compile(f"{escapedAppId}\\.yml"), # files in / folders: # images/featureGraphic.png, images/icon.png re.compile(f"{escapedAppId}/{localeID}/images/featureGraphic\\.png"), re.compile(f"{escapedAppId}/{localeID}/images/icon\\.png"), # images/phoneScreenshots/*.png re.compile(f"{escapedAppId}/{localeID}/images/phoneScreenshots/{notEmptyStarGlob}\\.png"), # full_description.txt, short_description.txt, title.txt re.compile(f"{escapedAppId}/{localeID}/full_description\\.txt"), re.compile(f"{escapedAppId}/{localeID}/short_description\\.txt"), re.compile(f"{escapedAppId}/{localeID}/title\\.txt"), ] filteredNames = [name for name in filteredNames if any(regex.fullmatch(name) for regex in expectedNamesREs)] unexpectedNames = sorted(set(names) - set(filteredNames)) unexpectedNames = [n for n in unexpectedNames if not n.endswith("/")] # remove folder names if unexpectedNames: log.warning(f"Found unexpected files in the Fastlane zip file: {unexpectedNames}") return filteredNames