1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 00:03:56 +00:00

Retrieve auth status when updating overlay ciphers. (#9282)

* Retrieve auth status when updating overlay ciphers.

We are experiencing a hang due to e8ed4f38f4/apps/browser/src/background/main.background.ts (L1218) and the fact that the auth status is not updated during account switch for this service. Ideally, the service would just use latest everywhere, but this is sufficient for this bug fix.

Account-switcher changes avoid multiple updates for the same event.

* Avoid loop

* Test fixes

Co-authored-by: Cesar Gonzalez <cesar.a.gonzalezcs@gmail.com>

---------

Co-authored-by: Cesar Gonzalez <cesar.a.gonzalezcs@gmail.com>
This commit is contained in:
Matt Gibson
2024-05-21 10:46:46 -04:00
committed by GitHub
parent 00db087cee
commit dff44b02e2
4 changed files with 13 additions and 9 deletions

View File

@@ -153,7 +153,7 @@ describe("AccountSwitcherService", () => {
await selectAccountPromise; await selectAccountPromise;
expect(accountService.switchAccount).toBeCalledWith(null); expect(messagingService.send).toHaveBeenCalledWith("switchAccount", { userId: null });
expect(removeListenerSpy).toBeCalledTimes(1); expect(removeListenerSpy).toBeCalledTimes(1);
}); });
@@ -176,7 +176,7 @@ describe("AccountSwitcherService", () => {
await selectAccountPromise; await selectAccountPromise;
expect(accountService.switchAccount).toBeCalledWith("1"); expect(messagingService.send).toHaveBeenCalledWith("switchAccount", { userId: "1" });
expect(messagingService.send).toBeCalledWith( expect(messagingService.send).toBeCalledWith(
"switchAccount", "switchAccount",
matches((payload) => { matches((payload) => {

View File

@@ -134,7 +134,6 @@ export class AccountSwitcherService {
const switchAccountFinishedPromise = this.listenForSwitchAccountFinish(userId); const switchAccountFinishedPromise = this.listenForSwitchAccountFinish(userId);
// Initiate the actions required to make account switching happen // Initiate the actions required to make account switching happen
await this.accountService.switchAccount(userId);
this.messagingService.send("switchAccount", { userId }); // This message should cause switchAccountFinish to be sent this.messagingService.send("switchAccount", { userId }); // This message should cause switchAccountFinish to be sent
// Wait until we receive the switchAccountFinished message // Wait until we receive the switchAccountFinished message

View File

@@ -1,4 +1,4 @@
import { mock, mockReset } from "jest-mock-extended"; import { mock, MockProxy, mockReset } from "jest-mock-extended";
import { BehaviorSubject, of } from "rxjs"; import { BehaviorSubject, of } from "rxjs";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status"; import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
@@ -62,7 +62,8 @@ describe("OverlayBackground", () => {
let overlayBackground: OverlayBackground; let overlayBackground: OverlayBackground;
const cipherService = mock<CipherService>(); const cipherService = mock<CipherService>();
const autofillService = mock<AutofillService>(); const autofillService = mock<AutofillService>();
const authService = mock<AuthService>(); let activeAccountStatusMock$: BehaviorSubject<AuthenticationStatus>;
let authService: MockProxy<AuthService>;
const environmentService = mock<EnvironmentService>(); const environmentService = mock<EnvironmentService>();
environmentService.environment$ = new BehaviorSubject( environmentService.environment$ = new BehaviorSubject(
@@ -94,6 +95,9 @@ describe("OverlayBackground", () => {
beforeEach(() => { beforeEach(() => {
domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider); domainSettingsService = new DefaultDomainSettingsService(fakeStateProvider);
activeAccountStatusMock$ = new BehaviorSubject(AuthenticationStatus.Unlocked);
authService = mock<AuthService>();
authService.activeAccountStatus$ = activeAccountStatusMock$;
overlayBackground = new OverlayBackground( overlayBackground = new OverlayBackground(
cipherService, cipherService,
autofillService, autofillService,
@@ -166,11 +170,11 @@ describe("OverlayBackground", () => {
}); });
beforeEach(() => { beforeEach(() => {
overlayBackground["userAuthStatus"] = AuthenticationStatus.Unlocked; activeAccountStatusMock$.next(AuthenticationStatus.Unlocked);
}); });
it("ignores updating the overlay ciphers if the user's auth status is not unlocked", async () => { it("ignores updating the overlay ciphers if the user's auth status is not unlocked", async () => {
overlayBackground["userAuthStatus"] = AuthenticationStatus.Locked; activeAccountStatusMock$.next(AuthenticationStatus.Locked);
jest.spyOn(BrowserApi, "getTabFromCurrentWindowId"); jest.spyOn(BrowserApi, "getTabFromCurrentWindowId");
jest.spyOn(cipherService, "getAllDecryptedForUrl"); jest.spyOn(cipherService, "getAllDecryptedForUrl");

View File

@@ -136,7 +136,8 @@ class OverlayBackground implements OverlayBackgroundInterface {
* list of ciphers if the extension is not unlocked. * list of ciphers if the extension is not unlocked.
*/ */
async updateOverlayCiphers() { async updateOverlayCiphers() {
if (this.userAuthStatus !== AuthenticationStatus.Unlocked) { const authStatus = await firstValueFrom(this.authService.activeAccountStatus$);
if (authStatus !== AuthenticationStatus.Unlocked) {
return; return;
} }
@@ -167,7 +168,7 @@ class OverlayBackground implements OverlayBackgroundInterface {
private async getOverlayCipherData(): Promise<OverlayCipherData[]> { private async getOverlayCipherData(): Promise<OverlayCipherData[]> {
const showFavicons = await firstValueFrom(this.domainSettingsService.showFavicons$); const showFavicons = await firstValueFrom(this.domainSettingsService.showFavicons$);
const overlayCiphersArray = Array.from(this.overlayLoginCiphers); const overlayCiphersArray = Array.from(this.overlayLoginCiphers);
const overlayCipherData = []; const overlayCipherData: OverlayCipherData[] = [];
let loginCipherIcon: WebsiteIconData; let loginCipherIcon: WebsiteIconData;
for (let cipherIndex = 0; cipherIndex < overlayCiphersArray.length; cipherIndex++) { for (let cipherIndex = 0; cipherIndex < overlayCiphersArray.length; cipherIndex++) {