[PM-46819] Cancel passkey request when the modal window is closed (#24734)

This commit is contained in:
Isaiah Inuwa
2026-11-13 11:03:02 -06:00
committed by GitHub
parent 95f5897428
commit ea55f7b9d3
5 changed files with 116 additions and 2 deletions

View File

@@ -532,6 +532,7 @@ const safeProviders: SafeProvider[] = [
DesktopFido2UserVerificationService,
PasswordRepromptService,
DomainSettingsService,
MessageListener,
],
}),
safeProvider({

View File

@@ -1,6 +1,6 @@
import { Router } from "@angular/router";
import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject, of } from "rxjs";
import { BehaviorSubject, of, Subject } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
@@ -8,6 +8,7 @@ import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authenticatio
import { DomainSettingsService } from "@bitwarden/common/autofill/services/domain-settings.service";
import { Fido2AuthenticatorErrorCode } from "@bitwarden/common/platform/abstractions/fido2/fido2-authenticator.service.abstraction";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessageListener } from "@bitwarden/common/platform/messaging";
import { Fido2Utils } from "@bitwarden/common/platform/services/fido2/fido2-utils";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherRepromptType, CipherType } from "@bitwarden/common/vault/enums";
@@ -75,6 +76,7 @@ describe("DesktopFido2UserInterfaceSession", () => {
let userVerificationService: MockProxy<DesktopFido2UserVerificationService>;
let passwordRepromptService: MockProxy<PasswordRepromptService>;
let domainSettingsService: MockProxy<DomainSettingsService>;
let messages$: Subject<{ command: string }>;
let activeAccountStatus$: BehaviorSubject<AuthenticationStatus>;
let abortController: AbortController;
@@ -109,6 +111,8 @@ describe("DesktopFido2UserInterfaceSession", () => {
domainSettingsService = mock<DomainSettingsService>();
domainSettingsService.getUrlEquivalentDomains.mockReturnValue(of(new Set<string>()));
messages$ = new Subject();
activeAccountStatus$ = new BehaviorSubject<AuthenticationStatus>(AuthenticationStatus.Unlocked);
authService.activeAccountStatus$ = activeAccountStatus$;
accountService.activeAccount$ = new BehaviorSubject({ id: "user-1" } as any);
@@ -171,6 +175,7 @@ describe("DesktopFido2UserInterfaceSession", () => {
userVerificationService,
passwordRepromptService,
domainSettingsService,
new MessageListener(messages$),
);
});
@@ -537,6 +542,40 @@ describe("DesktopFido2UserInterfaceSession", () => {
});
});
describe("closing the window", () => {
const closeWindow = () => messages$.next({ command: "modalWindowClosed" });
it("cancels a ceremony waiting on the lock screen", async () => {
activeAccountStatus$.next(AuthenticationStatus.Locked);
const result = session.ensureUnlockedVault();
await tick();
closeWindow();
await expect(result).rejects.toThrow("Could not retrieve vault unlock status");
expect(desktopSettingsService.setModalMode).toHaveBeenCalledWith(false);
});
it("does not cancel a ceremony that is not showing UI", async () => {
closeWindow();
expect(abortController.signal.aborted).toBe(false);
});
it("does not cancel once the ceremony has hidden its UI", async () => {
activeAccountStatus$.next(AuthenticationStatus.Locked);
const result = session.ensureUnlockedVault();
await tick();
activeAccountStatus$.next(AuthenticationStatus.Unlocked);
await result;
await session.hideUi();
closeWindow();
expect(abortController.signal.aborted).toBe(false);
});
});
describe("informExcludedCredential", () => {
it("shows the excluded credentials UI and waits for the user to dismiss it", async () => {
const result = session.informExcludedCredential(["cipher-1"]);

View File

@@ -11,6 +11,7 @@ import {
switchMap,
throwError,
MonoTypeOperatorFunction,
Subscription,
} from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
@@ -29,6 +30,7 @@ import {
} from "@bitwarden/common/platform/abstractions/fido2/fido2-user-interface.service.abstraction";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service";
import { CommandDefinition, MessageListener } from "@bitwarden/common/platform/messaging";
import { Fido2Utils } from "@bitwarden/common/platform/services/fido2/fido2-utils";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherRepromptType, CipherType, SecureNoteType } from "@bitwarden/common/vault/enums";
@@ -91,6 +93,12 @@ export type NativeWindowObject = {
userHandle?: number[];
};
/**
* Sent by the main process when the user closes the app window while it is in
* modal mode, i.e. while a ceremony is showing its UI in it.
*/
const MODAL_WINDOW_CLOSED = new CommandDefinition("modalWindowClosed");
/** A ceremony that can be refused because of its RP ID. */
export type RpIdNotAllowedOperation = "registration" | "assertion";
@@ -152,6 +160,7 @@ export class DesktopFido2UserInterfaceService implements Fido2UserInterfaceServi
private userVerificationService: DesktopFido2UserVerificationService,
private passwordRepromptService: PasswordRepromptService,
private domainSettingsService: DomainSettingsService,
private messageListener: MessageListener,
) {}
private currentSession: any;
@@ -184,6 +193,7 @@ export class DesktopFido2UserInterfaceService implements Fido2UserInterfaceServi
this.userVerificationService,
this.passwordRepromptService,
this.domainSettingsService,
this.messageListener,
);
this.currentSession = session;
@@ -204,6 +214,7 @@ export class DesktopFido2UserInterfaceSession implements Fido2UserInterfaceSessi
private userVerificationService: DesktopFido2UserVerificationService,
private passwordRepromptService: PasswordRepromptService,
private domainSettingsService: DomainSettingsService,
private messageListener: MessageListener,
) {}
private confirmCredentialSubject = new Subject<boolean>();
@@ -235,6 +246,11 @@ export class DesktopFido2UserInterfaceSession implements Fido2UserInterfaceSessi
*/
private uiShown = false;
/**
* Cancels the ceremony if the user closes the window while it shows our UI.
*/
private windowClosedSubscription: Subscription | undefined;
// Method implementation
async pickCredential(
params: PickCredentialParams,
@@ -610,6 +626,9 @@ export class DesktopFido2UserInterfaceSession implements Fido2UserInterfaceSessi
* own `finally`. Safe to call more than once.
*/
async hideUi(): Promise<void> {
this.windowClosedSubscription?.unsubscribe();
this.windowClosedSubscription = undefined;
// Always clear modal mode so the app can never get stuck in it. The main
// process only restyles the window on the modal -> standard transition, so
// this is inert when the ceremony never entered modal mode.
@@ -633,6 +652,17 @@ export class DesktopFido2UserInterfaceSession implements Fido2UserInterfaceSessi
showTrafficButtons: boolean = false,
disableRedirect?: boolean,
): Promise<void> {
this.windowClosedSubscription ??= this.messageListener
.messages$(MODAL_WINDOW_CLOSED)
.pipe(take(1))
.subscribe(() => {
this.logService.info(
"[DesktopFido2UserInterfaceSession]",
"Window closed while showing UI. Aborting the request.",
);
this.abortController.abort("Operation cancelled");
});
// Load the UI:
await this.desktopSettingsService.setModalMode(true, showTrafficButtons, position);
await this.accountService.setShowHeader(showTrafficButtons);

View File

@@ -5,13 +5,14 @@ import * as path from "path";
import { app, BrowserWindow, ipcMain, session } from "electron";
import { mock, MockProxy } from "jest-mock-extended";
import { NEVER, of } from "rxjs";
import { BehaviorSubject, NEVER, of } from "rxjs";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { AbstractStorageService } from "@bitwarden/common/platform/abstractions/storage.service";
import { BiometricStateService } from "@bitwarden/key-management";
import { SafeShell } from "../platform/main/safe-shell.main";
import { ModalModeState } from "../platform/models/domain/window-state";
import { DesktopSettingsService } from "../platform/services/desktop-settings.service";
// window.main.ts registers a privileged scheme at module load time, which
@@ -272,6 +273,7 @@ describe("WindowMain", () => {
describe("createWindow", () => {
let sut: WindowMain;
let modalMode$: BehaviorSubject<ModalModeState>;
const fakeWindow = () =>
Object.assign(new EventEmitter(), {
@@ -294,9 +296,11 @@ describe("WindowMain", () => {
(global as any).BIT_ENVIRONMENT = "production";
const desktopSettingsService = mock<DesktopSettingsService>();
modalMode$ = new BehaviorSubject<ModalModeState>({ isModalModeActive: false });
Object.assign(desktopSettingsService, {
alwaysOnTop$: of(false),
preventScreenshots$: of(false),
modalMode$,
});
sut = new WindowMain(
@@ -349,6 +353,33 @@ describe("WindowMain", () => {
});
});
it("tells the renderer when the user closes the window in modal mode", async () => {
const win = fakeWindow();
jest.mocked(BrowserWindow).mockImplementationOnce(() => win as any);
await sut.createWindow();
modalMode$.next({ isModalModeActive: true });
win.emit("close");
await new Promise(process.nextTick);
expect(win.webContents.send).toHaveBeenCalledWith("messagingService", {
command: "modalWindowClosed",
});
});
it("does not tell the renderer when the user closes the window outside modal mode", async () => {
const win = fakeWindow();
jest.mocked(BrowserWindow).mockImplementationOnce(() => win as any);
await sut.createWindow();
win.emit("close");
await new Promise(process.nextTick);
expect(win.webContents.send).not.toHaveBeenCalledWith("messagingService", {
command: "modalWindowClosed",
});
});
it("clears the window reference when the current window closes", async () => {
const win = fakeWindow();
jest.mocked(BrowserWindow).mockImplementationOnce(() => win as any);

View File

@@ -567,6 +567,7 @@ export class WindowMain {
win.on("close", async () => {
this.isClosing = true;
await this.notifyIfModalWindowClosed(win);
await this.updateWindowState(mainWindowSizeKey, win);
});
@@ -677,6 +678,18 @@ export class WindowMain {
return fn(this.win);
}
/**
* Notifies the renderer that the user closed the window while the modal was
* active, e.g. during a passkey ceremony.
*/
private async notifyIfModalWindowClosed(win: BrowserWindow) {
const modalMode = await firstValueFrom(this.desktopSettingsService.modalMode$);
if (!modalMode?.isModalModeActive || win.isDestroyed()) {
return;
}
win.webContents.send("messagingService", { command: "modalWindowClosed" });
}
private windowStateChangeHandler(configKey: string, win: BrowserWindow) {
global.clearTimeout(this.windowStateChangeTimer);
this.windowStateChangeTimer = global.setTimeout(async () => {