Kunzisoft / KeePassDX

Lightweight vault and password manager for Android, KeePassDX allows editing encrypted data in a single file in KeePass format and fill in the forms in a secure way.
https://www.keepassdx.com/
GNU General Public License v3.0
4.69k stars 276 forks source link

'Could not load your database.' error #1096

Closed johnd0e closed 3 years ago

johnd0e commented 3 years ago

I keep my database on Google Drive. And (from time to time) KeePassDX has difficulties to open it on start

Could not load your database. java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.docs.storagebackend.StorageBackendContentProvider from ProcessRecord{7ccec12 22497:com.kunzisoft.keepass.free/u0a401} (pid=22497, uid=10401) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
J-Jamet commented 3 years ago

Please fill in the bug template correctly with the version of the applications used and be more specific in describing the problem conditions. Linked to #1066 #1067 #1071 #1094

johnd0e commented 3 years ago

Describe the bug I keep my database on Google Drive. Sometimes (not often) when starting KeePassDX I get such error:

Could not load your database. java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.docs.storagebackend.StorageBackendContentProvider from ProcessRecord{7ccec12 22497:com.kunzisoft.keepass.free/u0a401} (pid=22497, uid=10401) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

After that my database disappers from the list, but I am able to readd it again in usual way (picking in filemanager).

To Reproduce Steps to reproduce the behavior:

  1. Start KeePassDX from launcher
  2. See toast with error

Expected behavior Either no error, or some clear and descriptive dialog.

KeePass Database

KeePassDX (please complete the following information):

Android (please complete the following information):

Additional context I'm also using KeePass 2.48.1 on my Windows machine, with local database, syncing it with GDrive using KPSync For Google Drive 4.0.7.0 plugin.

J-Jamet commented 3 years ago

Thanks for formatting the issue. This behavior of Google Drive has already been reported in the wiki. The exception is raised when the content provider of Google Drive is no longer able to provide the rights to the file. I think the file is invalidated for safety because it has not finished synchronizing internally. As you say, it is enough to reselect the file from its location.

https://github.com/Kunzisoft/KeePassDX/wiki/File-Manager-and-Sync#compatibility https://github.com/Kunzisoft/KeePassDX/discussions/1089

johnd0e commented 3 years ago

This behavior of Google Drive has already been reported in the wiki.

I do not feel that it is enough to file it in the wiki. If this exception is known and expected - then you could display user-friendly dialog with "reopen" button instead of brief and weird toast.

Also from your explanation I haven't figured out if your code makes use of persist permissions: https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions

J-Jamet commented 3 years ago

If this exception is known and expected - then you could display user-friendly dialog with "reopen" button instead of brief and weird toast.

This is not an expected behavior because by definition it is an exception.

Also from your explanation I haven't figured out if your code makes use of persist permissions: https://developer.android.com/training/data-storage/shared/documents-files#persist-permissions

https://github.com/Kunzisoft/KeePassDX/blob/82450c0ae8d21f0ad7a3919ac4c6b26fa090138e/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt#L114 https://github.com/Kunzisoft/KeePassDX/wiki/File-Manager-and-Sync#why-disparate-compatibility

johnd0e commented 3 years ago

This is not an expected behavior because by definition it is an exception.

If some operation is known to be exception-prone, then it is possible to process exceptions and present them in user-friendly form. I mean some dialog, which would contain a hint what was wrong and how to fix that (e.g. adding "Reopen datavase" button right to that dialog).

J-Jamet commented 3 years ago

But this exception (java.lang.SecurityException: Permission Denial) should not happen, adding a dialog is just a workaround.

I don't know why this is happening to you for the database URI but the only cases I've referenced is an already fixed problem related to key file permissions. What I put in the related issues #1066 #1067 #1071 #1094

Either you have used the 3.0.0 beta version of KeePassDX (#1066) with a key file and you only need to reselect the file once with the stable version 3.0.0 to solve the problem and you made a mistake in writing the issue.

Either as you describe, it comes from the main URI of the database from Google Drive, so it's a problem in the file manager. And I'm not going to make a specific dialog for each error case without being able to test all the errors that I don't even know exist due to the unwanted behavior of external applications...

If this exception happens for the main database URI it means that the file permissions have been released in another way and not by KeePassDX so it's up to the application that releases the URI to know what it's doing.

Displaying the exception in a dialog or in a snackbar does not change the use of the application, is only a change of view and does not solve a problem. A reopen database button assumes that an exception is normal and will not work in the majority of exceptions. Normally you should not even be able to reopen the database if there is this Permission Denialexception because it clearly means that there is no permission allowed!

I added the URI check on purpose to know if it was possible to open the database or not. Maybe the exception is raised in your case because Google Drive has not finished synchronizing your file in its cache, I don't know and KeePassDX doesn't have to know, just ask if it has the right or not. If Google Drive answers no but actually has the permissions, I can't do anything.

So I'm not going to counteract unintended behavior of external file provider with weird tricks based on URIs.