expo: 'Could not encrypt/decrypt the value for SecureStore' error on android app
Minimal reproducible example
https://snack.expo.dev/@simranjits11/error-with-securestorage
Summary
Context
When trying to get item using await SecureStore.getItemAsync('mobile'). It throws an error saying Could not encrypt/decrypt the value for SecureStore. The error is very device specific and happens occasionally.
- Message: Error: Could not encrypt/decrypt the value for SecureStore .
- SDK: 47.0.12
- Device: OnePlus 8T (Android 13)
Stack Trace
Could not encrypt/decrypt the value for SecureStore
at .construct([native code]:0:0) at .construct([native code]:0:0)
at .p(index.android.bundle:50:360)
at .s(index.android.bundle:48:363)
at .construct([native code]:0:0)
at .<unknown>(index.android.bundle:395:595)
at .h(index.android.bundle:395:731)
Environment
expo-env-info 1.0.5 environment info: System: OS: macOS 13.3.1 Shell: 5.9 - /bin/zsh Binaries: Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 9.6.7 - ~/Documents/Projects/realmagic/node_modules/.bin/npm Watchman: 2023.02.20.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /Users/simranjitsingh/.rbenv/shims/pod SDKs: iOS SDK: Platforms: DriverKit 22.4, iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4 IDEs: Android Studio: 2020.3 AI-203.7717.56.2031.7678000 Xcode: 14.3/14E222b - /usr/bin/xcodebuild npmPackages: react: 18.1.0 => 18.1.0 react-dom: 18.1.0 => 18.1.0 npmGlobalPackages: eas-cli: 3.15.0 expo-cli: 6.3.2 Expo Workflow: managed
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 10
- Comments: 49 (8 by maintainers)
Commits related to this issue
- fix(suite-native): handle error on SecureStore.getItemAsync - For some users on some phones, sometimes... SecureStore is not wiped correctly on app uninstall so it failed to loaded when app is instal... — committed to trezor/trezor-suite by matejkriz 3 months ago
- fix(suite-native): handle error on SecureStore.getItemAsync - For some users on some phones, sometimes... SecureStore is not wiped correctly on app uninstall so it failed to loaded when app is instal... — committed to trezor/trezor-suite by matejkriz 3 months ago
- fix(suite-native): handle error on SecureStore.getItemAsync - For some users on some phones, sometimes... SecureStore is not wiped correctly on app uninstall so it failed to loaded when app is instal... — committed to trezor/trezor-suite by matejkriz 3 months ago
@behenate I have noticed that this issue mostly comes up if i install a new build (apk) without clearing the app data (by going to app info in device settings) of the previously installed build.
If i clear the app data and then uninstall the previous build before installing the new build, then i don’t see the issue. I haven’t tested a lot of devices for this behaviour so not sure if this is consistent across all devices, but found this interesting. It might help you pin point the issue.
Seems like the previously present app data/cache is causing the issue in the new build in some device models
@behenate is currently working on the migration to the new modules api so he will look into this also
@DavidRG13 Hi, sorry I can’t provide any ETA, because we couldn’t reproduce the issue, any instructions on a reliable way to reproduce the issue would be really helpful.
We are working on an update to
expo-secure-storeit doesn’t contain a specific fix for the issue but improves the error logging so it might give us some insights into the issue after we release it.I’ve experienced same behaviour and got many sentry errors. As @edmbn already mentioned in https://github.com/expo/expo/issues/23426#issuecomment-1723901477 you shoud use try/catch and delete your entry on error because secure store can not read the value. After doing so I got zero sentry errors. here code sample:
Ok, so apparently it is normal behavior not to delete Secure Storage and also It is normal not being able to decrypt old data after reinstalling. The solution is to return null in a try/catch and save new data inside the same key. You can always save new data inside the same key but you cannot read old data from an old installation.
Is there any update or a resolution to this issue? We’re seeing this issue on Android devices. At a minimum, Samsung Galaxy S23 and Pixel 7a. No specific secure options used, just the default config. On
expo-secure-storeversion 12.1.1, but I’m unsure if it’s happening on other versions. Android only issue. Does not happen on iOS.Works for me
@AJGeel If this issue is solved by
12.5.0(we are still not sure what causes it, but 12.5.0 introduces improvements in areas that are very likely to cause this issue) then yes, re-saving the value with the new version of secure-store should help.Hi @behenate, thank you for the update!
Say, there are devices that have data stored with a previous version of secure-store. If the app updates the data stored in secure-store with the
>= 12.5.0version, would that resolve / improve the issue?Same with expo sdk 49, expo secure store 12.3.1. 75% percent of our events are android 13.
Not the cleanest solution but it does the job for now.
@behenate
I have not yet tried to downgrade. What has worked for me is to create a function that tries 5 times before failing as a workaround.
@behenate I’m not sure if this is useful, but the only thing I can extract from Sentry is the stack trace:
@behenate any updates? Our users keep complaining that our app signs them off and it’s related to this bug 😢
Got same error on OnePlus 10T 5G (CPH2415)
After reinstalling the app (Android here), the underlying data is still there, but when we see the above errors, it means the secure store itself cannot self-recover by itself, the developer needs to have a logic to delete those bad records manually.
For user, the above case, they would have the experience that after installing a new version, then everything is gone, they have to re-login, re-store the state whatever they previous done, this sounds like not a good user experience.
That would be great if the secure store can handle it gracefully without interacting with a developer/user. (The best case would be: self-recover by itself and be able to read the data back correctly) Thanks.
We too have encountered many cases where the store returns null while there was a value stored in.
We store JWT auth tokens with redux thunk action:
And retrieve it:
The token maybe
nullwithout throwing error after installing an update of the app.Experience the same issue. So far proves to be exactly that. Started to happen after the new install. Though it affected only one of two otherwise identical devices. Also Android. One alternative way to clear the error is to flush all app data through device settings. It surely isn’t a solution for production, but handy if you need to clear it fast and have access to the device.
Same issue found after upgrading to “expo-secure-store”: “~12.1.1” and expo SDK 48 on android device
I can also repro this issue in SM-T220. I’m getting:
Could not encrypt/decrypt the value for SecureStorein Sentry on these devices.I have this configuration:
No
@behenate I didn’t reproduce the error in those devices, I’ve just extracted the models from Sentry.
We have no specific implementation, just like @Simranjits11 provided repro. We are storing a JSON with 3 keys parsed as a string.
In so many other Android devices everything is working.
I don’t know how can I help