emeraldsanto / react-native-encrypted-storage

React Native wrapper around EncryptedSharedPreferences and Keychain to provide a secure alternative to Async Storage.
https://www.npmjs.com/package/react-native-encrypted-storage
MIT License
571 stars 76 forks source link

iOS storage retrieval issue #116

Open gurtejsmartdata opened 1 year ago

gurtejsmartdata commented 1 year ago

hi I have issue in accessing encrypted storage on iOS, I get"code":"-25308","message":"RNEncryptedStorageError: An error occurred while retrieving value" randomly . Kindly suggest the solution for it

billnbell commented 1 year ago

I am getting this too on IOS. Seems like we need a fix.

adamzu commented 1 year ago

hi i'm getting this too and I'm wondering if it's because I implemented the code block in the "Note Reading Keychain Persistence" section. Did either of you who were running into this issue also follow this suggestion? I think it might be the problem described here - https://stackoverflow.com/questions/20269116/nsuserdefaults-losing-its-keys-values-when-phone-is-rebooted-but-not-unlocked. I'm going to try and disable this code and see if the problem is fixed

billnbell commented 1 year ago

did it work?

adamzu commented 1 year ago

Need a few more days to gather data and will circle back

billnbell commented 1 year ago

I did not implement that. I am still getting the problem - when coming from background to foreground I dont have the saved info.

I was going to try below but several people did not say it worked- one person said it worked.

- (void) applicationProtectedDataDidBecomeAvailable:(UIApplication *)application {
  NSLog(@"Application Protected Data Did Become Available");
  [NSUserDefaults resetStandardUserDefaults];
}
leevlad commented 1 year ago

I have seen this happen quite often, and for quite a while in our app too. We don't have a good way of reproducing this yet. Would appreciate if anyone had any suggestions.

adamzu commented 1 year ago

We ended up replacing this package with react-native-keychain, which seems to have solved the problem. My theory for the problem my team was facings is that on iOS, this package by default writes data to the keychain with a accessibility state that only allows the React Native app t load the secure data when the phone is unlocked (see here - https://developer.apple.com/documentation/security/keychain_services/keychain_items/restricting_keychain_item_accessibility#2974972). For us, this was problematic, because when a user received a notification while their phone was locked (e.g. was in their pocket), the app would start in the background, attempt to load the auth data, and be denied access. react-native-keychain let us use set the data accessibility option to 'Always Accessible' which got around this issue (albeit at some security risk, but it was acceptable for our purposes).

Not sure if that's what's happening for anyone else here, but figured I would share. Hope this helps!

leevlad commented 1 year ago

Thanks for taking the time to reply here @adamzu, that is helpful.

While we don't make any attempts to access app state when the device is locked, I suspect there might be other scenarios that cause the app to attempt to load the store before the keychain is ready.