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

Auth/PM-7811 - Refactor User Auto Unlock Key Hydration Process To Remove Race Conditions #8979

Conversation

JaredSnider-Bitwarden
Copy link
Contributor

@JaredSnider-Bitwarden JaredSnider-Bitwarden commented Apr 29, 2024

Type of change

- [X] Bug fix
- [ ] New feature development
- [ ] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other

Objective

To resolve PM-7811 which initially presented itself as the CLI not properly unlocking after exporting a session key on a fresh install or after deleting data.json. During our investigation, we realized there was a cross client race condition with any code that called to check if the user had a user key in memory and the UserKeyInitService's listener for setting the user key in memory if the user auto unlock key was set. This refactor should eliminate any chances of race conditions and ensure that the user key in memory is always set as early as possible in the client initialization processes.

Code changes

  • Renamed UserKeyInitService to UserAutoUnlockKeyService as it is now purely involved with managing the user auto unlock key.
  • Updated UserAutoUnlockKeyService dependencies
  • apps/web/src/app/core/init.service.ts: Added userAutoUnlockKeyService.setUserKeyInMemoryIfAutoUserKeySet on Web client init for singular active account
  • apps/desktop/src/app/services/init.service.ts: Added userAutoUnlockKeyService.setUserKeyInMemoryIfAutoUserKeySet on Desktop client init for n accounts (desktop supports account switching)
  • apps/browser/src/background/main.background.ts: Added userAutoUnlockKeyService.setUserKeyInMemoryIfAutoUserKeySet on Browser Extension client init for n accounts (browser extension supports account switching)
  • apps/cli/src/bw.ts: Added userAutoUnlockKeyService.setUserKeyInMemoryIfAutoUserKeySet on CLI client init for singular active account which executes pre-command execution.

Screenshots

Web

PM-7811.-.Web.-.never.lock.works.mov

CLI

PM-7811 - CLI - never lock works

Desktop

PM-7811.-.Desktop.-.never.lock.works.on.init.and.for.account.switch.mov

Browser Extension

PM-7811.-.Browser.Extension.-.never.lock.works.on.init.and.for.account.switch.mov

Before you submit

  • Please add unit tests where it makes sense to do so (encouraged but not required)
  • If this change requires a documentation update - notify the documentation team
  • If this change has particular deployment requirements - notify the DevOps team
  • Ensure that all UI additions follow WCAG AA requirements

…emove active account listening logic as it introduced race conditions with user key memory retrieval happening before the user auto unlock key was set into memory.
… if there is an active account, then set the user key in memory from the user auto unlock key.
… key in memory if the auto unlock key is set on account switch and background init (must act on all accounts so that account switcher displays unlock status properly).
@github-actions github-actions bot added the needs-qa Marks a PR as requiring QA approval label Apr 29, 2024
Copy link

codecov bot commented Apr 29, 2024

Codecov Report

Attention: Patch coverage is 29.26829% with 29 lines in your changes are missing coverage. Please review.

Project coverage is 27.70%. Comparing base (6365dcd) to head (7251db3).
Report is 3 commits behind head on main.

Files Patch % Lines
apps/desktop/src/app/services/init.service.ts 0.00% 10 Missing ⚠️
apps/browser/src/background/main.background.ts 0.00% 7 Missing ⚠️
apps/cli/src/bw.ts 0.00% 6 Missing ⚠️
apps/web/src/app/core/init.service.ts 37.50% 3 Missing and 2 partials ⚠️
libs/angular/src/services/jslib-services.module.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8979      +/-   ##
==========================================
- Coverage   27.70%   27.70%   -0.01%     
==========================================
  Files        2392     2392              
  Lines       69356    69368      +12     
  Branches    12934    12938       +4     
==========================================
- Hits        19218    19215       -3     
- Misses      48658    48673      +15     
  Partials     1480     1480              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@JaredSnider-Bitwarden JaredSnider-Bitwarden marked this pull request as ready for review April 29, 2024 20:20
Copy link
Contributor

github-actions bot commented Apr 29, 2024

Logo
Checkmarx One – Scan Summary & Details39f58599-9138-4a02-b7a0-611f8d40552b

No New Or Fixed Issues Found

Copy link
Member

@justindbaur justindbaur left a comment

Choose a reason for hiding this comment

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

Just one question

@JaredSnider-Bitwarden JaredSnider-Bitwarden merged commit 20de053 into main Apr 29, 2024
60 of 63 checks passed
@JaredSnider-Bitwarden JaredSnider-Bitwarden deleted the auth/pm-7811/refactor-user-auto-unlock-key-hydration-process-to-remove-race-conditions branch April 29, 2024 21:43
JaredSnider-Bitwarden added a commit that referenced this pull request Apr 29, 2024
…ove Race Conditions (#8979)

* PM-7811 - Refactor UserKeyInitService to UserAutoUnlockKeyService - remove active account listening logic as it introduced race conditions with user key memory retrieval happening before the user auto unlock key was set into memory.

* PM-7811 - CLI - (1) Fix deps (2) On CLI init (pre command execution), if there is an active account, then set the user key in memory from the user auto unlock key.

* PM-7811 - Browser Extension / desktop - (1) Update deps (2) Sets user key in memory if the auto unlock key is set on account switch and background init (must act on all accounts so that account switcher displays unlock status properly).

* PM-7811 - Web - (1) Update deps (2) Sets user key in memory if the auto unlock key is set on init

* PM-7811 - Fix account switcher service changes not being necessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-qa Marks a PR as requiring QA approval
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants