mCodex / react-native-sensitive-info

Save sensitive data into Android's Shared Preferences with keystore encryption/iOS's Keychain for React Native
https://mcodex.dev/react-native-sensitive-info/
MIT License
982 stars 220 forks source link

Android: Attempt to invoke interface method on a null object reference (patch-package fix available) #364

Closed CostachescuCristinel closed 1 year ago

CostachescuCristinel commented 2 years ago

There is an issue when using this library on Android, where the method private String sharedPreferences(ReadableMap options) might receive a null object for options. Thus, calling options.hasKey() on it will throw the following error:

Attempt to invoke interface method 'boolean com.facebook.react.bridge.ReadableMap. hasKey(java.lang.String)' on a null object reference

This happens if you try to read any key of any storage (other than the default shared_preferences), before you ever written anything into it.

The solution is to just check for null before attempting options.hasKey().
The error screenshot and a patch-package patch is available below. Please include this fix in next releases.

react-native-sensitive-info/android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java

react-native-sensitive-info+6.0.0-alpha.9.patch

diff --git a/node_modules/react-native-sensitive-info/android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java b/node_modules/react-native-sensitive-info/android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java
index 9b4602a..f7be1c9 100644
--- a/node_modules/react-native-sensitive-info/android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java
+++ b/node_modules/react-native-sensitive-info/android/src/main/java/dev/mcodex/RNSensitiveInfoModule.java
@@ -265,7 +265,10 @@ public class RNSensitiveInfoModule extends ReactContextBaseJavaModule {

     @NonNull
     private String sharedPreferences(ReadableMap options) {
-        String name = options.hasKey("sharedPreferencesName") ? options.getString("sharedPreferencesName") : "shared_preferences";
+        String name = ((options != null) && options.hasKey("sharedPreferencesName"))
+           ? options.getString("sharedPreferencesName")
+           : "shared_preferences";
+       
         if (name == null) {
             name = "shared_preferences";
         }

Screenshot_1669365975

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.