Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getString fails on reload after calling delete with encyptionKey set in config #665

Open
morganick opened this issue Apr 19, 2024 · 8 comments

Comments

@morganick
Copy link

morganick commented Apr 19, 2024

Steps to reproduce:

  • Set the encryptionKey in the config for your MMKV store
  • Set a single key in the store
  • Reload application to see the value you set pulling from the store with getString
  • Remove the key from the store
  • Set the single key in the store
  • Reload the app with the developer menu
  • 💥 Value is undefined
  • Close the application
  • Open application
  • Value appears back on the screen

Conditions to reproduce:

  • encryptionKey set in config
  • Item being stored is over 253 characters long
  • Only one item is stored in MMKV

Minimum Reproducible Example (MRE)

There are instructions in the README for installation:

https://github.com/morganick/mmkv-encryption-app-mre

I've documented permutations in the code that you can make that make it work again:

Video

This happens on both iOS and Android.

iOS 17.4 - iPhone 15 Pro
MMKV_encrypted_single_key_store_delete_reload_bug.mov

Logs

If you use the create-expo-app cli, you'll get MMKV logging in the metro output as follow:

[MMKVEncryptionTestApp] [I] <MMKV_IO.cpp:84::loadFromFile> loading [session] with 0 actual size, file size 16384, InterProcess 0, meta info version:0
[MMKVEncryptionTestApp] [I] <MMKV_IO.cpp:133::loadFromFile> loaded [session] with 0 key-values
[MMKVEncryptionTestApp] [I] <MMKV_IO.cpp:516::writeActualSize> [session] increase sequence to 1, crc 727578001, actualSize 4
iOS Bundled 37ms (node_modules/expo/AppEntry.js)
iOS Bundled 35ms (node_modules/expo/AppEntry.js)
[MMKVEncryptionTestApp] Installing global.mmkvCreateNewInstance...
[MMKVEncryptionTestApp] [W] <libMMKV.mm:76::+[MMKV initializeMMKV:logLevel:handler:]> already called +initializeMMKV before, ignore this request
[MMKVEncryptionTestApp] Installed global.mmkvCreateNewInstance!
[MMKVEncryptionTestApp] [D] <libMMKV.mm:919::+[MMKV mmapKeyWithMMapID:rootPath:]> mmapKey: session
iOS Bundled 37ms (node_modules/expo/AppEntry.js)
iOS Bundled 31ms (node_modules/expo/AppEntry.js)
[MMKVEncryptionTestApp] Installing global.mmkvCreateNewInstance...
[MMKVEncryptionTestApp] [W] <libMMKV.mm:76::+[MMKV initializeMMKV:logLevel:handler:]> already called +initializeMMKV before, ignore this request
[MMKVEncryptionTestApp] Installed global.mmkvCreateNewInstance!
[MMKVEncryptionTestApp] [D] <libMMKV.mm:919::+[MMKV mmapKeyWithMMapID:rootPath:]> mmapKey: session
[MMKVEncryptionTestApp] [E] <MMKV_OSX.cpp:225::getObject> InvalidProtocolBuffer truncatedMessage
iOS Bundled 37ms (node_modules/expo/AppEntry.js)
iOS Bundled 29ms (node_modules/expo/AppEntry.js)
[MMKVEncryptionTestApp] Installing global.mmkvCreateNewInstance...
[MMKVEncryptionTestApp] [W] <libMMKV.mm:76::+[MMKV initializeMMKV:logLevel:handler:]> already called +initializeMMKV before, ignore this request
[MMKVEncryptionTestApp] Installed global.mmkvCreateNewInstance!
[MMKVEncryptionTestApp] [D] <libMMKV.mm:919::+[MMKV mmapKeyWithMMapID:rootPath:]> mmapKey: session
[MMKVEncryptionTestApp] [E] <MMKV_OSX.cpp:225::getObject> InvalidProtocolBuffer truncatedMessage
[MMKVEncryptionTestApp] [I] <MMKV_OSX.cpp:99::setIsInBackground> g_isInBackground:1
[MMKVEncryptionTestApp] [I] <libMMKV.mm:308::+[MMKV didEnterBackground]> isInBackground:1

[!NOTE]
The InvalidProtocolBuffer truncatedMessage shows up in the logs when this breaks.

Special thanks to @frankcalise for being a sounding board while I tracked this down and testing out the MRE.

@scblason
Copy link

I have a similar issue with version 2.12.2. Before I was using 2.9.0 and I don't have any issues (and we just updated MMKV, we didn't made any code changes).
But repro steps are similar to the ones mentioned above. The getString fails to get the valid data in an ecrypted store after applying a delete and set operation.
In our case:

  • We refresh the app
  • We read the store (getString OK)
  • We delete the key from the store
  • We set a new value for that same key
  • We do a getString from that key FAILS

@scblason
Copy link

scblason commented May 6, 2024

An update on this: I get the same error in 2.9.0. This started to happen when we updated to RN 0.73.x

@Akumzy
Copy link

Akumzy commented May 7, 2024

Facing the same issue here

@mrousavy
Copy link
Owner

mrousavy commented May 8, 2024

Two things:

  1. Does this bug also happen when not using encryption?
  2. Can you reproduce this in the current example app? This one uses new-arch, react-native-mmkv V3. Please test this.

@Romick2005
Copy link

  1. It is not happen when not using encryption.

@mrousavy
Copy link
Owner

Okay gotcha - maybe a bug with encryption. Did you test if react-native-mmkv V3 fixes this issue? (requires new architeceture)

@scblason
Copy link

Okay gotcha - maybe a bug with encryption. Did you test if react-native-mmkv V3 fixes this issue? (requires new architeceture)

I tested with latest v3 version and I had the same issue, but I'm not working with the new architecture though

@morganick
Copy link
Author

@mrousavy it happens only when encryption is turned on. I pulled in the code from my repro project into the example application, but haven't had a chance to put it through its paces yet. I'll try to get to that this afternoon and report back.

Thanks for your patience 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants