From 6aeca53b3ed4530e57a31d7a5593ae16b550b985 Mon Sep 17 00:00:00 2001 From: antFrancon <72255317+antFrancon@users.noreply.github.com> Date: Wed, 16 Oct 2024 07:30:30 -0700 Subject: [PATCH] fix(android): Prevent ArrayIndexOutOfBoundsException in permission check (#47047) Summary: This PR addresses a potential `ArrayIndexOutOfBoundsException` in the Android module's permission checking logic. ```diff - results.length > 0 && results[j] == PackageManager.PERMISSION_GRANTED + results.length > j && results[j] == PackageManager.PERMISSION_GRANTED ``` It ensures that we only access the results array when the index `j` is within bounds, preventing crashes due to invalid array access that have been occurring in the production environment. Here is the Crashlytics dashboard concerning this type of crash on my app last week (react-native 0.75.4 - old arch): ![image](https://github.com/user-attachments/assets/0b7c1a10-39f6-4a17-9eee-4b17986f5b85) ## Changelog: [ANDROID] [FIXED] - Prevent ArrayIndexOutOfBoundsException in permission check Pull Request resolved: https://github.com/facebook/react-native/pull/47047 Test Plan: 1. Verify normal permission request scenarios still work on Android: - Request a permission (e.g., camera, location) - Grant the permission - Verify the app functions correctly with the granted permission 2. Monitor production crash reports: - Deploy the fix to production - Observe a decrease in ArrayIndexOutOfBoundsException occurrences in the permission check logic Note: As this crash was only observed in production, we'll rely on production monitoring to verify the fix's effectiveness over time. Reviewed By: rshest Differential Revision: D64464171 Pulled By: cortinico fbshipit-source-id: 87beacf7ee749a51665577d05a56cc8d6889d70f --- .../com/facebook/react/modules/permissions/PermissionsModule.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt index 508b5ea21d3..5de54bd0752 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt @@ -130,7 +130,7 @@ public class PermissionsModule(reactContext: ReactApplicationContext?) : val callbackActivity = args[1] as PermissionAwareActivity for (j in permissionsToCheck.indices) { val permission = permissionsToCheck[j] - if (results.size > 0 && results[j] == PackageManager.PERMISSION_GRANTED) { + if (results.size > j && results[j] == PackageManager.PERMISSION_GRANTED) { grantedPermissions.putString(permission, GRANTED) } else { if (callbackActivity.shouldShowRequestPermissionRationale(permission)) {