mirror of
https://github.com/bitwarden/browser
synced 2025-12-20 10:13:31 +00:00
Auth/PM-7811 - Refactor User Auto Unlock Key Hydration Process To Remove 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.
This commit is contained in:
@@ -113,7 +113,7 @@ import { MemoryStorageService } from "@bitwarden/common/platform/services/memory
|
||||
import { MigrationBuilderService } from "@bitwarden/common/platform/services/migration-builder.service";
|
||||
import { MigrationRunner } from "@bitwarden/common/platform/services/migration-runner";
|
||||
import { SystemService } from "@bitwarden/common/platform/services/system.service";
|
||||
import { UserKeyInitService } from "@bitwarden/common/platform/services/user-key-init.service";
|
||||
import { UserAutoUnlockKeyService } from "@bitwarden/common/platform/services/user-auto-unlock-key.service";
|
||||
import { WebCryptoFunctionService } from "@bitwarden/common/platform/services/web-crypto-function.service";
|
||||
import {
|
||||
ActiveUserStateProvider,
|
||||
@@ -334,7 +334,7 @@ export default class MainBackground {
|
||||
billingAccountProfileStateService: BillingAccountProfileStateService;
|
||||
// eslint-disable-next-line rxjs/no-exposed-subjects -- Needed to give access to services module
|
||||
intraprocessMessagingSubject: Subject<Message<object>>;
|
||||
userKeyInitService: UserKeyInitService;
|
||||
userAutoUnlockKeyService: UserAutoUnlockKeyService;
|
||||
scriptInjectorService: BrowserScriptInjectorService;
|
||||
kdfConfigService: kdfConfigServiceAbstraction;
|
||||
|
||||
@@ -1064,11 +1064,7 @@ export default class MainBackground {
|
||||
}
|
||||
}
|
||||
|
||||
this.userKeyInitService = new UserKeyInitService(
|
||||
this.accountService,
|
||||
this.cryptoService,
|
||||
this.logService,
|
||||
);
|
||||
this.userAutoUnlockKeyService = new UserAutoUnlockKeyService(this.cryptoService);
|
||||
}
|
||||
|
||||
async bootstrap() {
|
||||
@@ -1079,7 +1075,18 @@ export default class MainBackground {
|
||||
|
||||
// This is here instead of in in the InitService b/c we don't plan for
|
||||
// side effects to run in the Browser InitService.
|
||||
this.userKeyInitService.listenForActiveUserChangesToSetUserKey();
|
||||
const accounts = await firstValueFrom(this.accountService.accounts$);
|
||||
|
||||
const setUserKeyInMemoryPromises = [];
|
||||
for (const userId of Object.keys(accounts) as UserId[]) {
|
||||
// For each acct, we must await the process of setting the user key in memory
|
||||
// if the auto user key is set to avoid race conditions of any code trying to access
|
||||
// the user key from mem.
|
||||
setUserKeyInMemoryPromises.push(
|
||||
this.userAutoUnlockKeyService.setUserKeyInMemoryIfAutoUserKeySet(userId),
|
||||
);
|
||||
}
|
||||
await Promise.all(setUserKeyInMemoryPromises);
|
||||
|
||||
await (this.i18nService as I18nService).init();
|
||||
(this.eventUploadService as EventUploadService).init(true);
|
||||
|
||||
Reference in New Issue
Block a user