react-native-mmkv-storage: Unable to use encrypted storage after restoring from iCloud
Describe the bug Either getting or setting in storage with encryption is failing.
For context, this is the wrapper around the library:
import { MMKVLoader } from 'react-native-mmkv-storage';
const SecureStorage = new MMKVLoader()
.withEncryption()
.withInstanceID('mySecureStorage')
.initialize();
export const setItem = (key: string, value: string): void => {
SecureStorage.setString(key, value);
};
export const getItem = (key: string): string | null | undefined =>
SecureStorage.getString(key);
and this being the set of actions taken:
SecureStorage.setItem(SOME_KEY, someValue); // this happens and does not throw an error
// ...and even about 1 second later...
SecureStorage.getItem(SOME_KEY); // this does not yield a value
To Reproduce
I have no consistent reproduction steps đ
itâs happening to a lot of people, but only rarely. On devices where this happens, it happens consistently. Itâs almost like react-native-mmkv-storage
is âbrokenâ for that device.
Expected behavior Setting a value synchronously should allow you to get it right after.
Platform Information:
- OS: iOS
- React Native Version 0.68.2
- Library Version 0.7.6
Additional context On the current device we have that has this issue, a recent restore from iCloud was performed, but it hasnât been proven that this is the cause.
Itâs worth noting that weâre on version 0.7.6 of this library, which gives us the following dependencies in Podfile.lock:
...
- MMKV (1.2.10):
- MMKVCore (~> 1.2.10)
- MMKVCore (1.2.13)
...
Whatâs interesting about that is that backup & restore was introduced in MMKV 1.2.11, so our version of MMKVCore would have that functionality, but MMKV does not. Is that an issue?
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 29 (9 by maintainers)
I agree. There should be a way to work around this. I will look into it. The simplest way would be to exclude mmkv files from cloud backup and throw some error upon which store can be deleted and recreated possibly.
What about verifying on app load that the key works with the store, and entirely deleting the store if it does not? That way thereâs not an unusable MMKV store
But that would only resolve the issue for folks who choose to sync their keychain, right?
On my iCloud for example, I have âiCloud Backupâ set to âonâ and âKeychainâ set to âoffâ, so my keychain and my app backups would be out of sync and my app wouldnât survive a backup and restore.
Even besides getting backup & restore working, Iâd like to have the following to at least be able to mitigate it:
On that second point, the most we can do right now is clearStore, and it doesnât do enough to resolve the issue
The error today happens because when creating the storage, the library uses this property:
kSecAttrAccessibleAfterFirstUnlock
And reading apple docs of this property you have this information: After the first unlock, the data remains accessible until the next restart. This is recommended for items that need to be accessed by background applications. Items with this attribute migrate to a new device when using encrypted backups.
What happens is:
There are two solutions:
kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
Unfortunately the documentation is not clear enough, and this error is âignoredâ by the library, so if you use encryption you must follow one of these two steps.
@ammarahm-ed , @abejfehr , any solution yet?