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

feat: add reset after failure, result.error after failure #865

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

juliansteenbakker
Copy link
Owner

fixes #853

Copy link

@michael-markl michael-markl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!
Sorry, I am quite busy these days... I hope, I'll get to testing it soon! The changes look promising though :)

@@ -31,51 +31,45 @@ public class FlutterSecureStorage {

private String sharedPreferencesName = DEFAULT_PREF_NAME;
private String preferencesKeyPrefix = DEFAULT_KEY_PREFIX;
private SharedPreferences encryptedPreferences;
private final SharedPreferences encryptedPreferences;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly, the constructor of FlutterSecureStorage should throw an error, if the initialization of encryptedPreferences fails. I believe, that this means, that encryptedPreferences cannot be null (see EncryptedSharedPreferences.create).
If so, we could/should maybe add the @NonNull annotation for encryptedPreferences here (and we can probably remove most null checks in class methods).

Copy link

@michael-markl michael-markl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this change and it solves the issue I described in #853. The Secure Storage is resetted when a backup+restore is done (even if the backup+restore was done on version 9.2.2 followed by updating the app to the version of this branch).

One thing that I noticed is that (if the backup+restore was done on version 9.2.2) the migration will continue to fail on the version of this branch; maybe we can even clean up these old keys if the migration failed due to the BadPaddingException (caused by the backup+restore):

01-19 14:54:52.373 10353 10353 E SecureStorageAndroid: Migration failed for key: VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIHNlY3VyZSBzdG9yYWdlCg_userCodesBase64
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at com.android.org.conscrypt.NativeCrypto.EVP_CipherFinal_ex(Native Method)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at com.android.org.conscrypt.OpenSSLEvpCipher.doFinalInternal(OpenSSLEvpCipher.java:152)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at com.android.org.conscrypt.OpenSSLCipher.engineDoFinal(OpenSSLCipher.java:374)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at javax.crypto.Cipher.doFinal(Cipher.java:2074)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at com.it_nomads.fluttersecurestorage.ciphers.h.a(SourceFile:36)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.a.c(SourceFile:8)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.a.i(SourceFile:81)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.a.g(SourceFile:7)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.a.<init>(SourceFile:23)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.e.d(SourceFile:20)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.e.c(SourceFile:39)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at P2.e.onAttachedToEngine(SourceFile:7)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at a3.b.c(SourceFile:116)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(SourceFile:50)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at java.lang.reflect.Method.invoke(Native Method)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at h3.a.a(SourceFile:22)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at Z2.i.E(SourceFile:10)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at app.entitlementcard.MainActivity.E(SourceFile:6)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at Z2.j.s(SourceFile:59)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at Z2.i.onCreate(SourceFile:14)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at app.entitlementcard.MainActivity.onCreate(SourceFile:4)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.Activity.performCreate(Activity.java:9002)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.Activity.performCreate(Activity.java:8980)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1526)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4030)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4235)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:112)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:174)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:109)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:81)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2636)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.os.Handler.dispatchMessage(Handler.java:107)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.os.Looper.loopOnce(Looper.java:232)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.os.Looper.loop(Looper.java:317)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at android.app.ActivityThread.main(ActivityThread.java:8705)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at java.lang.reflect.Method.invoke(Native Method)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
01-19 14:54:52.373 10353 10353 E SecureStorageAndroid:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
01-19 14:54:52.373 10353 10353 I SecureStorageAndroid: Failed to migrate 1 keys.

final SharedPreferences encryptedPreferences = initializeEncryptedSharedPreferencesManager(applicationContext);
migrateToEncryptedPreferences(encryptedPreferences);
return encryptedPreferences;
} catch (Exception e) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit unsure, if we should reset the storage on any exception; maybe, we should only reset it on exceptions where we know that the reset is absolutely necessary? (I think, javax.crypto.AEADBadTagException would be an example)

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

Successfully merging this pull request may close these issues.

v10.0.0-beta2: After Backup+Restore: Read and write fails without throwing exceptions
2 participants