Closed c01nd01r closed 11 months ago
Thanks for the report, you uncovered a few bugs, although the actual bugs are not quite what you pointed out. Working on a fix.
v5.0.0 has just been released. Actually the only actual bug was that decryptString
was throwing an error instead of returning null
. The leftover notFound
stuff was not actually a bug, just unused code that has been removed.
I changed the plugin to throw an actual instance of StorageError, and StorageErrorType is now a string enum, hence the bump to v5.
Awesome! Thank you so much!
Bug report
Capacitor version
Platform(s)
Android 13 iOS(?)
Current behavior
The plugin returns an error object that contains empty strings for the
code
andmessage
fields.Expected behavior
The plugin should return
null
if it is unable to retrieve data for the specified key.Code reproduction
Hard to reproduce
Other technical details
npm --version
output: 10.1.0node --version
output: v20.8.1pod --version
output (iOS issues only): 1.13.0Additional context
During the development of the application, after numerous installations and uninstallations of the app on a real Android device, I found that when attempting to retrieve data by key, I receive an empty error message from Capacitor native platform.
I decided to investigate the issue and found that the error occurs in the call to the
decryptString()
method, specifically in the condition check that triggers the throwing of theKeyStoreException(KeyStoreException.ErrorKind.notFound, prefixedKey)
exception (lines 279-281 in the SecureStorage.java file).https://github.com/aparajita/capacitor-secure-storage/blob/7ece5065831e8e4fb58f4aefde46993d33906456/android/src/main/java/com/aparajita/capacitor/securestorage/SecureStorage.java#L279-L281
According to the commit history in the repository, the error of type
ErrorKind.notFound
was replaced with anull
return value (commit 0f2491a, feat: no blowfish, return null).capacitor-secure-storage/android/src/main/java/com/aparajita/capacitor/securestorage/KeyStoreException.java
However, in my situation, an exception was thrown in a different part of the source code. As I understand it, the empty error is returned because the error description corresponding to the
ErrorKind.notFound
type was not found in theerrorMap
field of the KeyStoreException object.https://github.com/aparajita/capacitor-secure-storage/blob/7ece5065831e8e4fb58f4aefde46993d33906456/android/src/main/java/com/aparajita/capacitor/securestorage/KeyStoreException.java#L8-L17
This description was removed in the commit 0f2491a.
And one more thing. I noticed that in the constructor
init
of theKeyStoreException
error object, there is a call to thetoString
method onErrorKind
. I may be mistaken (I'm not a Java developer), but according to the documentation, callingtoString
on anenum
should return the name of the field (for example,notFound
), not the numeric code of the specified enum.https://github.com/aparajita/capacitor-secure-storage/blob/7ece5065831e8e4fb58f4aefde46993d33906456/android/src/main/java/com/aparajita/capacitor/securestorage/KeyStoreException.java#L63
I also noticed that in the plugin code for iOS, there is also the usage of
ErrorKind.notFound
. Perhaps this issue is relevant for iOS as well? Unfortunately, I cannot confirm this.https://github.com/aparajita/capacitor-secure-storage/blob/7ece5065831e8e4fb58f4aefde46993d33906456/ios/Plugin/KeychainError.swift#L12