Skip to content

Commit

Permalink
Fix bug where duplicate entries could be saved from Android AutoFill
Browse files Browse the repository at this point in the history
Could only reproduce on Android 14 but am unsure what could explain that so
probably also affected older Android versions sometimes too.
  • Loading branch information
luckyrat committed Mar 20, 2024
1 parent 12ee232 commit c79bdf7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/cubit/autofill_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class AutofillCubit extends Cubit<AutofillState> {
}

Future<void> finishSaving() async {
l.t('Autofillcubit.finishSaving');
if (state is AutofillSaving || state is AutofillSaved) {
emit(AutofillSaved((state as AutofillModeActive).androidMetadata));
await AutofillService().onSaveComplete();
Expand Down
2 changes: 2 additions & 0 deletions lib/cubit/entry_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class EntryCubit extends Cubit<EntryState> {
EntryCubit() : super(EntryInitial());

void startEditing(KdbxEntry entry, {bool startDirty = false}) {
l.t('EntryCubit.startEditing');
final newEntry = EditEntryViewModel.fromKdbxEntry(entry).let((it) => startDirty ? it.copyWith(isDirty: true) : it);
emit(EntryLoaded(newEntry));
}

void endEditing(KdbxEntry? entry) {
l.t('EntryCubit.endEditing');
if (entry != null) {
(state as EntryLoaded).entry.commit(entry);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/cubit/vault_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ class VaultCubit extends Cubit<VaultState> {
final rootGroup = vault.files.current.body.rootGroup;
initAutofillPersistentQueue(rootGroup.uuid.uuidUrlSafe);
if (isAutofilling()) {
// Make sure that any previous autofill operations have their adjustments
// applied before we present the entry list or entry saving interface to the user.
await _persistentQueueAfAssociations?.ready;
final pendingItems = (await _persistentQueueAfAssociations?.toList());
if (pendingItems != null) {
Expand Down Expand Up @@ -1272,6 +1274,7 @@ class VaultCubit extends Cubit<VaultState> {
KdbxEntry createEntry({
required KdbxGroup group,
}) {
l.t('VaultCubit.createEntry');
final destinationGroup = group;
final entry = KdbxEntry.create(currentVaultFile!.files.current, destinationGroup);
destinationGroup.addEntry(entry);
Expand Down
6 changes: 6 additions & 0 deletions lib/widgets/autofill_save.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:keevault/cubit/vault_cubit.dart';
import 'package:keevault/extension_methods.dart';
import 'package:keevault/widgets/loading_spinner.dart';
import 'package:matomo_tracker/matomo_tracker.dart';
import '../logging/logger.dart';
import 'coloured_safe_area_widget.dart';
import 'entry.dart';

Expand All @@ -28,6 +29,7 @@ class _AutofillSaveWidgetState extends State<AutofillSaveWidget> with TraceableC
KdbxEntry? newEntry;
@override
void initState() {
l.t('_AutofillSaveWidgetState.initState');
super.initState();
final autofillState = BlocProvider.of<AutofillCubit>(context).state as AutofillModeActive;
final vaultCubit = BlocProvider.of<VaultCubit>(context);
Expand All @@ -39,6 +41,10 @@ class _AutofillSaveWidgetState extends State<AutofillSaveWidget> with TraceableC
final password = autofillState.androidMetadata.saveInfo!.password;
//TODO:f: expose value of save compat mode to Entry widget:
//final isCompatMode = autofillState.androidMetadata.saveInfo!.isCompatMode;

// We'll get duplicate entries if multiple instances of this widget are built.
// Should move the business logic to somewhere else if possible and have this
// widget just show whatever new entry has been supplied to it (e.g. by the cubit).
setState(() {
newEntry = vaultCubit.createEntry(group: vault.files.current.body.rootGroup);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/kee_vault_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class KeeVaultAppState extends State<KeeVaultApp> with WidgetsBindingObserver, T
RemoteVaultRepository(userService, storageService),
LocalVaultRepository(quickUnlocker),
entryCubit,
autofillCubit.isAutofilling,
() => autofillCubit.isAutofilling() || autofillCubit.isAutofillSaving(),
generatorProfilesCubit,
accountCubit,
)),
Expand Down
6 changes: 5 additions & 1 deletion lib/widgets/vault.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ class _VaultWidgetState extends State<VaultWidget> with WidgetsBindingObserver {

Future<void> _refresh() async {
final user = BlocProvider.of<AccountCubit>(context).currentUserIfKnown;
if (user == null) {
return;
}
final AutofillState autofillState = BlocProvider.of<AutofillCubit>(context).state;
if (autofillState is AutofillModeActive || user == null) {
if (autofillState is AutofillModeActive) {
l.t('Skip refresh due to state: ${autofillState.runtimeType}');
return;
}
await BlocProvider.of<VaultCubit>(context).refresh(user);
Expand Down

0 comments on commit c79bdf7

Please sign in to comment.