[PR #1246] [MERGED] Fix crash when using Yubikey via usb on Android #2808

Closed
opened 2025-11-26 23:24:22 -06:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/bitwarden/android/pull/1246
Author: @eliykat
Created: 1/29/2021
Status: Merged
Merged: 2/2/2021
Merged by: @eliykat

Base: masterHead: bugfix-android-usb-yubikey


📝 Commits (2)

  • 63b5ecf Fix crash when using yubikey via usb on Android
  • c3345d0 Fix crash when using usb keyboard on Android

📊 Changes

2 files changed (+6 additions, -1 deletions)

View changed files

📝 src/Android/MainActivity.cs (+3 -1)
📝 src/App/Pages/Accounts/TwoFactorPage.xaml.cs (+3 -0)

📄 Description

Objective

Fix #769 and #350. Inserting a Yubikey via USB on the 2FA page will close the 2FA page and revert the user back to the initial welcome page. See example here: https://youtu.be/bKYIPMlMJVU

This actually happens on any of the pre-login pages, but obviously the 2FA page is the biggest concern.

Cause of issue

Android apps use "Activity" objects to manage views and entry points into the app. Xamarin.Forms implements this by creating a single MainActivity to manage the app.

When certain configuration changes occur, Android rapidly destroys and recreates Activities to help them gracefully handle the configuration change. It is the app developer's responsibility to make sure that their Activities handle this lifecycle without loss of data or interruption to the user.

The problem is that plugging in a Yubikey or USB keyboard triggers a configuration change, which in turn destroys and recreats MainActivity. We evidently have nothing in place to handle this part of the lifecycle, so the app loses its state and reverts back to the entry page.

Code changes

  • MainActivity.cs - add Keyboard and KeyboardHidden events to the list of configuration changes that the app will handle itself without destroying and recreating the Activity.

    • Note: the official Android documentation says this is a "last resort" only because our app should handle its Activity lifecycle properly. However, we already use this option for configuration changes triggered by screen size and orientation, so I've done the same. It seems to "just work" but we may want to consider handling it properly.
  • TwoFactorPage.xaml.cs - fixed so that the app will focus on the Yubikey input element when the 2FA page loads. The app will not receive the Yubikey code unless this element has focus.

Testing considerations

Note: Yubikeys are recognised as USB keyboards.

Expected behaviour:

  • given the user is on the Yubikey 2FA page, the user should be able to plug in the Yubikey via USB, press the Yubikey's button, and be logged in without any further action required.
  • user should be able to insert a Yubikey or USB keyboard on any page within the app without changing the current page or causing any errors.

If we are adopting the solution proposed above, QA should try triggering various events related to the keyboard on different pages to make sure that they are still properly handled by the app. e.g. plugging/unplugging a physical keyboard, hiding/showing software keyboard, rotating to change the software keyboard dimensions.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/bitwarden/android/pull/1246 **Author:** [@eliykat](https://github.com/eliykat) **Created:** 1/29/2021 **Status:** ✅ Merged **Merged:** 2/2/2021 **Merged by:** [@eliykat](https://github.com/eliykat) **Base:** `master` ← **Head:** `bugfix-android-usb-yubikey` --- ### 📝 Commits (2) - [`63b5ecf`](https://github.com/bitwarden/android/commit/63b5ecf2e43b97da2b7c4eefc38828f48f02c392) Fix crash when using yubikey via usb on Android - [`c3345d0`](https://github.com/bitwarden/android/commit/c3345d04918bc35a456daa1383a91c1449d2a18f) Fix crash when using usb keyboard on Android ### 📊 Changes **2 files changed** (+6 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `src/Android/MainActivity.cs` (+3 -1) 📝 `src/App/Pages/Accounts/TwoFactorPage.xaml.cs` (+3 -0) </details> ### 📄 Description ## Objective Fix #769 and #350. Inserting a Yubikey via USB on the 2FA page will close the 2FA page and revert the user back to the initial welcome page. See example here: https://youtu.be/bKYIPMlMJVU This actually happens on any of the pre-login pages, but obviously the 2FA page is the biggest concern. ## Cause of issue Android apps use "Activity" objects to manage views and entry points into the app. Xamarin.Forms implements this by creating a single `MainActivity` to manage the app. When certain configuration changes occur, Android rapidly destroys and recreates Activities to help them gracefully handle the configuration change. It is the app developer's responsibility to make sure that their Activities handle this lifecycle without loss of data or interruption to the user. The problem is that plugging in a Yubikey or USB keyboard triggers a configuration change, which in turn destroys and recreats `MainActivity`. We evidently have nothing in place to handle this part of the lifecycle, so the app loses its state and reverts back to the entry page. ## Code changes * `MainActivity.cs` - add Keyboard and KeyboardHidden events to the list of configuration changes that the app will handle itself without destroying and recreating the Activity. * Note: [the official Android documentation says this is a "last resort" only](https://developer.android.com/guide/topics/manifest/activity-element.html#config) because our app should handle its Activity lifecycle properly. However, we already use this option for configuration changes triggered by screen size and orientation, so I've done the same. It seems to "just work" but we may want to consider handling it properly. * `TwoFactorPage.xaml.cs` - fixed so that the app will focus on the Yubikey input element when the 2FA page loads. The app will not receive the Yubikey code unless this element has focus. ## Testing considerations Note: Yubikeys are recognised as USB keyboards. Expected behaviour: * given the user is on the Yubikey 2FA page, the user should be able to plug in the Yubikey via USB, press the Yubikey's button, and be logged in without any further action required. * user should be able to insert a Yubikey or USB keyboard on any page within the app without changing the current page or causing any errors. If we are adopting the solution proposed above, QA should try triggering various events related to the keyboard on different pages to make sure that they are still properly handled by the app. e.g. plugging/unplugging a physical keyboard, hiding/showing software keyboard, rotating to change the software keyboard dimensions. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2025-11-26 23:24:22 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/android#2808