flutter_secure_storage: FlutterSecureStorage.read broken on Android 9.0 Samsung only
I just got Android 9.0 pie on my phone (physical not virtual). Everything was working fine and then once I downloaded 9.0 this error occurred. My co-worker has an Android but not a Samsung and it works fine but my Samsung produces this error when trying to read from secure storage. (Writing to secure storage doesn’t seem to be an issue). Possibly locked keychain? Not sure if this fix is local or if anyone else experiences this issue.
E/flutter (31692): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception: E/flutter (31692): PlatformException(error, Unsupported value: javax.crypto.BadPaddingException: error:1e000065:Cipher functions:OPENSSL_internal:BAD_DECRYPT, null) E/flutter (31692): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:551:7) E/flutter (31692): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:292:18) E/flutter (31692): <asynchronous suspension> E/flutter (31692): #2 FlutterSecureStorage.read (package:flutter_secure_storage/flutter_secure_storage.dart:16:24) E/flutter (31692): <asynchronous suspension> E/flutter (31692): #3 main (package:phone_app/main.dart:27:11) E/flutter (31692): #4 _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:289:19) E/flutter (31692): #5 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 8
- Comments: 17 (3 by maintainers)
Commits related to this issue
- don't back up data or app contents - 1 because it should not be needed and 2 because of https://github.com/mogol/flutter_secure_storage/issues/43 — committed to pseudorand-dev/nullpass-mobile by irasekh3 4 years ago
- Configure backup rules for flutter_secure_storage https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-674412687 — committed to tommy351/eh-redux by tommy351 4 years ago
- Configure backup rules for flutter_secure_storage https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-674412687 — committed to tommy351/eh-redux by tommy351 4 years ago
- Add Device Syncing Ability (#6) * update 'cached_network_image' to work with latest version of flutter * setup OneSignal library and dependency for Android * add support for getting notifications a... — committed to pseudorand-dev/nullpass-mobile by irasekh3 3 years ago
- fix securestorage usage backing up to drive apparently causes problems see https://github.com/mogol/flutter_secure_storage/issues/43 — committed to miDeb/digitales_register by miDeb 4 years ago
- implemented the solution suggested in https://github.com/mogol/flutter_secure_storage/issues/43 for flutter_secure_storage package — committed to ercross/pgsk by ercross 3 years ago
- feat: 181 - add backup rules so that encrypted data is not backedup see https://github.com/mogol/flutter_secure_storage/issues/43 — committed to SocialGouv/pass_emploi_app by deleted user 3 years ago
- feat: 181 - add backup rules so that encrypted data is not backedup see https://github.com/mogol/flutter_secure_storage/issues/43 — committed to SocialGouv/pass_emploi_app by deleted user 3 years ago
- fix: bug with secure storage https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-471642126 — committed to AgoraDesk-LocalMonero/agoradesk-app-foss by sergdeus 2 years ago
- fix: bug with secure storage https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-471642126 — committed to AgoraDesk-LocalMonero/agoradesk-app-foss by sergdeus 2 years ago
- fix(android): Clear secure storage on exception See flutter_secure_storage issue: https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-1326487020 — committed to Cavatina/Music-Scool-App by kbn 2 years ago
Yep, I think you’re right. My sharedPrefs was weird as well. I solved this by adding
on AndroidManifest.xml. So it can stop restoring stuff.
If you need
fullBackupContent="yes"
, you can disable backup of prefs used by the plugin.Sorry it’s unclear to me. Why would I need to set both
android:allowBackup="false"
andandroid:fullBackupContent="false"
?According to the Android doc, the latter is used to configure which data should be include/exclude when
allowBackup
is enabled, right?So, if I understand correctly, there are 2 viable solutions:
Option 1: Disable backup completely:
Option 2: Keep backup enable but exclude the shared pref used by this plugin:
and this file
res/xml/backup_rules.xml
:Am I correct?
I had two users w/ this problem and I did both recommendations from above:
and this:
and this file res/xml/backup_rules.xml:
And the issue was resolved
This worked for me. However, a fresh install was needed to make it work.
When choosing to exclude the shared preferences file from backup (
Option 2
of https://github.com/mogol/flutter_secure_storage/issues/43#issuecomment-674412687), now there is an additional XML file which must? be defined, if the app targets Android 12 or higher: https://developer.android.com/guide/topics/data/autobackup#include-exclude-android-12As the doc says for the Android 11 and lower way:
This means one more attribute for the
<application>
tag in the manifest and one more XML file.In AndroidManifest.xml:
Create
res/xml/data_extraction_rules.xml
:Not sure if the
<include>
tag is needed, maybe the<exclude>
is enough. I grabbed it from the linked Android docs.Currently Flutter
3.3.9
definestargetSdkVersion
as31
, so the above may be required for new apps: https://github.com/flutter/flutter/blob/3.3.9/packages/flutter_tools/gradle/flutter.gradle#L39Of course disabling backup by setting
android:allowBackup="false"
is still a valid option. In that case the above shouldn’t be done.Well… I kind of got it working trying some weird things. In the screen you can see where’s my problem, maybe its yours too. To solve I just called “.deleteAll();” (or _sotrage.deleteAll(); following the example provided by the autor). I just call it at app’s first run using a “SharedPreferences _firstRun” variable.
I think its something with the keys recovery…idk Oh, I also tryed to solve it by configuring android:allowBackup and it did nothing…
UPDATE I could fix my problem in two steps: 1: Add this to the manifest:
2: Call .deleteAll(); in my code only once to clean everything.
After this everything got working again (no need for sharedPreffs stuff I mention above and no need to keep .deleteAll(); in the code).
You can also catch the error and erase the failing value.
For my app, it seems like once a user runs into this issue, they will not get out of it simply by the addition of the manifest changes. Trying to communicate with this random app user is also not easy to try to get them to uninstall and reinstall.
My question is: Can I catch this exception in Flutter-space and handle it more gracefully?
I saw a comment somewhere among the discussions here on Github that suggested catching the exception and then running deleteAll(). Would this work? I ask because I cannot reproduce this locally. I can only debug this by releasing a new version and then watching the error reporting service for a day or two waiting for results from the handful of users that have run into this error.
Something like: