braintree / braintree_android

Braintree SDK for Android
https://developer.paypal.com/braintree/docs/start/hello-client/android/v4
MIT License
409 stars 233 forks source link

Crash SharedPrefKeysetReader #604

Closed DmitriiBroli closed 1 year ago

DmitriiBroli commented 2 years ago

General information

Issue description

Crashlytics crash

java.io.FileNotFoundException: can't read keyset; the pref value __androidx_security_crypto_encrypted_prefs_value_keyset__ does not exist
        at xl6.b(SharedPrefKeysetReader.java:33)
        at xl6.a(SharedPrefKeysetReader.java:1)
        at ee3.j(KeysetHandle.java:1)
        at gd$b.e(AndroidKeysetManager.java:7)
        at gd$b.f(AndroidKeysetManager.java:1)
        at gd$b.d(AndroidKeysetManager.java:12)
        at gt1.b(EncryptedSharedPreferences.java:97)
        at gt1.a(EncryptedSharedPreferences.java:5)
        at e30.d(BraintreeSharedPreferences.java:22)
        at e30.a(BraintreeSharedPreferences.java:1)
        at pr0.b(ConfigurationCache.java:20)
        at pr0.a(ConfigurationCache.java:5)
        at tr0.c(ConfigurationLoader.java:7)
        at tr0.d(ConfigurationLoader.java:49)
        at n20$a.a(BraintreeClient.java:17)
        at qo$a.b(AuthorizationLoader.java:19)
        at kv.resumeWith(ContinuationImpl.kt:12)
        at sk1.run(DispatchedTask.kt:119)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
scannillo commented 2 years ago

👋 Hello @DmitriiBroli - do you have steps to replicate?

DmitriiBroli commented 2 years ago

@scannillo Hi, no steps unfortunately, this is a Crashlytics crash.

sshropshire commented 2 years ago

Hi @DmitriiBroli we have an issue with our 3rd party MPI provider Cardinal that prevents us from providing explicit support for API version 33. I'm guessing this issue may be related, although it is hard to tell without steps. We appreciate the stack trace though that is helpful.

Is the crash frequent? Also you can follow along with #585 once this is resolved we'll release a new minor version update to include explicit support for Android 13.

jaxdesmarais commented 1 year ago

Hello @DmitriiBroli -

We believe this has been fixed by our 3rd Party MPI Cardinal. The fix has been released in version 4.18.0. Are you able to test again as we were not able to reproduce this after updating and confirm if you are seeing this issue resolved?

DmitriiBroli commented 1 year ago

Hi, the crash still persists. Additionally, we also noticed StrictMode violations from braintree side:

D/StrictMode: StrictMode policy violation; ~duration=113 ms: android.os.strictmode.DiskReadViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1596)
        at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:275)
        at android.app.SharedPreferencesImpl.edit(SharedPreferencesImpl.java:368)
        at com.google.crypto.tink.integration.android.SharedPrefKeysetWriter.<init>(SharedPrefKeysetWriter.java:56)
        at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.withSharedPref(AndroidKeysetManager.java:163)
        at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:153)
        at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
        at com.braintreepayments.api.BraintreeSharedPreferences.getSharedPreferences(BraintreeSharedPreferences.java:34)
        at com.braintreepayments.api.BraintreeSharedPreferences.containsKey(BraintreeSharedPreferences.java:85)
        at com.braintreepayments.api.ConfigurationCache.getConfiguration(ConfigurationCache.java:40)
        at com.braintreepayments.api.ConfigurationCache.getConfiguration(ConfigurationCache.java:34)
        at com.braintreepayments.api.ConfigurationLoader.getCachedConfiguration(ConfigurationLoader.java:75)
        at com.braintreepayments.api.ConfigurationLoader.loadConfiguration(ConfigurationLoader.java:39)
        at com.braintreepayments.api.BraintreeClient$1.onAuthorizationResult(BraintreeClient.java:183)
        at com.braintreepayments.api.AuthorizationLoader$1.onSuccess(AuthorizationLoader.java:26)
sshropshire commented 1 year ago

@DmitriiBroli thank you for your patience. We have a PR up. After looking at this stack overflow post, it looks like we may benefit from explicitly providing an encryption key alias. Once we have it merged it, it may help us if you could verify that our SNAPSHOT build fixes the issue and if so, we can add it to the next release.

sshropshire commented 1 year ago

Hey @DmitriiBroli can you try version 4.19.0 to see if this resolves the issue? Also in regards to the StrictMode violations, we were notified that the Jetpack Security Crypto library has restarted active development recently. We will make updates to this dependency when possible.

DmitriiBroli commented 1 year ago

@sshropshire Thank you for your time. We have updated to 4.19.0 and we will check out whether this helped after releasing to prod.

jcnoriega commented 1 year ago

@DmitriiBroli issue has been fixed in latest version 4.20.0. In order to avoid a crash, you need to catch BraintreeSharedPreferencesException. Let me know if it works and i'll close this issue!