From 00a55bb34688b5f8c7cea95bba723b82a1e67426 Mon Sep 17 00:00:00 2001 From: Joshua Quick Date: Thu, 10 Dec 2020 19:49:58 -0800 Subject: [PATCH] fix(android): file listing/exist checks fail with remote encryption policy Fixes TIMOB-28283 --- .../kroll/util/KrollAssetHelper.java | 57 ++++++++++++++----- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/android/runtime/common/src/java/org/appcelerator/kroll/util/KrollAssetHelper.java b/android/runtime/common/src/java/org/appcelerator/kroll/util/KrollAssetHelper.java index 1abad992c13..f999c1c948f 100644 --- a/android/runtime/common/src/java/org/appcelerator/kroll/util/KrollAssetHelper.java +++ b/android/runtime/common/src/java/org/appcelerator/kroll/util/KrollAssetHelper.java @@ -32,6 +32,7 @@ public class KrollAssetHelper private static String packageName; private static String cacheDir; private static AssetCrypt assetCrypt; + private static Collection encryptedAssetFilePaths; private static HashSet apkAssetFilePathSet = new HashSet<>(256); private static DirectoryListingMap apkDirectoryListingMap = new DirectoryListingMap(32); private static DirectoryListingMap encrpytedDirectoryListingMap = new DirectoryListingMap(256); @@ -53,15 +54,9 @@ public static void setAssetCrypt(AssetCrypt assetCrypt) KrollAssetHelper.assetCrypt = assetCrypt; // Fetch all directories and their file listings from encrypted assets. (For fast access.) + KrollAssetHelper.encryptedAssetFilePaths = null; KrollAssetHelper.encrpytedDirectoryListingMap.clear(); - if (assetCrypt != null) { - Collection collection = assetCrypt.getAssetPaths(); - if (collection != null) { - for (String path : collection) { - KrollAssetHelper.encrpytedDirectoryListingMap.addPath(path); - } - } - } + getEncrpytedDirectoryListingMap(); } public static void init(Context context) @@ -198,10 +193,30 @@ public static String readAsset(String path) public static Collection getEncryptedAssetPaths() { - if (assetCrypt != null) { - return assetCrypt.getAssetPaths(); + // Do not continue if there are no encrypted assets. + if (KrollAssetHelper.assetCrypt == null) { + return null; } - return null; + + // Fetch encrypted asset file path collection if not done already. + // Note: Make sure all paths are prefixed with "Resources/" root directory if missing. + // This happens when property "appc-sourcecode-encryption-policy" is set to "remote". + if (KrollAssetHelper.encryptedAssetFilePaths == null) { + Collection pathCollection = KrollAssetHelper.assetCrypt.getAssetPaths(); + if ((pathCollection != null) && !pathCollection.isEmpty()) { + if (pathCollection.contains("app.js")) { + HashSet pathSet = new HashSet<>(pathCollection.size()); + for (String nextPath : pathCollection) { + pathSet.add("Resources/" + nextPath); + } + pathCollection = pathSet; + } + KrollAssetHelper.encryptedAssetFilePaths = pathCollection; + } + } + + // Return the requested collection. + return KrollAssetHelper.encryptedAssetFilePaths; } public static String readFile(String path) @@ -242,7 +257,7 @@ public static boolean assetExists(String path) public static boolean directoryExists(String path) { path = normalizeDirectoryPath(path); - if (encrpytedDirectoryListingMap.containsKey(path)) { + if (getEncrpytedDirectoryListingMap().containsKey(path)) { return true; } if (apkDirectoryListingMap.containsKey(path)) { @@ -264,7 +279,7 @@ public static List getDirectoryListing(String path) } { // Merge the encrypted assets listing (if any) into the result list. - Collection collection = encrpytedDirectoryListingMap.get(path); + Collection collection = getEncrpytedDirectoryListingMap().get(path); if (collection != null) { for (String entry : collection) { if (!resultList.contains(entry)) { @@ -286,6 +301,22 @@ public static String getCacheDir() return cacheDir; } + private static DirectoryListingMap getEncrpytedDirectoryListingMap() + { + // Fetch all encrypted directory file listings if not done already. + // Note: When property "appc-sourcecode-encryption-policy" is set to "remote", + // the returned path collection will be empty until remote info has been downloaded. + if (KrollAssetHelper.encrpytedDirectoryListingMap.isEmpty() && (KrollAssetHelper.assetCrypt != null)) { + Collection collection = getEncryptedAssetPaths(); + if (collection != null) { + for (String path : collection) { + KrollAssetHelper.encrpytedDirectoryListingMap.addPath(path); + } + } + } + return KrollAssetHelper.encrpytedDirectoryListingMap; + } + private static String normalizeDirectoryPath(String path) { // Normalize the given path, if necessary.