-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Seeing "Realm file decryption failed" errors #5615
Comments
Hi @JoshHrach, Any news on this topic ? I have the same problem with an App I'm working on. Implementation has been made by a team with a singleton pattern which seems not to be the best way for realm. Is it also you're case? Thanks, |
@Spenders No luck on our end. I ended up doing an early check of the Realm (within the AppDelegate) and am deleting the database if it hits this. It's cut down on crashes in our app, but it hasn't addressed the ultimate problem. |
We're also experiencing this issue as well with the latest available version of RealmSwift. It's quite annoying since encryption key is being generated properly and stored for later uses. |
Hey @JoshHrach @Spenders @corteggo @bmunkholm, we're having this happen for a small number of our users (including me). It's very intermittent and we haven't been able to nail it down yet. Has anyone worked this out or is it fixed in a later version of RealmSwift? |
I'm having the same issue as well (Realm v3.7.1, issue reported on iOS 11 and 12 so far). We never had an unencrypted Realm DB (It was encrypted from the start) so I don't think it's trying to decrypt an unencrypted DB. My best guess is that somehow something went wrong with the key? For now, I am checking and deleting the DB when the app starts (like @JoshHrach mentioned), but this is less than ideal as in some situations the DB containus user inputted data that it not backed anywhere. Received error:
Initialization code:
Key Getter Code (attempts to load a key from the keychain, if none found it generates one and then stores it back to the keychain):
Just like the others, this is happening only for a small amount of users, seemingly randomly. We have not been able to reproduce it ourselves. Maybe there's something going on with the keychain? Are you all storing your keys there? If not the only answer I see is that it's within Realm itself. |
Hey @JoshHrach @Spenders @corteggo @bmunkholm @jguerinet I think the issue might be about waking from a suspended state and Realm not starting fast enough to reply to the request to open the database file. We turned off all iOS background processing in the XCode project settings and it solved our main issue. We also think we've found a situation when we call the keychain for credentials for an encrypted database and it's returning the wrong credentials (maybe older ones), so we're now in the process of seeing if we can get multiple credentials and then cycle through each one to ensure we find the one that's valid. I hope this is helpful to you! |
One thing I notice about your |
@nwainwright Thanks for the update. I don't believe this is the case for me, as this is happening on the main thread upon opening the completely-terminated app. I also don't know how the wrong credentials could have affected this, as we have only ever used one. Regardless, thanks for the theories and good luck! @tgoyne This was a case I had originally considered as well, except that in that situation this would crash:
Since we're asking to add and not to update, if the key already existed in any capacity the result would be |
@nwainwright Thanks for the tip. I can check what we're doing with our app, though we did want to support background processing. @tgoyne Your point about the keychain lines up with what I was thinking. I was trying to see if perhaps there was a bad key being used with our users. I was originally thinking that, somehow, a new key was generated, but I had no way to replicate it to know. But your post gives me something to work on. If it truly can't get it from the keychain, then I might need to do a catch for that. |
We just realized what was the problem and I thought it might be helpful for you to know what we did in case it helps to fix your issue as well. Our scenario is the following one:
We were experiencing a random issue where the encription key failed to open the database. There was no usage pattern to reproduce it and it happened also in Production for some users. What we did is:
After all of those changes, there were still a few users having problems with database access. We realized there was a situation where the user was using iCloud to make backups of their phone but wasn't using iCloud Keychain. He/she changed or made a factory reset to his/her phone and restored the iCloud backup. Once the app was being started, Keychain returned nil for com.ourcompany.ourappname.databaseEncriptionKeyProd key and database failed to open (obviously). We added a specific control for this situation and created an empty database with a brand new encription key saved to the new Keychain. Hope it helps. |
So how you handle a new phone with iTunes backup and no iCloud Keychain turned on? New database? |
@cucamacuca Unfortunately the answer is "yes" at this point. We're looking for other solutions for the future like having a backup of encription keys remotely that can be downloaded using a second-factor authentication or something like that but there is no decision yet. |
Hi so the problem I have experienced is related to the source code that Realm provides for the generation of the key:
The problem is that SecItemAdd can fail if the keyChain is inacessible for some reason (device pass locked) so if this happens the keyChain will not save the encryptionKey thus creating a Realm Database with an encryptionKey that the app no longer can recover from, so the solution for this is verifying the result of status like so:
|
I had this same issue in some users and i was able to understand why and how to fix it. Take a look at this suggestion that i made to KeychainWrapper and see if your issue is related too: |
What the problem was: Solution:
PS : Old keys might not be accessible, so migrate to a new key perhaps. Thankyou guys & specially @TimOliver(we discussed this at iOSConfSG) for pointing in the right direction. No more annoying crashes! |
Yay! Glad I was able to help! 😁 |
➤ Lee Maguire commented: Looks like this has been solved. |
Background
Our iOS app is live with over one hundred thousand users. Of those, we have an issue happening with approximately 80 users. We're unable to reproduce this locally, but this is what we're able to piece together from Crashlytics.
Details
We store sensitive customer data in an encrypted Realm. To accomplish this, we have extended Realm with a static function that returns our encrypted Realm, or throws an error if it fails.
For 99% of users, this works fine. For less than 1%, we're seeing the "Realm file decryption failed" error. From investigating, it seems that this error is caused by trying to access a Realm with either the wrong key, or by trying to decrypt an unencrypted Realm.
Right now, with how we're generating the key, I'm inclined to think that that is not the issue.
Code Sample
We have a Realm extension with the following:
The first place Realm is hit in our app is via our User Settings manager. Simple version:
Expected results
While we accounted for the fact that something could go wrong, I was expecting to see errors pertaining to file I/O, disk space, etc.
Actual results
For less than 1% of our users, we're seeing "Realm file decryption failed". As all hits to Realm are happening through the
Realm.encryptedRealm()
function, I don't know how a non-encrypted Realm could be created. So I'm at a loss as to what might be going on.The Crashlytics trace has our
realm
property getting above, followed by:Looking up errors for that has shown me potentially issues with Realm, background threads, and the need to use
autoreleasepool
. But the error I'm catching before thefatalError
call seems to imply otherwise.Any guidance would be welcome at this point. Thanks.
Version of Realm
2.10.2 (Unable to upgrade at this time)
Xcode version: 9.2
iOS/OSX version:
Bug has been reported on iOS 10 and 11
Dependency manager + version: CocoaPods 1.4.0
The text was updated successfully, but these errors were encountered: