[PM-46036] perf(browser): Stop decrypting and caching the whole vault (#24277)

This commit is contained in:
Bernd Schoolmann
2026-11-12 03:35:37 +09:00
committed by GitHub
parent a21f43157b
commit 6b8ce54785
40 changed files with 1014 additions and 300 deletions

View File

@@ -131,6 +131,9 @@ describe("NotificationBackground", () => {
accountService.activeAccount$ = activeAccountSubject;
const storedCipherPolicy = new DefaultFillCipherPolicy(cipherService);
fillCipherPolicy.ciphers.mockImplementation((...args) => storedCipherPolicy.ciphers(...args));
fillCipherPolicy.ciphersForIds.mockImplementation((...args) =>
storedCipherPolicy.ciphersForIds(...args),
);
fillCipherPolicy.ciphersForUrl.mockImplementation((...args) =>
storedCipherPolicy.ciphersForUrl(...args),
);

View File

@@ -290,18 +290,20 @@ export default class NotificationBackground {
const {
data: { cipherIds },
} = cipherQueueMessage;
const cipherViews = await this.fillCipherPolicy.ciphers(activeUserId, CipherUse.SavePrompt);
return cipherViews
.filter((cipher) => cipherIds.includes(cipher.id))
.map((cipherView) => {
const organizationType = getOrganizationType(cipherView.organizationId);
return this.convertToNotificationCipherData(
cipherView,
iconsServerUrl,
showFavicons,
organizationType,
);
});
const cipherViews = await this.fillCipherPolicy.ciphersForIds(
activeUserId,
cipherIds,
CipherUse.SavePrompt,
);
return cipherViews.map((cipherView) => {
const organizationType = getOrganizationType(cipherView.organizationId);
return this.convertToNotificationCipherData(
cipherView,
iconsServerUrl,
showFavicons,
organizationType,
);
});
} else {
cipherView = this.convertAddLoginQueueMessageToCipherView(cipherQueueMessage);
}

View File

@@ -60,6 +60,12 @@ describe("ContextMenuClickedHandler", () => {
};
};
/** Serves `ciphers` to lookups by id, as the vault would. */
const serveCiphers = (policy: MockProxy<FillCipherPolicy>, ciphers: CipherView[]) =>
policy.ciphersForIds.mockImplementation(async (_userId, ids) =>
ciphers.filter((cipher) => ids.includes(cipher.id)),
);
const createCipher = (data?: {
id?: CipherView["id"];
username?: CipherView["login"]["username"];
@@ -179,7 +185,7 @@ describe("ContextMenuClickedHandler", () => {
extra: () => expect(totpService.getCode$).not.toHaveBeenCalled(),
},
])("breaks silently when cipher is missing for $label", async ({ parentId, extra }) => {
fillCipherPolicy.ciphers.mockResolvedValue([]);
serveCiphers(fillCipherPolicy, []);
await expect(runWithUrl(createData(`${parentId}_1`, parentId))).resolves.toBeUndefined();
@@ -209,7 +215,7 @@ describe("ContextMenuClickedHandler", () => {
])("breaks silently when $label property is missing", async ({ parentId, unset, isTotp }) => {
const cipher = createCipher();
unset(cipher);
fillCipherPolicy.ciphers.mockResolvedValue([cipher]);
serveCiphers(fillCipherPolicy, [cipher]);
await expect(runWithUrl(createData(`${parentId}_1`, parentId))).resolves.toBeUndefined();
@@ -232,7 +238,7 @@ describe("ContextMenuClickedHandler", () => {
it("attempts to autofill the correct cipher", async () => {
const cipher = createCipher();
fillCipherPolicy.ciphers.mockResolvedValue([cipher]);
serveCiphers(fillCipherPolicy, [cipher]);
await sut.run(createData(`${AUTOFILL_ID}_1`, AUTOFILL_ID), { id: 5 } as any);
@@ -242,15 +248,19 @@ describe("ContextMenuClickedHandler", () => {
});
it("looks up the cipher the user picked by id as a user fill", async () => {
fillCipherPolicy.ciphers.mockResolvedValue([createCipher()]);
serveCiphers(fillCipherPolicy, [createCipher()]);
await sut.run(createData(`${AUTOFILL_ID}_1`, AUTOFILL_ID), { id: 5 } as any);
expect(fillCipherPolicy.ciphers).toHaveBeenCalledWith(mockUserId, CipherUse.UserFill);
expect(fillCipherPolicy.ciphersForIds).toHaveBeenCalledWith(
mockUserId,
["1"],
CipherUse.UserFill,
);
});
it("copies username to clipboard", async () => {
fillCipherPolicy.ciphers.mockResolvedValue([createCipher({ username: "TEST_USERNAME" })]);
serveCiphers(fillCipherPolicy, [createCipher({ username: "TEST_USERNAME" })]);
await sut.run(createData(`${COPY_USERNAME_ID}_1`, COPY_USERNAME_ID), {
url: "https://test.com",
@@ -265,7 +275,7 @@ describe("ContextMenuClickedHandler", () => {
});
it("copies password to clipboard", async () => {
fillCipherPolicy.ciphers.mockResolvedValue([createCipher({ password: "TEST_PASSWORD" })]);
serveCiphers(fillCipherPolicy, [createCipher({ password: "TEST_PASSWORD" })]);
await sut.run(createData(`${COPY_PASSWORD_ID}_1`, COPY_PASSWORD_ID), {
url: "https://test.com",
@@ -280,7 +290,7 @@ describe("ContextMenuClickedHandler", () => {
});
it("copies totp code to clipboard", async () => {
fillCipherPolicy.ciphers.mockResolvedValue([createCipher({ totp: "TEST_TOTP_SEED" })]);
serveCiphers(fillCipherPolicy, [createCipher({ totp: "TEST_TOTP_SEED" })]);
jest.spyOn(totpService, "getCode$").mockImplementation((seed: string) => {
if (seed === "TEST_TOTP_SEED") {

View File

@@ -211,8 +211,11 @@ export class ContextMenuClickedHandler {
cipher = ciphers[0];
} else {
const ciphers = await this.fillCipherPolicy.ciphers(activeUserId, CipherUse.UserFill);
cipher = ciphers.find(({ id }) => id === menuItemId);
[cipher] = await this.fillCipherPolicy.ciphersForIds(
activeUserId,
[menuItemId],
CipherUse.UserFill,
);
}
if (!cipher && !isCreateCipherAction) {

View File

@@ -35,6 +35,8 @@ export abstract class FillCipherPolicy {
/** Emits when a lookup result can change. A cached result must recompute. */
abstract changed$(userId: UserId): Observable<void>;
abstract ciphers(userId: UserId, use: CipherUse): Promise<CipherView[]>;
/** The ciphers among `ids`. Decrypts only those, not the whole vault. */
abstract ciphersForIds(userId: UserId, ids: string[], use: CipherUse): Promise<CipherView[]>;
abstract ciphersForUrl(
url: string,
userId: UserId,

View File

@@ -25,17 +25,25 @@ describe("DefaultFillCipherPolicy", () => {
});
it("emits changed$ after each decryption and not while decryption is in progress", async () => {
const cipherViews$ = new Subject<CipherView[] | null>();
cipherService.cipherViews$.mockReturnValue(cipherViews$);
const listViews$ = new Subject<CipherView[] | null>();
cipherService.cipherListViews$.mockReturnValue(listViews$);
const emitted = firstValueFrom(policy.changed$(userId).pipe(toArray()));
cipherViews$.next([view("a")]);
cipherViews$.next(null);
cipherViews$.next([view("b")]);
cipherViews$.complete();
listViews$.next([view("a")]);
listViews$.next(null);
listViews$.next([view("b")]);
listViews$.complete();
expect(await emitted).toEqual([undefined, undefined]);
expect(cipherService.cipherViews$).toHaveBeenCalledWith(userId);
expect(cipherService.cipherListViews$).toHaveBeenCalledWith(userId);
});
it("watches list views for changed$, so the background never decrypts the whole vault", () => {
cipherService.cipherListViews$.mockReturnValue(new Subject<CipherView[] | null>());
policy.changed$(userId).subscribe();
expect(cipherService.cipherViews$).not.toHaveBeenCalled();
});
it.each(Object.values(CipherUse))("returns the stored ciphers for %s", async (use) => {
@@ -45,6 +53,15 @@ describe("DefaultFillCipherPolicy", () => {
expect(await policy.ciphers(userId, use)).toBe(stored);
});
it("decrypts only the requested ids", async () => {
const stored = [view("a")];
cipherService.getAllDecryptedForIds.mockResolvedValue(stored);
expect(await policy.ciphersForIds(userId, ["a"], CipherUse.UserFill)).toBe(stored);
expect(cipherService.getAllDecryptedForIds).toHaveBeenCalledWith(userId, ["a"]);
expect(cipherService.getAllDecrypted).not.toHaveBeenCalled();
});
it("passes the URL options to the stored lookup", async () => {
const stored = [view("stored")];
cipherService.getAllDecryptedForUrl.mockResolvedValue(stored);

View File

@@ -12,9 +12,13 @@ import { CipherUse, FillCipherPolicy, SaveCandidate } from "./abstractions/fill-
export class DefaultFillCipherPolicy implements FillCipherPolicy {
constructor(protected readonly cipherService: CipherService) {}
/** Emits after each decryption, so a lookup that follows reads the new ciphers. */
/**
* Emits after each decryption, so a lookup that follows reads the new ciphers. Watches list
* views: the background subscribes for as long as the vault is unlocked, and full views would
* keep the whole vault decrypted in memory.
*/
changed$(userId: UserId): Observable<void> {
return this.cipherService.cipherViews$(userId).pipe(
return this.cipherService.cipherListViews$(userId).pipe(
filter((views) => views != null),
map((): void => undefined),
);
@@ -24,6 +28,10 @@ export class DefaultFillCipherPolicy implements FillCipherPolicy {
return this.cipherService.getAllDecrypted(userId);
}
ciphersForIds(userId: UserId, ids: string[], use: CipherUse): Promise<CipherView[]> {
return this.cipherService.getAllDecryptedForIds(userId, ids);
}
ciphersForUrl(
url: string,
userId: UserId,

View File

@@ -424,6 +424,7 @@ import {
isNotificationsSupported,
} from "../platform/system-notifications/browser-system-notification.service";
import { fromChromeRuntimeMessaging } from "../platform/utils/from-chrome-runtime-messaging";
import { ListViewsAnnouncerService } from "../vault/background/list-views-announcer.service";
import { AtRiskCipherBadgeUpdaterService } from "../vault/services/at-risk-cipher-badge-updater.service";
import CommandsBackground from "./commands.background";
@@ -2416,6 +2417,11 @@ export default class MainBackground {
await this.autofillBadgeUpdaterService.init();
await this.clipboardNotificationBadgeUpdaterService.init();
await this.atRiskCipherUpdaterService.init();
new ListViewsAnnouncerService(
this.accountService,
this.cipherService,
this.messagingService,
).init();
}
generatePasswordToClipboard = () => {

View File

@@ -213,6 +213,12 @@ describe("RuntimeBackground collection dispatch", () => {
});
});
/** Serves `ciphers` to lookups by id, as the vault would. */
const serveCiphers = (policy: MockProxy<FillCipherPolicy>, ciphers: CipherView[]) =>
policy.ciphersForIds.mockImplementation(async (_userId, ids) =>
ciphers.filter((cipher) => ids.includes(cipher.id)),
);
describe("fillCipherForPopup", () => {
const cipher = Object.assign(new CipherView(), { id: "cipher-1" });
let fillCipherPolicy: MockProxy<FillCipherPolicy>;
@@ -220,7 +226,7 @@ describe("RuntimeBackground collection dispatch", () => {
beforeEach(() => {
jest.spyOn(BrowserApi, "getTab").mockResolvedValue(tab);
fillCipherPolicy = mock<FillCipherPolicy>();
fillCipherPolicy.ciphers.mockResolvedValue([cipher]);
serveCiphers(fillCipherPolicy, [cipher]);
(main as any).fillCipherPolicy = fillCipherPolicy;
autofillOrchestrator.unsafeAutofillTabWithCipher.mockResolvedValue({
outcome: AutofillOutcome.Filled,
@@ -244,7 +250,11 @@ describe("RuntimeBackground collection dispatch", () => {
popupSender,
);
expect(fillCipherPolicy.ciphers).toHaveBeenCalledWith("user-1", CipherUse.UserFill);
expect(fillCipherPolicy.ciphersForIds).toHaveBeenCalledWith(
"user-1",
["cipher-1"],
CipherUse.UserFill,
);
});
it("security: rejects a content-script sender without fetching or filling", async () => {
@@ -261,7 +271,7 @@ describe("RuntimeBackground collection dispatch", () => {
// Logging the rejection is part of the security requirement: the warning is the
// observable record that the boundary fired on this sender.
expect(logService.warning).toHaveBeenCalled();
expect(fillCipherPolicy.ciphers).not.toHaveBeenCalled();
expect(fillCipherPolicy.ciphersForIds).not.toHaveBeenCalled();
expect(autofillOrchestrator.unsafeAutofillTabWithCipher).not.toHaveBeenCalled();
});

View File

@@ -1,6 +1,6 @@
// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { firstValueFrom, map, mergeMap } from "rxjs";
import { filter, firstValueFrom, map, mergeMap } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { AutofillOverlayVisibility, ExtensionCommand } from "@bitwarden/common/autofill/constants";
@@ -55,6 +55,7 @@ import BrowserInitialInstallService from "../platform/services/browser-initial-i
import { BrowserPlatformUtilsService } from "../platform/services/platform-utils/browser-platform-utils.service";
import { isValidVaultReferrer } from "../platform/utils/valid-vault-referrer";
import { getWebExtSender } from "../platform/utils/web-ext-sender";
import { GET_CIPHER_LIST_VIEWS } from "../vault/services/foreground-cipher.service";
import MainBackground from "./main.background";
@@ -124,6 +125,7 @@ export default class RuntimeBackground {
"fillCipherForPopup",
"getBitwardenAutofillAttributeSettings",
VaultMessages.checkBwInstalled,
GET_CIPHER_LIST_VIEWS,
];
if (messagesWithResponse.includes(msg.command)) {
@@ -180,6 +182,17 @@ export default class RuntimeBackground {
}
return await this.autofillOrchestrator.collectPageDetails(targetTab);
}
case GET_CIPHER_LIST_VIEWS: {
// The list views are decrypted vault data, so only extension pages get them.
if (!BrowserApi.senderIsInternal(sender, this.logService)) {
return [];
}
return await firstValueFrom(
this.main.cipherService
.cipherListViewsIncludingRestricted$(msg.userId)
.pipe(filter((views) => views != null)),
);
}
case "fillCipherForPopup": {
// The popup keeps the reprompt and TOTP copy in the foreground but dispatches the fill here.
// The cipher is referenced by id and re-fetched below so decrypted vault data never crosses
@@ -198,8 +211,11 @@ export default class RuntimeBackground {
if (activeUserId == null) {
return AUTOFILL_DENIED;
}
const ciphers = await this.main.fillCipherPolicy.ciphers(activeUserId, CipherUse.UserFill);
const cipher = ciphers.find((candidate) => candidate.id === msg.cipherId);
const [cipher] = await this.main.fillCipherPolicy.ciphersForIds(
activeUserId,
[msg.cipherId],
CipherUse.UserFill,
);
if (cipher == null) {
return AUTOFILL_DENIED;
}

View File

@@ -7,6 +7,9 @@ import { FakeStorageService, makeEncString, makeSymmetricCryptoKey } from "@bitw
import { EncryptService, KeyGenerationService, SymmetricCryptoKey } from "@bitwarden/legacy-crypto";
import { StorageService } from "@bitwarden/storage-core";
import { BrowserApi } from "../browser/browser-api";
import { portName } from "../storage/port-name";
import BrowserLocalStorageService from "./browser-local-storage.service";
import {
LocalBackedSessionStorageService,
@@ -113,6 +116,44 @@ describe("LocalBackedSessionStorage", () => {
);
});
describe("port connections", () => {
it("stops broadcasting updates to a port once it disconnects", async () => {
jest.spyOn(BrowserApi, "senderIsInternal").mockReturnValue(true);
let onConnect!: (port: chrome.runtime.Port) => void;
jest
.spyOn(BrowserApi, "addListener")
.mockImplementation((_event, callback) => (onConnect = callback as typeof onConnect));
const connectedSut = new LocalBackedSessionStorageService(
memoryStorage,
localStorage,
keyGenerationService,
encryptService,
platformUtilsService,
logService,
);
let disconnect!: () => void;
const port = {
name: portName(chrome.storage.session),
sender: {},
postMessage: jest.fn(),
onMessage: { addListener: jest.fn(), removeListener: jest.fn() },
onDisconnect: { addListener: jest.fn((cb: () => void) => (disconnect = cb)) },
} as unknown as chrome.runtime.Port;
onConnect(port);
encryptService.encryptString.mockResolvedValue(makeEncString("encrypted"));
const broadcast = jest.spyOn(
connectedSut as unknown as { broadcastMessage: () => void },
"broadcastMessage",
);
disconnect();
await connectedSut.save("key", "value");
expect(broadcast).not.toHaveBeenCalled();
});
});
describe("get", () => {
const encString = makeEncString("encrypted");

View File

@@ -115,7 +115,15 @@ export class LocalBackedSessionStorageService
this.ports.add(port);
const listenerCallback = this.onMessageFromForeground.bind(this);
// One per port, dropped on disconnect so each popup open doesn't leave a listener behind.
const updatesSubscription = this.updates$.subscribe((update) => {
this.broadcastMessage({
action: "subject_update",
data: update,
});
});
port.onDisconnect.addListener(() => {
updatesSubscription.unsubscribe();
this.ports.delete(port);
port.onMessage.removeListener(listenerCallback);
});
@@ -125,12 +133,6 @@ export class LocalBackedSessionStorageService
action: "initialization",
data: Array.from(Object.keys(this.cache)),
});
this.updates$.subscribe((update) => {
this.broadcastMessage({
action: "subject_update",
data: update,
});
});
});
}

View File

@@ -144,7 +144,10 @@ import { UnsupportedSystemNotificationsService } from "@bitwarden/common/platfor
import { SendApiService } from "@bitwarden/common/tools/send/services/send-api.service.abstraction";
import { InternalSendService } from "@bitwarden/common/tools/send/services/send.service.abstraction";
import { CipherArchiveService } from "@bitwarden/common/vault/abstractions/cipher-archive.service";
import { CipherEncryptionService } from "@bitwarden/common/vault/abstractions/cipher-encryption.service";
import { CipherSdkService } from "@bitwarden/common/vault/abstractions/cipher-sdk.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherFileUploadService } from "@bitwarden/common/vault/abstractions/file-upload/cipher-file-upload.service";
import { FolderApiServiceAbstraction } from "@bitwarden/common/vault/abstractions/folder/folder-api.service.abstraction";
import { InternalFolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { TotpService as TotpServiceAbstraction } from "@bitwarden/common/vault/abstractions/totp.service";
@@ -260,6 +263,7 @@ import {
import { fromChromeRuntimeMessaging } from "../../platform/utils/from-chrome-runtime-messaging";
import { BrowserAutofillNudgeService } from "../../vault/popup/services/browser-autofill-nudge.service";
import { Fido2UserVerificationService } from "../../vault/services/fido2-user-verification.service";
import { ForegroundCipherService } from "../../vault/services/foreground-cipher.service";
import { ExtensionAnonLayoutWrapperDataService } from "../components/extension-anon-layout-wrapper/extension-anon-layout-wrapper-data.service";
import { DebounceNavigationService } from "./debounce-navigation.service";
@@ -281,6 +285,27 @@ const DISK_BACKUP_LOCAL_STORAGE = new SafeInjectionToken<
*/
const safeProviders: SafeProvider[] = [
safeProvider(InitService),
safeProvider({
provide: CipherService,
useClass: ForegroundCipherService,
deps: [
MessageListener,
KeyService,
LegacyCompatKeyService,
DomainSettingsService,
ApiService,
I18nServiceAbstraction,
AutofillSettingsServiceAbstraction,
EncryptService,
CipherFileUploadService,
ConfigService,
StateProvider,
AccountServiceAbstraction,
LogService,
CipherEncryptionService,
CipherSdkService,
],
}),
safeProvider(DebounceNavigationService),
safeProvider(DialogService),
safeProvider(PopupCloseWarningService),

View File

@@ -0,0 +1,55 @@
import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject, Subject } from "rxjs";
import { Account, AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { MessageSender } from "@bitwarden/common/platform/messaging";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherListView } from "@bitwarden/sdk-internal";
import { CIPHER_LIST_VIEWS_CHANGED } from "../services/foreground-cipher.service";
import { ListViewsAnnouncerService } from "./list-views-announcer.service";
describe("ListViewsAnnouncerService", () => {
const userId = "user-id" as UserId;
let activeAccount$: BehaviorSubject<Account | null>;
let listViews$: Subject<CipherListView[] | null>;
let cipherService: MockProxy<CipherService>;
let messageSender: MockProxy<MessageSender>;
beforeEach(() => {
activeAccount$ = new BehaviorSubject<Account | null>({ id: userId } as Account);
listViews$ = new Subject();
cipherService = mock<CipherService>();
cipherService.cipherListViewsIncludingRestricted$.mockReturnValue(listViews$);
messageSender = mock<MessageSender>();
const accountService = mock<AccountService>();
accountService.activeAccount$ = activeAccount$;
new ListViewsAnnouncerService(accountService, cipherService, messageSender).init();
});
it("announces each list-view change, so an open popup fetches them again", () => {
listViews$.next([]);
listViews$.next([]);
expect(cipherService.cipherListViewsIncludingRestricted$).toHaveBeenCalledWith(userId);
expect(messageSender.send).toHaveBeenCalledTimes(2);
expect(messageSender.send).toHaveBeenCalledWith(CIPHER_LIST_VIEWS_CHANGED);
});
it("stays quiet while list views are cleared or decrypting", () => {
listViews$.next(null);
expect(messageSender.send).not.toHaveBeenCalled();
});
it("stays quiet without an active account", () => {
activeAccount$.next(null);
listViews$.next([]);
expect(messageSender.send).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,38 @@
import { EMPTY, filter, switchMap } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { MessageSender } from "@bitwarden/common/platform/messaging";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CIPHER_LIST_VIEWS_CHANGED } from "../services/foreground-cipher.service";
/**
* Tells an open popup each time the background's list views change, after an edit, sync or
* `localData` update, so the popup fetches them again. The popup holds no decrypted vault of its
* own and cannot see these changes otherwise.
*
* background popup
* cipherListViewsIncludingRestricted$ ──▶ CIPHER_LIST_VIEWS_CHANGED
* ◀── GET_CIPHER_LIST_VIEWS
*/
export class ListViewsAnnouncerService {
constructor(
private readonly accountService: AccountService,
private readonly cipherService: CipherService,
private readonly messageSender: MessageSender,
) {}
init(): void {
this.accountService.activeAccount$
.pipe(
switchMap((account) =>
account == null
? EMPTY
: this.cipherService.cipherListViewsIncludingRestricted$(account.id),
),
// Null means cleared or still decrypting: nothing new to fetch yet.
filter((views) => views != null),
)
.subscribe(() => this.messageSender.send(CIPHER_LIST_VIEWS_CHANGED));
}
}

View File

@@ -379,7 +379,7 @@ const buildNotificationPolicies = (args: StoryArgs) => {
/**
* Tasks backing `vault-at-risk-password-callout`. `pendingTasks$` joins each task to a cipher by
* `cipherId` and keeps only the editable, password-visible ones, so tasks have to point at ciphers
* the story provides and `CipherService.cipherViews$` has to return them.
* the story provides and `CipherService.cipherListViews$` has to return them.
*/
const buildAtRiskTask = (
cipherId: PopupCipherViewLike["id"],
@@ -558,6 +558,8 @@ const buildProviders = (args: StoryArgs) => {
// No decryption failures, so the failure dialog never opens during a snapshot.
failedToDecryptCiphers$: () => of([]),
cipherViews$: () => of(atRiskCiphers),
// The at-risk password callout matches tasks to list views.
cipherListViews$: () => of(atRiskCiphers),
ciphers$: () => of({}),
},
},

View File

@@ -24,8 +24,6 @@ import { RevealedCipherService } from "@bitwarden/common/vault/abstractions/reve
import { SearchService } from "@bitwarden/common/vault/abstractions/search.service";
import { VaultSettingsService } from "@bitwarden/common/vault/abstractions/vault-settings/vault-settings.service";
import { CipherType } from "@bitwarden/common/vault/enums";
import { CipherData } from "@bitwarden/common/vault/models/data/cipher.data";
import { LocalData } from "@bitwarden/common/vault/models/data/local.data";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import {
RestrictedCipherType,
@@ -61,8 +59,7 @@ describe("VaultPopupItemsService", () => {
mockSignal: WritableSignal<string | null>;
};
let ciphersSubject: BehaviorSubject<Record<CipherId, CipherData>>;
let localDataSubject: BehaviorSubject<Record<CipherId, LocalData>>;
let cipherList$: BehaviorSubject<CipherView[]>;
let failedToDecryptCiphersSubject: BehaviorSubject<CipherView[]>;
const cipherServiceMock = mock<CipherService>();
@@ -109,22 +106,14 @@ describe("VaultPopupItemsService", () => {
cipherList[2].favorite = true;
cipherList[3].favorite = true;
const cipherList$ = new BehaviorSubject<CipherView[]>(cipherList);
cipherList$ = new BehaviorSubject<CipherView[]>(cipherList);
cipherServiceMock.cipherListViewsIncludingRestricted$.mockReturnValue(
cipherList$.asObservable(),
);
ciphersSubject = new BehaviorSubject<Record<CipherId, CipherData>>({});
localDataSubject = new BehaviorSubject<Record<CipherId, LocalData>>({});
failedToDecryptCiphersSubject = new BehaviorSubject<CipherView[]>([]);
cipherServiceMock.ciphers$.mockImplementation((userId: UserId) =>
ciphersSubject.asObservable(),
);
cipherServiceMock.localData$.mockImplementation((userId: UserId) =>
localDataSubject.asObservable(),
);
cipherServiceMock.failedToDecryptCiphers$.mockImplementation((userId: UserId) =>
failedToDecryptCiphersSubject.asObservable(),
);
@@ -239,26 +228,12 @@ describe("VaultPopupItemsService", () => {
});
});
it("should update cipher list when cipherService.ciphers$ emits", async () => {
it("should update cipher list when cipherService.cipherListViewsIncludingRestricted$ emits", async () => {
const tracker = new ObservableTracker(service.autoFillCiphers$);
await tracker.expectEmission();
ciphersSubject.next({});
await tracker.expectEmission();
// Should only emit twice
expect(tracker.emissions.length).toBe(2);
await expect(tracker.pauseUntilReceived(3)).rejects.toThrow("Timeout exceeded");
});
it("should update cipher list when cipherService.localData$ emits", async () => {
const tracker = new ObservableTracker(service.autoFillCiphers$);
await tracker.expectEmission();
localDataSubject.next({});
cipherList$.next(cipherList$.value);
await tracker.expectEmission();
@@ -383,7 +358,6 @@ describe("VaultPopupItemsService", () => {
it("lists restricted ciphers", async () => {
cipherServiceMock.cipherListViewsIncludingRestricted$.mockReturnValue(of([restrictedRow()]));
ciphersSubject.next({});
const ciphers = await firstValueFrom(service.filteredCiphers$);
@@ -397,7 +371,6 @@ describe("VaultPopupItemsService", () => {
const revealed = Object.assign(new CipherView(), { id: "restricted" as CipherId });
cipherServiceMock.cipherListViewsIncludingRestricted$.mockReturnValue(of([row]));
revealed$.next(new Map([["restricted" as CipherId, revealed]]));
ciphersSubject.next({});
const [shown] = await firstValueFrom(service.filteredCiphers$);
@@ -411,7 +384,6 @@ describe("VaultPopupItemsService", () => {
cipherServiceMock.cipherListViewsIncludingRestricted$.mockReturnValue(
of([deletedRow, deleted]),
);
ciphersSubject.next({});
const deletedCiphers = await firstValueFrom(service.deletedCiphers$);
@@ -507,7 +479,6 @@ describe("VaultPopupItemsService", () => {
const ciphers = [new CipherView(), new CipherView(), new CipherView(), deletedCipher];
cipherServiceMock.cipherListViewsIncludingRestricted$.mockReturnValue(of(ciphers));
ciphersSubject.next({});
const deletedCiphers = await firstValueFrom(service.deletedCiphers$);
expect(deletedCiphers.length).toBe(1);
@@ -526,7 +497,6 @@ describe("VaultPopupItemsService", () => {
cipherServiceMock.cipherListViewsIncludingRestricted$.mockReturnValue(
of([personalCipher, memberOrgCipher, revokedOrgCipher]),
);
ciphersSubject.next({});
const deletedCiphers = await firstValueFrom(service.deletedCiphers$);
expect(deletedCiphers.map((c) => c.organizationId)).toEqual([undefined, "org1"]);
@@ -657,11 +627,11 @@ describe("VaultPopupItemsService", () => {
expect(tracked.emissions[1]).toBe(false);
});
it("should cycle when cipherService.ciphers$ emits", async () => {
it("should cycle when cipherService.cipherListViewsIncludingRestricted$ emits", async () => {
await trackedCiphers.expectEmission();
// Restart tracking
tracked = new ObservableTracker(service.loading$);
ciphersSubject.next({});
cipherList$.next(cipherList$.value);
await trackedCiphers.pauseUntilReceived(2);

View File

@@ -136,16 +136,15 @@ export class VaultPopupItemsService {
map((a) => a?.id),
filter((userId): userId is UserId => userId != null),
switchMap((userId) =>
merge(this.cipherService.ciphers$(userId), this.cipherService.localData$(userId)).pipe(
this.cipherService.cipherListViewsIncludingRestricted$(userId).pipe(
filter((ciphers) => ciphers != null),
debounceTime(0),
runInsideAngular(this.ngZone),
tap(() => this._ciphersLoading$.next()),
waitUntilSync(this.syncService),
switchMap(() =>
switchMap((ciphers) =>
combineLatest([
this.cipherService
.cipherListViewsIncludingRestricted$(userId)
.pipe(filter((ciphers) => ciphers != null)),
of(ciphers),
this.cipherService.failedToDecryptCiphers$(userId).pipe(startWith([])),
this.restrictedItemTypesService.restricted$,
]),

View File

@@ -41,7 +41,7 @@ describe("AtRiskCipherBadgeUpdaterService", () => {
service = new AtRiskCipherBadgeUpdaterService(
{ setState } as unknown as BadgeService,
{ activeAccount$ } as unknown as AccountService,
{ cipherViews$: () => cipherViews$, getAllDecryptedForUrl } as unknown as CipherService,
{ cipherListViews$: () => cipherViews$, getAllDecryptedForUrl } as unknown as CipherService,
{ pendingTasks$: () => pendingTasks$ } as unknown as TaskService,
);

View File

@@ -23,7 +23,8 @@ export class AtRiskCipherBadgeUpdaterService {
.pipe(
map((tasks) => tasks.filter((t) => t.type === SecurityTaskType.UpdateAtRiskCredential)),
),
this.cipherService.cipherViews$(user.id).pipe(filterOutNullish()),
// A change trigger only: list views, so the vault is not fully decrypted for it.
this.cipherService.cipherListViews$(user.id).pipe(filterOutNullish()),
]),
),
);

View File

@@ -0,0 +1,101 @@
import { mock } from "jest-mock-extended";
import { firstValueFrom, Subject } from "rxjs";
import { Message, MessageListener } from "@bitwarden/common/platform/messaging";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/services/cipher.service";
import { CipherListView } from "@bitwarden/sdk-internal";
import { BrowserApi } from "../../platform/browser/browser-api";
import {
CIPHER_LIST_VIEWS_CHANGED,
ForegroundCipherService,
GET_CIPHER_LIST_VIEWS,
} from "./foreground-cipher.service";
describe("ForegroundCipherService", () => {
const userId = "user-id" as UserId;
let messages$: Subject<Message<Record<string, unknown>>>;
let sendMessageWithResponse: jest.SpyInstance;
let service: ForegroundCipherService;
const listViews = (name: string) => [{ id: "cipher-id", name }] as unknown as CipherListView[];
beforeEach(() => {
messages$ = new Subject();
sendMessageWithResponse = jest
.spyOn(BrowserApi, "sendMessageWithResponse")
.mockResolvedValue({ result: listViews("first") });
// Only the list-view stream is under test; the inherited dependencies stay mocks.
const cipherServiceDeps = Array.from({ length: 14 }, () =>
mock<any>(),
) as ConstructorParameters<typeof CipherService>;
service = new ForegroundCipherService(new MessageListener(messages$), ...cipherServiceDeps);
});
afterEach(() => {
jest.restoreAllMocks();
});
it("takes the list views from the background instead of decrypting them", async () => {
const views = await firstValueFrom(service.cipherListViewsIncludingRestricted$(userId));
expect(views).toEqual(listViews("first"));
expect(sendMessageWithResponse).toHaveBeenCalledWith(GET_CIPHER_LIST_VIEWS, { userId });
});
it("fetches again when the background announces new list views", async () => {
const emitted: (CipherListView[] | null)[] = [];
service.cipherListViewsIncludingRestricted$(userId).subscribe((views) => emitted.push(views));
await Promise.resolve();
sendMessageWithResponse.mockResolvedValue({ result: listViews("second") });
messages$.next({ command: CIPHER_LIST_VIEWS_CHANGED });
await Promise.resolve();
await Promise.resolve();
expect(emitted.at(-1)).toEqual(listViews("second"));
expect(sendMessageWithResponse).toHaveBeenCalledTimes(2);
});
it("ignores the overlay's updateOverlayCiphers, which nothing sends anymore", async () => {
service.cipherListViewsIncludingRestricted$(userId).subscribe();
await Promise.resolve();
messages$.next({ command: "updateOverlayCiphers" });
await Promise.resolve();
expect(sendMessageWithResponse).toHaveBeenCalledTimes(1);
});
it("waits for the background's announcement when the background did not answer", async () => {
// Chrome calls back without a response when no listener is registered yet.
sendMessageWithResponse.mockResolvedValueOnce(undefined);
const emitted: (CipherListView[] | null)[] = [];
service.cipherListViewsIncludingRestricted$(userId).subscribe((views) => emitted.push(views));
await Promise.resolve();
expect(emitted).toEqual([null]);
messages$.next({ command: CIPHER_LIST_VIEWS_CHANGED });
await Promise.resolve();
await Promise.resolve();
expect(emitted.at(-1)).toEqual(listViews("first"));
});
it("drops its copy of the list views when the cache is cleared", async () => {
const emitted: (CipherListView[] | null)[] = [];
service.cipherListViewsIncludingRestricted$(userId).subscribe((views) => emitted.push(views));
await Promise.resolve();
jest.spyOn(Object.getPrototypeOf(ForegroundCipherService.prototype), "clearCache");
Object.getPrototypeOf(ForegroundCipherService.prototype).clearCache.mockResolvedValue(
undefined,
);
await service.clearCache(userId);
expect(emitted.at(-1)).toBeNull();
});
});

View File

@@ -0,0 +1,54 @@
import { filter, map, startWith, Subject, switchMap } from "rxjs";
import { MessageListener } from "@bitwarden/common/platform/messaging";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/services/cipher.service";
import { perUserCache$ } from "@bitwarden/common/vault/utils/observable-utilities";
import { CipherListView } from "@bitwarden/sdk-internal";
import { BrowserApi } from "../../platform/browser/browser-api";
export const GET_CIPHER_LIST_VIEWS = "getCipherListViews";
/** What the background's `ListViewsAnnouncerService` sends each time its list views change. */
export const CIPHER_LIST_VIEWS_CHANGED = "cipherListViewsChanged";
/**
* The popup's cipher service. Takes the list views from the background, which already
* decrypted them, instead of decrypting the whole vault again on every popup open.
*/
export class ForegroundCipherService extends CipherService {
private readonly clearListViews$ = new Subject<UserId | null>();
constructor(
private readonly messageListener: MessageListener,
...cipherServiceArgs: ConstructorParameters<typeof CipherService>
) {
super(...cipherServiceArgs);
}
// Fetches when opened and whenever the background announces new list views, so the popup
// never asks before the background caught up with a change.
override cipherListViewsIncludingRestricted$ = perUserCache$(
(userId: UserId) =>
this.messageListener.allMessages$.pipe(
filter((message) => message.command === CIPHER_LIST_VIEWS_CHANGED),
startWith(null),
switchMap(() =>
BrowserApi.sendMessageWithResponse<{ result: CipherListView[] }>(GET_CIPHER_LIST_VIEWS, {
userId,
}),
),
// No answer, e.g. from a background still starting after a restart, is not ready yet;
// the background announces its list views once it has decrypted them.
map((response) => response?.result ?? null),
),
this.clearListViews$,
);
/** Drops the popup's copy of the list views too, e.g. on lock. */
override async clearCache(userId?: UserId): Promise<void> {
await super.clearCache(userId);
this.clearListViews$.next(userId ?? null);
}
}

View File

@@ -1,5 +1,8 @@
// @ts-strict-ignore
import { mock } from "jest-mock-extended";
import { Observable, of } from "rxjs";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { GetCommand } from "./get.command";
@@ -26,4 +29,54 @@ describe("GetCommand", () => {
expect(actual.data).toEqual(expected.data);
});
});
describe("item lookup by search term", () => {
const listViews = [
{ id: "c1", name: "Alpha", type: { login: {} } },
{ id: "c2", name: "Beta", type: { login: {} } },
{ id: "c3", name: "Alpha trashed", type: { login: {} }, deletedDate: "2024-01-01" },
];
function build(listViews$: Observable<unknown>) {
const alpha = Object.assign(new CipherView(), { id: "c1", name: "Alpha" });
const cipherService = {
cipherListViews$: jest.fn().mockReturnValue(listViews$),
getAllDecrypted: jest.fn().mockResolvedValue([alpha]),
getAllDecryptedForIds: jest.fn().mockResolvedValue([alpha]),
};
const searchService = {
searchCiphersBasic: jest
.fn()
.mockImplementation((ciphers: any[], query: string) =>
ciphers.filter((c) => c.name.toLowerCase().includes(query)),
),
};
const args: any[] = new Array(16).fill(null).map(() => mock());
args[0] = cipherService;
args[8] = searchService;
const getCommand = new (GetCommand as any)(...args);
return { cipherService, getCommand };
}
it("decrypts only the matching ciphers", async () => {
const { cipherService, getCommand } = build(of(listViews));
await getCommand["getCipherView"]("alpha", "user-1");
expect(cipherService.getAllDecrypted).not.toHaveBeenCalled();
expect(cipherService.getAllDecryptedForIds).toHaveBeenCalledWith("user-1", ["c1"]);
});
// A failed write clears the stream and nothing re-emits it, so waiting would hang.
it("decrypts directly when the list views were cleared", async () => {
const { cipherService, getCommand } = build(of(null));
const result = await getCommand["getCipherView"]("alpha", "user-1");
expect(cipherService.getAllDecrypted).toHaveBeenCalledWith("user-1");
expect(cipherService.getAllDecryptedForIds).not.toHaveBeenCalled();
expect(result.id).toBe("c1");
});
});
});

View File

@@ -29,6 +29,7 @@ import { PassportExport } from "@bitwarden/common/models/export/passport.export"
import { SecureNoteExport } from "@bitwarden/common/models/export/secure-note.export";
import { ErrorResponse } from "@bitwarden/common/models/response/error.response";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { uuidAsString } from "@bitwarden/common/platform/abstractions/sdk/sdk.service";
import { getById } from "@bitwarden/common/platform/misc";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { CipherId, OrganizationId, UserId } from "@bitwarden/common/types/guid";
@@ -39,6 +40,10 @@ import { TotpService } from "@bitwarden/common/vault/abstractions/totp.service";
import { CipherType } from "@bitwarden/common/vault/enums";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
import {
CipherViewLike,
CipherViewLikeUtils,
} from "@bitwarden/common/vault/utils/cipher-view-like-utils";
import { KeyService } from "@bitwarden/key-management";
// eslint-disable-next-line no-restricted-imports
import { EncryptService, LegacyCompatKeyService } from "@bitwarden/legacy-crypto";
@@ -132,9 +137,7 @@ export class GetCommand extends DownloadCommand {
decCipher = await this.cipherService.decrypt(cipher, userId);
}
} else if (id.trim() !== "") {
let ciphers = await this.cipherService.getAllDecrypted(userId);
ciphers = ciphers.filter((c) => !c.isDeleted && !c.isArchived);
ciphers = this.searchService.searchCiphersBasic(ciphers, id);
const ciphers = await this.searchCiphers(id, userId);
if (ciphers.length > 1) {
return ciphers;
}
@@ -146,6 +149,30 @@ export class GetCommand extends DownloadCommand {
return decCipher;
}
/**
* Searches the list views and decrypts only the matches, so a lookup does not decrypt the vault.
* Falls back to decrypting directly when the views were cleared by a write that failed: nothing
* re-emits them, so waiting would hang `bw serve`.
*/
private async searchCiphers(query: string, userId: UserId): Promise<CipherView[]> {
const listViews = await firstValueFrom(this.cipherService.cipherListViews$(userId));
if (listViews == null) {
const all = await this.cipherService.getAllDecrypted(userId);
return this.searchService.searchCiphersBasic(all.filter(this.isActive), query);
}
const matches = this.searchService.searchCiphersBasic(listViews.filter(this.isActive), query);
return await this.cipherService.getAllDecryptedForIds(
userId,
matches.map((v) => uuidAsString(v.id)),
);
}
private isActive = (c: CipherViewLike): boolean =>
!CipherViewLikeUtils.isDeleted(c) && !CipherViewLikeUtils.isArchived(c);
private async getCipher(id: string, filter?: (c: CipherView) => boolean) {
const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId));

View File

@@ -0,0 +1,99 @@
// @ts-strict-ignore
import { of } from "rxjs";
import { CipherType } from "@bitwarden/common/vault/enums";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { SecureNoteView } from "@bitwarden/common/vault/models/view/secure-note.view";
import { ListCommand } from "./list.command";
function fullView(id: string, name: string, extra: Partial<CipherView> = {}): CipherView {
const view = new CipherView();
view.id = id;
view.name = name;
view.type = CipherType.SecureNote;
view.secureNote = new SecureNoteView();
return Object.assign(view, extra);
}
describe("ListCommand items", () => {
let command: ListCommand;
let cipherService: any;
beforeEach(() => {
cipherService = {
cipherViews$: jest
.fn()
.mockReturnValue(
of([
fullView("c1", "Alpha"),
fullView("c2", "Beta"),
fullView("c3", "Trashed", { deletedDate: new Date() }),
]),
),
getAllDecrypted: jest.fn(),
getAllDecryptedForUrl: jest.fn().mockResolvedValue([fullView("c9", "Url match")]),
};
const searchService = {
searchCiphersBasic: jest
.fn()
.mockImplementation((ciphers: CipherView[], query: string) =>
ciphers.filter((c) => c.name.toLowerCase().includes(query.toLowerCase())),
),
};
command = new ListCommand(
cipherService,
null,
null,
null,
searchService as any,
null,
null,
{ collectMany: jest.fn() } as any,
{ activeAccount$: of({ id: "user-1" }) } as any,
null,
{ filterRestrictedCiphers: jest.fn().mockImplementation(async (c) => c) } as any,
{ userCanArchive$: jest.fn().mockReturnValue(of(false)) } as any,
null,
);
});
const listedIds = (response: any): string[] => response.data.data.map((i: any) => i.id);
it("reads the cached stream instead of decrypting the vault", async () => {
const response = await command.run("items", {});
expect(cipherService.getAllDecrypted).not.toHaveBeenCalled();
expect(listedIds(response)).toEqual(["c1", "c2"]);
});
it("filters by search", async () => {
const response = await command.run("items", { search: "beta" });
expect(listedIds(response)).toEqual(["c2"]);
});
it("lists the trash on request", async () => {
const response = await command.run("items", { trash: true });
expect(listedIds(response)).toEqual(["c3"]);
});
it("matches a URL without the cached stream", async () => {
const response = await command.run("items", { url: "https://example.com" });
expect(cipherService.cipherViews$).not.toHaveBeenCalled();
expect(listedIds(response)).toEqual(["c9"]);
});
// A failed write clears the stream and nothing re-emits it, so waiting would hang.
it("decrypts directly when the cached stream was cleared", async () => {
cipherService.cipherViews$.mockReturnValue(of(null));
cipherService.getAllDecrypted.mockResolvedValue([fullView("c1", "Alpha")]);
const response = await command.run("items", {});
expect(listedIds(response)).toEqual(["c1"]);
});
});

View File

@@ -87,7 +87,11 @@ export class ListCommand {
if (options.url != null && options.url.trim() !== "") {
ciphers = await this.cipherService.getAllDecryptedForUrl(options.url, activeUserId);
} else {
ciphers = await this.cipherService.getAllDecrypted(activeUserId);
// The cached stream, so repeated requests (`bw serve`) do not decrypt the vault each time.
// Null once a write failed after clearing it: nothing re-emits, so decrypt directly.
ciphers =
(await firstValueFrom(this.cipherService.cipherViews$(activeUserId))) ??
(await this.cipherService.getAllDecrypted(activeUserId));
}
if (

View File

@@ -42,12 +42,12 @@ describe("PamFillCipherPolicy", () => {
describe("changed$", () => {
it("emits for decrypted stored cipher changes and revealed view changes", async () => {
const cipherViews$ = new Subject<CipherView[] | null>();
cipherService.cipherViews$.mockReturnValue(cipherViews$ as Observable<CipherView[]>);
const listViews$ = new Subject<CipherView[] | null>();
cipherService.cipherListViews$.mockReturnValue(listViews$ as Observable<CipherView[]>);
const emitted = firstValueFrom(policy.changed$(userId).pipe(take(3), toArray()));
cipherViews$.next(null);
cipherViews$.next([view("stored")]);
listViews$.next(null);
listViews$.next([view("stored")]);
reveal(view("revealed"));
expect(await emitted).toEqual([undefined, undefined, undefined]);
@@ -83,6 +83,34 @@ describe("PamFillCipherPolicy", () => {
});
});
describe("ciphersForIds", () => {
beforeEach(() => {
cipherService.getAllDecryptedForIds.mockImplementation(async (_userId, ids) =>
[view("stored")].filter((c) => ids.includes(c.id)),
);
});
it("appends the requested revealed views for a user fill", async () => {
reveal(view("revealed"), view("unrequested"));
const ciphers = await policy.ciphersForIds(
userId,
["stored", "revealed"],
CipherUse.UserFill,
);
expect(ciphers.map((c) => c.id)).toEqual(["stored", "revealed"]);
});
it.each(otherUses)("returns only the stored ciphers for %s", async (use) => {
reveal(view("revealed"));
const ciphers = await policy.ciphersForIds(userId, ["stored", "revealed"], use);
expect(ciphers.map((c) => c.id)).toEqual(["stored"]);
});
});
describe("ciphersForUrl", () => {
it("appends only the revealed views that match the URL for a user fill", async () => {
reveal(view("revealed"), view("other-site"));

View File

@@ -51,6 +51,24 @@ export class PamFillCipherPolicy extends DefaultFillCipherPolicy {
return withRevealed(ciphers, revealed);
}
override async ciphersForIds(
userId: UserId,
ids: string[],
use: CipherUse,
): Promise<CipherView[]> {
if (use !== CipherUse.UserFill) {
return super.ciphersForIds(userId, ids, use);
}
const [ciphers, revealed] = await Promise.all([
super.ciphersForIds(userId, ids, use),
this.getRevealed(userId),
]);
return withRevealed(
ciphers,
revealed.filter((view) => ids.includes(view.id)),
);
}
override async ciphersForUrl(
url: string,
userId: UserId,

View File

@@ -6,6 +6,7 @@ import { AccountService } from "@bitwarden/common/auth/abstractions/account.serv
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherViewLikeUtils } from "@bitwarden/common/vault/utils/cipher-view-like-utils";
import { DefaultSingleNudgeService } from "../default-single-nudge.service";
import { NudgeStatus, NudgeType } from "../nudges.service";
@@ -36,7 +37,7 @@ export class HasItemsNudgeService extends DefaultSingleNudgeService {
);
return combineLatest([
this.cipherService.cipherViews$(userId),
this.cipherService.cipherListViews$(userId),
this.getNudgeStatus$(nudgeType, userId),
profileDate$,
of(Date.now() - THIRTY_DAYS_MS),
@@ -44,7 +45,7 @@ export class HasItemsNudgeService extends DefaultSingleNudgeService {
switchMap(async ([ciphers, nudgeStatus, profileDate, profileCutoff]) => {
const profileOlderThanCutoff = profileDate.getTime() < profileCutoff;
const filteredCiphers = ciphers?.filter((cipher) => {
return cipher.deletedDate == null;
return !CipherViewLikeUtils.isDeleted(cipher);
});
if (

View File

@@ -4,6 +4,7 @@ import { combineLatest, filter, Observable, switchMap } from "rxjs";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherType } from "@bitwarden/common/vault/enums";
import { CipherViewLikeUtils } from "@bitwarden/common/vault/utils/cipher-view-like-utils";
import { DefaultSingleNudgeService } from "../default-single-nudge.service";
import { NudgeStatus, NudgeType } from "../nudges.service";
@@ -20,7 +21,7 @@ export class NewItemNudgeService extends DefaultSingleNudgeService {
nudgeStatus$(nudgeType: NudgeType, userId: UserId): Observable<NudgeStatus> {
return combineLatest([
this.getNudgeStatus$(nudgeType, userId),
this.cipherService.cipherViews$(userId).pipe(filter((ciphers) => ciphers != null)),
this.cipherService.cipherListViews$(userId).pipe(filter((ciphers) => ciphers != null)),
]).pipe(
switchMap(async ([nudgeStatus, ciphers]) => {
if (nudgeStatus.hasSpotlightDismissed) {
@@ -47,7 +48,9 @@ export class NewItemNudgeService extends DefaultSingleNudgeService {
break;
}
const ciphersBoolean = ciphers.some((cipher) => cipher.type === currentType);
const ciphersBoolean = ciphers.some(
(cipher) => CipherViewLikeUtils.getType(cipher) === currentType,
);
if (ciphersBoolean && !nudgeStatus.hasSpotlightDismissed) {
const dismissedStatus = {

View File

@@ -25,7 +25,7 @@ export class VaultSettingsImportNudgeService extends DefaultSingleNudgeService {
nudgeStatus$(nudgeType: NudgeType, userId: UserId): Observable<NudgeStatus> {
return combineLatest([
this.getNudgeStatus$(nudgeType, userId),
this.cipherService.cipherViews$(userId),
this.cipherService.cipherListViews$(userId),
this.organizationService.organizations$(userId),
this.collectionService.decryptedCollections$(userId),
]).pipe(

View File

@@ -7,9 +7,9 @@ import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abs
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId, CipherId } from "@bitwarden/common/types/guid";
import { UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherData } from "@bitwarden/common/vault/models/data/cipher.data";
import { CipherListView } from "@bitwarden/sdk-internal";
import { PremiumUpsellService } from "./premium-upsell.service";
@@ -21,7 +21,7 @@ describe("PremiumUpsellService", () => {
let hasPremiumSubject: BehaviorSubject<boolean>;
let accountAgeThresholdSubject: BehaviorSubject<number>;
let ciphersSubject: BehaviorSubject<Record<CipherId, CipherData>>;
let ciphersSubject: BehaviorSubject<CipherListView[]>;
const userId = "user-id" as UserId;
// Jan 1 creation date; tests run with system time set to Feb 15 = 45 days old
@@ -29,13 +29,8 @@ describe("PremiumUpsellService", () => {
const currentDate = new Date("2024-02-15T00:00:00.000Z");
const accountAgeInDays = 45;
const makeCiphers = (count: number): Record<CipherId, CipherData> => {
const ciphers = {} as Record<CipherId, CipherData>;
for (let i = 0; i < count; i++) {
ciphers[`cipher-${i}` as CipherId] = {} as CipherData;
}
return ciphers;
};
const makeCiphers = (count: number): CipherListView[] =>
Array.from({ length: count }, () => ({}) as CipherListView);
const createService = (options: { creationDate?: Date | undefined } = {}) => {
const accountService = mockAccountServiceWith(userId, {
@@ -61,7 +56,7 @@ describe("PremiumUpsellService", () => {
hasPremiumSubject = new BehaviorSubject<boolean>(false);
accountAgeThresholdSubject = new BehaviorSubject<number>(30);
ciphersSubject = new BehaviorSubject<Record<CipherId, CipherData>>(makeCiphers(5));
ciphersSubject = new BehaviorSubject<CipherListView[]>(makeCiphers(5));
billingAccountService = mock<BillingAccountProfileStateService>();
configService = mock<ConfigService>();
@@ -69,7 +64,7 @@ describe("PremiumUpsellService", () => {
billingAccountService.hasPremiumFromAnySource$.mockReturnValue(hasPremiumSubject);
configService.getFeatureFlag$.mockReturnValue(accountAgeThresholdSubject);
cipherService.ciphers$.mockReturnValue(ciphersSubject);
cipherService.cipherListViewsIncludingRestricted$.mockReturnValue(ciphersSubject);
service = createService();
});
@@ -141,8 +136,8 @@ describe("PremiumUpsellService", () => {
});
});
it("verifies ciphers$ is called with the active user id", () => {
expect(cipherService.ciphers$).toHaveBeenCalledWith(userId);
it("verifies cipherListViewsIncludingRestricted$ is called with the active user id", () => {
expect(cipherService.cipherListViewsIncludingRestricted$).toHaveBeenCalledWith(userId);
});
it("verifies hasPremiumFromAnySource$ is called with the active user id", () => {

View File

@@ -25,9 +25,11 @@ export class PremiumUpsellService {
return this.billingAccountService.hasPremiumFromAnySource$(userId);
}),
);
private ciphers$ = this.cipherService.ciphers$(this.activeUserId());
private cipherCount$ = this.cipherService
.cipherListViewsIncludingRestricted$(this.activeUserId())
.pipe(map((ciphers) => ciphers?.length ?? 0));
private readonly ciphers = toSignal(this.ciphers$, { initialValue: {} });
private readonly cipherCount = toSignal(this.cipherCount$, { initialValue: 0 });
private readonly hasPremium = toSignal(this.hasPremium$, { initialValue: true });
private readonly accountAgeFeatureFlag = toSignal(
this.configService.getFeatureFlag$(FeatureFlag.PM32180PremiumUpsellAccountAge),
@@ -47,11 +49,9 @@ export class PremiumUpsellService {
private readonly accountAgeInDays = toSignal(this.accountAgeInDays$, { initialValue: -1 });
showUpsell() {
const cipherCount = Object.keys(this.ciphers()).length;
return (
this.accountAgeInDays() >= this.accountAgeFeatureFlag() &&
cipherCount >= 5 &&
this.cipherCount() >= 5 &&
!this.hasPremium()
);
}

View File

@@ -56,6 +56,14 @@ describe("FidoAuthenticatorService", () => {
let authenticator!: Fido2AuthenticatorService<ParentWindowReference>;
let windowReference!: ParentWindowReference;
/** Serves `ciphers` as the vault: its list views, and lookups by id. */
const serveCiphers = (ciphers: CipherView[]) => {
cipherService.cipherListViews$.mockReturnValue(of(ciphers));
cipherService.getAllDecryptedForIds.mockImplementation(async (_userId, ids) =>
ciphers.filter((cipher) => ids.includes(cipher.id)),
);
};
beforeEach(async () => {
cipherService = mock<CipherService>();
userInterface = mock<Fido2UserInterfaceService<ParentWindowReference>>();
@@ -164,7 +172,7 @@ describe("FidoAuthenticatorService", () => {
cipherService.get.mockImplementation(async (id) =>
id === excludedCipher.id ? ({} as any) : undefined,
);
cipherService.getAllDecrypted.mockResolvedValue([excludedCipher]);
serveCiphers([excludedCipher]);
cipherService.decrypt.mockResolvedValue(excludedCipher);
});
@@ -235,7 +243,7 @@ describe("FidoAuthenticatorService", () => {
of({ [existingCipher.id as CipherId]: {} as CipherData }),
);
cipherService.getAllDecrypted.mockResolvedValue([existingCipher]);
serveCiphers([existingCipher]);
cipherService.decrypt.mockResolvedValue(existingCipher);
});
@@ -368,7 +376,7 @@ describe("FidoAuthenticatorService", () => {
of({ [cipher.id as CipherId]: {} as CipherData }),
);
cipherService.getAllDecrypted.mockResolvedValue([await cipher]);
serveCiphers([await cipher]);
cipherService.decrypt.mockResolvedValue(cipher);
cipherService.createWithServer.mockImplementation(async (cipherView, _userId) => {
cipherView.id = cipherId;
@@ -519,7 +527,7 @@ describe("FidoAuthenticatorService", () => {
**/
it("should inform user if no credential exists when fallback is not supported", async () => {
params.fallbackSupported = false;
cipherService.getAllDecrypted.mockResolvedValue([]);
serveCiphers([]);
userInterfaceSession.informCredentialNotFound.mockResolvedValue();
try {
@@ -534,7 +542,7 @@ describe("FidoAuthenticatorService", () => {
const cipher = await createCipherView({ type: CipherType.Login });
cipher.login.fido2Credentials[0].credentialId = credentialId;
cipher.login.fido2Credentials[0].rpId = "mismatch-rpid";
cipherService.getAllDecrypted.mockResolvedValue([cipher]);
serveCiphers([cipher]);
userInterfaceSession.informCredentialNotFound.mockResolvedValue();
try {
@@ -554,7 +562,7 @@ describe("FidoAuthenticatorService", () => {
allowCredentialDescriptorList: [],
rpId: RpId,
});
cipherService.getAllDecrypted.mockResolvedValue([]);
serveCiphers([]);
});
/** Spec: If credentialOptions is now empty, return an error code equivalent to "NotAllowedError" and terminate the operation. */
@@ -589,7 +597,7 @@ describe("FidoAuthenticatorService", () => {
})),
rpId: RpId,
});
cipherService.getAllDecrypted.mockResolvedValue(ciphers);
serveCiphers(ciphers);
});
it("should ask for all credentials in list when `params` contains allowedCredentials list", async () => {
@@ -697,7 +705,7 @@ describe("FidoAuthenticatorService", () => {
})),
rpId: RpId,
});
cipherService.getAllDecrypted.mockResolvedValue(ciphers);
serveCiphers(ciphers);
userInterfaceSession.pickCredential.mockResolvedValue({
cipherId: ciphers[0].id,
userVerified: false,
@@ -808,7 +816,7 @@ describe("FidoAuthenticatorService", () => {
{ credentialId, rpId: RpId, discoverable: true },
);
const ciphers = [cipher];
cipherService.getAllDecrypted.mockResolvedValue(ciphers);
serveCiphers(ciphers);
const result = await authenticator.silentCredentialDiscovery(RpId);

View File

@@ -6,7 +6,7 @@ import { SdkRandomNumberClient } from "@bitwarden/sdk-internal";
import { AccountService } from "../../../auth/abstractions/account.service";
import { getUserId } from "../../../auth/services/account.service";
import { CipherId } from "../../../types/guid";
import { CipherId, UserId } from "../../../types/guid";
import { CipherService } from "../../../vault/abstractions/cipher.service";
import { SyncService } from "../../../vault/abstractions/sync/sync.service.abstraction";
import { CipherRepromptType } from "../../../vault/enums/cipher-reprompt-type";
@@ -14,6 +14,8 @@ import { CipherType } from "../../../vault/enums/cipher-type";
import { Cipher } from "../../../vault/models/domain/cipher";
import { CipherView } from "../../../vault/models/view/cipher.view";
import { Fido2CredentialView } from "../../../vault/models/view/fido2-credential.view";
import { CipherViewLikeUtils } from "../../../vault/utils/cipher-view-like-utils";
import { filterOutNullish } from "../../../vault/utils/observable-utilities";
import {
Fido2AlgorithmIdentifier,
Fido2AuthenticatorError,
@@ -417,14 +419,11 @@ export class Fido2AuthenticatorService<
}
const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId));
const ciphers = await this.cipherService.getAllDecrypted(activeUserId);
const ciphers = await this.passkeyCiphers(activeUserId);
return ciphers
.filter(
(cipher) =>
!cipher.isDeleted &&
cipher.organizationId == undefined &&
cipher.type === CipherType.Login &&
cipher.login.hasFido2Credentials &&
ids.includes(cipher.login.fido2Credentials[0].credentialId),
)
.map((cipher) => cipher.id);
@@ -439,12 +438,9 @@ export class Fido2AuthenticatorService<
}
const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId));
const ciphers = await this.cipherService.getAllDecrypted(activeUserId);
const ciphers = await this.passkeyCiphers(activeUserId);
return ciphers.filter(
(cipher) =>
!cipher.isDeleted &&
cipher.type === CipherType.Login &&
cipher.login.hasFido2Credentials &&
cipher.login.fido2Credentials[0].rpId === rpId &&
credentials.some((credential) =>
compareCredentialIds(
@@ -455,14 +451,32 @@ export class Fido2AuthenticatorService<
);
}
/**
* Non-deleted login ciphers holding a passkey, fully decrypted. Picked on list views first,
* so a passkey lookup never decrypts the whole vault.
*/
private async passkeyCiphers(userId: UserId): Promise<CipherView[]> {
const listViews = await firstValueFrom(
this.cipherService.cipherListViews$(userId).pipe(filterOutNullish()),
);
const ids = listViews
.filter(
(cipher) =>
CipherViewLikeUtils.getType(cipher) === CipherType.Login &&
CipherViewLikeUtils.hasFido2Credentials(cipher) &&
!CipherViewLikeUtils.isDeleted(cipher),
)
.map((cipher) => CipherViewLikeUtils.getId(cipher))
.filter((id) => id != null);
return this.cipherService.getAllDecryptedForIds(userId, ids);
}
private async findCredentialsByRp(rpId: string): Promise<CipherView[]> {
const activeUserId = await firstValueFrom(this.accountService.activeAccount$.pipe(getUserId));
const ciphers = await this.cipherService.getAllDecrypted(activeUserId);
const ciphers = await this.passkeyCiphers(activeUserId);
return ciphers.filter(
(cipher) =>
!cipher.isDeleted &&
cipher.type === CipherType.Login &&
cipher.login.hasFido2Credentials &&
cipher.login.fido2Credentials[0].rpId === rpId &&
cipher.login.fido2Credentials[0].discoverable,
);

View File

@@ -39,6 +39,7 @@ import { CipherRepromptType } from "../enums/cipher-reprompt-type";
import { CipherType } from "../enums/cipher-type";
import { CipherPermissionsApi } from "../models/api/cipher-permissions.api";
import { CipherData } from "../models/data/cipher.data";
import { LocalData } from "../models/data/local.data";
import { Cipher } from "../models/domain/cipher";
import { AttachmentView } from "../models/view/attachment.view";
import { CipherView } from "../models/view/cipher.view";
@@ -46,7 +47,7 @@ import { LoginUriView } from "../models/view/login-uri.view";
import { LoginView } from "../models/view/login.view";
import { CipherService } from "./cipher.service";
import { DECRYPTED_CIPHERS, ENCRYPTED_CIPHERS } from "./key-state/ciphers.state";
import { ENCRYPTED_CIPHERS, LOCAL_DATA_KEY } from "./key-state/ciphers.state";
const ENCRYPTED_TEXT = "This data has been encrypted";
function encryptText(clearText: string | Uint8Array) {
@@ -629,6 +630,70 @@ describe("Cipher Service", () => {
expect(result).toEqual([normalListView, partialListView]);
});
it("attaches localData to list views without decrypting again when it changes", async () => {
const emitted: CipherListView[][] = [];
const subscription = cipherService
.cipherListViewsIncludingRestricted$(mockUserId)
.subscribe((views) => emitted.push(views));
await new Promise((resolve) => setTimeout(resolve));
const lastUsedDate = Date.now();
stateProvider.singleUser
.getFake(mockUserId, LOCAL_DATA_KEY)
.nextState({ normal: { lastUsedDate } } as unknown as Record<CipherId, LocalData>);
await new Promise((resolve) => setTimeout(resolve));
expect(cipherService["decryptCiphersWithSdk"]).toHaveBeenCalledTimes(1);
expect(emitted).toHaveLength(2);
expect(emitted.at(-1)?.[0].localData?.lastUsedDate).toBe(
new Date(lastUsedDate).toISOString(),
);
stateProvider.singleUser.getFake(mockUserId, LOCAL_DATA_KEY).nextState({});
subscription.unsubscribe();
});
it("leaves list views without localData untouched", async () => {
stateProvider.singleUser
.getFake(mockUserId, LOCAL_DATA_KEY)
.nextState({ other: { lastUsedDate: Date.now() } } as unknown as Record<
CipherId,
LocalData
>);
const result = await firstValueFrom(
cipherService.cipherListViewsIncludingRestricted$(mockUserId),
);
expect(result[0]).toBe(normalListView);
stateProvider.singleUser.getFake(mockUserId, LOCAL_DATA_KEY).nextState({});
});
it("keeps list views decrypted after a cache clear that finishes late", async () => {
const emitted: unknown[] = [];
const subscription = cipherService
.cipherListViewsIncludingRestricted$(mockUserId)
.subscribe((views) => emitted.push(views));
await new Promise((resolve) => setTimeout(resolve));
// Clearing writes state first, which is slow while the background is busy decrypting.
let finishStateClear!: () => void;
jest
.spyOn(cipherService as any, "clearDecryptedCiphersState")
.mockReturnValue(new Promise<void>((resolve) => (finishStateClear = resolve)));
const clearing = cipherService.clearCache(mockUserId);
// A decrypt of the latest ciphers completes while the clear is still running.
stateProvider.singleUser
.getFake(mockUserId, ENCRYPTED_CIPHERS)
.nextState({ [cipherData.id]: cipherData } as Record<CipherId, CipherData>);
await new Promise((resolve) => setTimeout(resolve));
finishStateClear();
await clearing;
expect(emitted.at(-1)).toEqual([normalListView, partialListView]);
subscription.unsubscribe();
});
});
it("getAllDecrypted excludes restricted ciphers (all imperative consumers: export, reports, autofill, CLI...)", async () => {
@@ -642,25 +707,26 @@ describe("Cipher Service", () => {
});
it("getAllDecryptedCiphersOfType excludes restricted ciphers (autofill card/identity suggestions)", async () => {
const card = { type: { card: {} } };
(cipherService as any).cipherListViewsIncludingRestricted$ = jest.fn().mockReturnValue(
of([
{ ...card, id: "card-normal", partial: false },
{ ...card, id: "card-partial", partial: true },
{ type: { login: {} }, id: "login", partial: false },
]),
);
const normalCard = new CipherView(encryptionContext.cipher);
normalCard.id = "card-normal" as CipherId;
normalCard.type = CipherType.Card;
normalCard.partial = false;
const partialCard = new CipherView(encryptionContext.cipher);
partialCard.id = "card-partial" as CipherId;
partialCard.type = CipherType.Card;
partialCard.partial = true;
jest
.spyOn(cipherService as any, "getAllDecryptedIncludingRestricted")
.mockResolvedValue([normalCard, partialCard]);
const decryptIds = jest
.spyOn(cipherService as any, "decryptIds")
.mockResolvedValue([normalCard]);
const result = await (cipherService as any).getAllDecryptedCiphersOfType(
[CipherType.Card],
mockUserId,
);
// Only the matching, non-partial cipher is fully decrypted.
expect(decryptIds).toHaveBeenCalledWith(mockUserId, ["card-normal"]);
expect(result).toEqual([normalCard]);
});
@@ -1634,11 +1700,6 @@ describe("Cipher Service", () => {
});
describe("getAllDecrypted()", () => {
beforeEach(() => {
// Clear the decrypted cache to ensure we test the decrypt path
stateProvider.singleUser.getFake(mockUserId, DECRYPTED_CIPHERS).nextState({});
});
it("should use SDK to list and decrypt ciphers", async () => {
const mockCipherView1 = new CipherView();
mockCipherView1.name = "Test Cipher 1";
@@ -1665,22 +1726,16 @@ describe("Cipher Service", () => {
expect(result[1]).toBeInstanceOf(CipherView);
});
it("should serve the decrypted cache without calling the SDK when the cache is populated", async () => {
const cachedView = new CipherView();
cachedView.id = "cached-cipher-id" as CipherId;
cachedView.name = "Cached Cipher";
it("decrypts on every call", async () => {
jest
.spyOn(cipherService, "ciphers$")
.mockReturnValue(of({ a: { ...cipherData, id: "a" } } as Record<CipherId, CipherData>));
cipherEncryptionService.decrypt.mockResolvedValue(new CipherView());
stateProvider.singleUser
.getFake(mockUserId, DECRYPTED_CIPHERS)
.nextState({ [cachedView.id]: cachedView });
await cipherService.getAllDecrypted(mockUserId);
await cipherService.getAllDecrypted(mockUserId);
const sdkServiceSpy = jest.spyOn(cipherSdkService, "getAllDecrypted");
const result = await cipherService.getAllDecrypted(mockUserId);
expect(sdkServiceSpy).not.toHaveBeenCalled();
expect(result).toHaveLength(1);
expect(result[0].name).toBe("Cached Cipher");
expect(cipherEncryptionService.decrypt).toHaveBeenCalledTimes(2);
});
});
@@ -1901,6 +1956,48 @@ describe("Cipher Service", () => {
});
});
describe("decrypting only the ciphers needed", () => {
const otherId = "other-cipher-id" as CipherId;
const id = cipherData.id as CipherId;
const viewOf = (viewId: string, partial = false) =>
Object.assign(new CipherView(), { id: viewId, partial });
beforeEach(() => {
stateProvider.singleUser.getFake(mockUserId, ENCRYPTED_CIPHERS).nextState({
[id]: cipherData,
[otherId]: { ...cipherData, id: otherId },
} as Record<CipherId, CipherData>);
keyService.cipherDecryptionKeys$.mockReturnValue(of({} as CipherDecryptionKeys));
(cipherService as any).cipherListViewsIncludingRestricted$ = jest
.fn()
.mockReturnValue(of([{ id }, { id: otherId }]));
});
it("getAllDecryptedForUrl fully decrypts the matching ciphers only, never the whole vault", async () => {
jest.spyOn(cipherService, "filterCiphersForUrl").mockResolvedValue([{ id } as any]);
cipherEncryptionService.decryptManyLegacy.mockResolvedValue([[viewOf(id)], []]);
const wholeVault = jest.spyOn(cipherService as any, "getAllDecryptedIncludingRestricted");
const result = await cipherService.getAllDecryptedForUrl("https://example.com", mockUserId);
const [decrypted] = cipherEncryptionService.decryptManyLegacy.mock.calls[0];
expect(decrypted.map((cipher) => cipher.id)).toEqual([id]);
expect(result.map((cipher) => cipher.id)).toEqual([id]);
expect(wholeVault).not.toHaveBeenCalled();
});
it("getAllDecryptedForIds keeps the requested order and drops partial ciphers", async () => {
cipherEncryptionService.decryptManyLegacy.mockResolvedValue([
[viewOf(id, true), viewOf(otherId)],
[],
]);
const result = await cipherService.getAllDecryptedForIds(mockUserId, [otherId, id]);
expect(result.map((cipher) => cipher.id)).toEqual([otherId]);
});
});
describe("getCipherForUrl localData freshness", () => {
beforeEach(() => {
Object.defineProperty(autofillSettingsService, "autofillOnPageLoadDefault$", {

View File

@@ -46,7 +46,7 @@ import { FieldType } from "../enums";
import { CipherType } from "../enums/cipher-type";
import { CipherUpdateRoute } from "../enums/cipher-update-route";
import { CipherData } from "../models/data/cipher.data";
import { LocalData } from "../models/data/local.data";
import { LocalData, toSdkLocalData } from "../models/data/local.data";
import { Cipher } from "../models/domain/cipher";
import { SortedCiphersCache } from "../models/domain/sorted-ciphers-cache";
import { CipherWithIdRequest } from "../models/request/cipher-with-id.request";
@@ -59,7 +59,6 @@ import { hydrateCiphersWithLocalData } from "../utils/hydrate-ciphers-with-local
import {
ADD_EDIT_CIPHER_INFO_KEY,
DECRYPTED_CIPHERS,
ENCRYPTED_CIPHERS,
FAILED_DECRYPTED_CIPHERS,
LOCAL_DATA_KEY,
@@ -152,16 +151,15 @@ export class CipherService implements CipherServiceAbstraction {
cipherListViewsIncludingRestricted$ = perUserCache$((userId: UserId) => {
let decryptMeasurement: Measurement;
return combineLatest([
// `localData` stays out of this stream. It changes on every autofill and would decrypt the
// whole vault again, so it is attached to the decrypted views below instead.
const decrypted$ = combineLatest([
this.encryptedCiphersState(userId).state$,
this.localData$(userId),
this.keyService.cipherDecryptionKeys$(userId, true),
]).pipe(
filter(([cipherDataState, _, keys]) => cipherDataState != null && keys != null),
map(([cipherDataState, localData]) =>
Object.values(cipherDataState).map(
(cipherData) => new Cipher(cipherData, localData?.[cipherData.id as CipherId]),
),
filter(([cipherDataState, keys]) => cipherDataState != null && keys != null),
map(([cipherDataState]) =>
Object.values(cipherDataState).map((cipherData) => new Cipher(cipherData)),
),
tap(() => {
decryptMeasurement = this.logService.startMeasurement(
@@ -180,6 +178,12 @@ export class CipherService implements CipherServiceAbstraction {
}),
map(([decrypted]) => decrypted),
);
// Emits on `localData` changes too, so the popup and `FillCipherPolicy.changed$` see the
// last-used ordering without a new decrypt.
return combineLatest([decrypted$, this.localData$(userId)]).pipe(
map(([views, localData]) => this.attachLocalData(views, localData)),
);
}, this.clearCipherViewsForUser$);
/**
@@ -198,6 +202,17 @@ export class CipherService implements CipherServiceAbstraction {
);
}, this.clearCipherViewsForUser$);
/** Re-attaches the client-only `localData` the SDK can't know about, without decrypting again. */
private attachLocalData(
views: CipherListView[],
localData: Record<CipherId, LocalData>,
): CipherListView[] {
return views.map((view) => {
const data = view.id == null ? null : localData[uuidAsString(view.id) as CipherId];
return data == null ? view : { ...view, localData: toSdkLocalData(data) };
});
}
/** Overloaded so a caller's array type is kept rather than widened to an array of the union. */
private excludeRestricted(views: CipherView[] | null): CipherView[] | null;
private excludeRestricted(
@@ -229,15 +244,6 @@ export class CipherService implements CipherServiceAbstraction {
);
}, this.clearCipherViewsForUser$);
async setDecryptedCipherCache(value: CipherView[], userId: UserId) {
// Sometimes we might prematurely decrypt the vault and that will result in no ciphers
// if we cache it then we may accidentally return it when it's not right, we'd rather try decryption again.
// We still want to set null though, that is the indicator that the cache isn't valid and we should do decryption.
if (value == null || value.length !== 0) {
await this.setDecryptedCiphers(value, userId);
}
}
async setFailedDecryptedCiphers(cipherViews: CipherView[], userId: UserId) {
await this.stateProvider.setUserState(FAILED_DECRYPTED_CIPHERS, cipherViews, userId);
}
@@ -261,27 +267,15 @@ export class CipherService implements CipherServiceAbstraction {
);
}
private async setDecryptedCiphers(value: CipherView[], userId: UserId) {
const measurement = this.logService.startMeasurement(
PERF_TRACK_GROUP,
PERF_TRACK,
"setDecryptedCiphers",
);
const cipherViews: { [id: string]: CipherView } = {};
value?.forEach((c) => {
cipherViews[c.id] = c;
});
await this.stateProvider.setUserState(DECRYPTED_CIPHERS, cipherViews, userId);
measurement.finish([["Items", value?.length ?? 0]]);
}
async clearCache(userId?: UserId): Promise<void> {
const activeUserId = await firstValueFrom(this.stateProvider.activeUserId$);
userId ??= activeUserId;
await this.clearDecryptedCiphersState(userId);
// Force the cached cipherView$ observable(s) to emit a null value
// Force the cached cipherView$ observable(s) to emit a null value. Before the state write
// below: a decrypt that finishes while it runs must not be overwritten by the null after,
// which would leave the views null until the ciphers change again.
this.clearCipherViewsForUser$.next(userId);
await this.clearDecryptedCiphersState(userId);
}
/**
@@ -358,19 +352,15 @@ export class CipherService implements CipherServiceAbstraction {
}
/**
* Like {@link getAllDecrypted} but keeps restricted ciphers, and fills the decrypted-cipher cache.
* Like {@link getAllDecrypted} but keeps restricted ciphers. Decrypts on every call: the decrypted
* vault is not cached in state, which would keep every password decrypted for as long as the
* vault is unlocked (in the extension, in session storage too).
* Everything except {@link cipherViewsIncludingRestricted$} must go through {@link getAllDecrypted}.
*/
private async getAllDecryptedIncludingRestricted(userId: UserId): Promise<CipherView[]> {
const decCiphers = await this.getDecryptedCiphers(userId);
if (decCiphers != null && decCiphers.length !== 0) {
return decCiphers;
}
// `localData` (e.g. lastUsedDate) is a client-only field the SDK does not populate on the
// decrypted views. Re-attach it here so every consumer of `cipherViews$`/`getAllDecrypted`
// sees it, matching the legacy and list-view decryption paths.
// (Note, the cached views above are already hydrated)
let localData: Record<CipherId, LocalData> | undefined;
// Wrap `localData` in try-catch so a failure to hydrate data doesn't cascade to a larger failed experience
@@ -398,7 +388,6 @@ export class CipherService implements CipherServiceAbstraction {
this.getLocaleSortingFunction(),
);
await this.setDecryptedCipherCache(sortedSuccesses, userId);
await this.setFailedDecryptedCiphers(result.failures, userId);
return sortedSuccesses;
@@ -428,12 +417,6 @@ export class CipherService implements CipherServiceAbstraction {
return result;
}
private async getDecryptedCiphers(userId: UserId) {
return Object.values(
await firstValueFrom(this.decryptedCiphersState(userId).state$.pipe(map((c) => c ?? {}))),
);
}
/**
* Decrypts a cipher using either the SDK or the legacy method based on the feature flag.
* @param cipher The cipher to decrypt.
@@ -476,25 +459,77 @@ export class CipherService implements CipherServiceAbstraction {
defaultMatch: UriMatchStrategySetting = null,
overrideNeverMatchStrategy?: true,
): Promise<CipherView[]> {
return await firstValueFrom(
this.cipherViews$(userId).pipe(
filter((c) => c != null),
switchMap(
async (ciphers) =>
await this.filterCiphersForUrl<CipherView>(
ciphers,
url,
includeOtherTypes,
defaultMatch,
overrideNeverMatchStrategy,
),
),
),
// Matches on list views, then fully decrypts the matches only: callers such as the badge
// count run on every tab change, and a full decrypt of the vault would keep every password
// decrypted in memory.
const listViews = await this.loadedListViews(userId);
const matches = await this.filterCiphersForUrl(
listViews,
url,
includeOtherTypes,
defaultMatch,
overrideNeverMatchStrategy,
);
return this.decryptIds(
userId,
matches.filter((view) => view.id != null).map((view) => uuidAsString(view.id!)),
);
}
async getAllDecryptedForIds(userId: UserId, ids: string[]): Promise<CipherView[]> {
return this.decryptedForIds(this.cipherViews$(userId), ids);
await this.loadedListViews(userId);
return this.decryptIds(userId, ids);
}
/**
* List views once decrypted, which also means the encrypted ciphers and keys are loaded.
* Excludes restricted ciphers, like {@link cipherListViews$}.
*/
private async loadedListViews(userId: UserId): Promise<CipherListView[]> {
const views = await firstValueFrom(
this.cipherListViewsIncludingRestricted$(userId).pipe(filterOutNullish()),
);
return views.filter((view) => !CipherViewLikeUtils.isRestricted(view));
}
/**
* Full views of the ciphers `ids`, in that order, decrypted on demand from the encrypted
* state. Excludes restricted ciphers, like {@link cipherViews$}.
*/
private async decryptIds(userId: UserId, ids: string[]): Promise<CipherView[]> {
if (ids.length === 0) {
return [];
}
const [encrypted, localData] = await Promise.all([
firstValueFrom(this.ciphers$(userId)),
firstValueFrom(this.localData$(userId)),
]);
const ciphers: Cipher[] = [];
for (const id of ids) {
const data = encrypted[id as CipherId];
if (data == null) {
continue;
}
ciphers.push(new Cipher(data, localData[id as CipherId]));
}
const decryptable = await this.excludeCiphersMissingOrgKey(ciphers, userId);
const [successes, failures] = await this.cipherEncryptionService.decryptManyLegacy(
decryptable,
userId,
);
if (failures.length > 0) {
this.logService.warning(`Failed to decrypt ${failures.length} requested ciphers.`);
}
const views = new Map(
hydrateCiphersWithLocalData(successes, localData).map((view) => [view.id, view]),
);
return this.excludeRestricted(ids.map((id) => views.get(id)).filter((view) => view != null));
}
/** @inheritdoc */
@@ -575,12 +610,19 @@ export class CipherService implements CipherServiceAbstraction {
type: CipherType[],
userId: UserId,
): Promise<CipherView[]> {
const ciphers = await this.getAllDecrypted(userId);
return ciphers
.filter(
(cipher) => cipher.deletedDate == null && !cipher.isArchived && type.includes(cipher.type),
)
.sort((a, b) => this.sortCiphersByLastUsedThenName(a, b));
// Picks the type on list views, so only ciphers of that type are fully decrypted.
const listViews = (await this.loadedListViews(userId)).filter(
(cipher) =>
!CipherViewLikeUtils.isDeleted(cipher) &&
!CipherViewLikeUtils.isArchived(cipher) &&
type.includes(CipherViewLikeUtils.getType(cipher)),
);
const ciphers = await this.decryptIds(
userId,
listViews.filter((view) => view.id != null).map((view) => uuidAsString(view.id!)),
);
return ciphers.sort((a, b) => this.sortCiphersByLastUsedThenName(a, b));
}
async getAllFromApiForOrganization(
@@ -736,20 +778,6 @@ export class CipherService implements CipherServiceAbstraction {
}
await this.localDataState(userId).update(() => ciphersLocalData);
const decryptedCipherCache = await this.getDecryptedCiphers(userId);
if (!decryptedCipherCache) {
return;
}
for (let i = 0; i < decryptedCipherCache.length; i++) {
const cached = decryptedCipherCache[i];
if (cached.id === id) {
cached.localData = ciphersLocalData[id as CipherId];
break;
}
}
await this.setDecryptedCiphers(decryptedCipherCache, userId);
}
async updateLastLaunchedDate(id: string, userId: UserId): Promise<void> {
@@ -763,20 +791,6 @@ export class CipherService implements CipherServiceAbstraction {
ciphersLocalData[id as CipherId] = { lastLaunched: currentTime, lastUsedDate: currentTime };
await this.localDataState(userId).update(() => ciphersLocalData);
const decryptedCipherCache = await this.getDecryptedCiphers(userId);
if (!decryptedCipherCache) {
return;
}
for (let i = 0; i < decryptedCipherCache.length; i++) {
const cached = decryptedCipherCache[i];
if (cached.id === id) {
cached.localData = ciphersLocalData[id as CipherId];
break;
}
}
await this.setDecryptedCiphers(decryptedCipherCache, userId);
}
async saveNeverDomain(domain: string): Promise<void> {
@@ -1539,13 +1553,6 @@ export class CipherService implements CipherServiceAbstraction {
return this.stateProvider.getUser(userId, ENCRYPTED_CIPHERS);
}
/**
* @returns a SingleUserState for the decrypted ciphers
*/
private decryptedCiphersState(userId: UserId) {
return this.stateProvider.getUser(userId, DECRYPTED_CIPHERS);
}
/**
* @returns a SingleUserState for the add/edit cipher info
*/
@@ -1719,7 +1726,6 @@ export class CipherService implements CipherServiceAbstraction {
}
private async clearDecryptedCiphersState(userId: UserId) {
await this.setDecryptedCiphers(null, userId);
await this.setFailedDecryptedCiphers(null, userId);
this.clearSortedCiphers();
}

View File

@@ -19,15 +19,6 @@ export const ENCRYPTED_CIPHERS = UserKeyDefinition.record<CipherData>(CIPHERS_DI
clearOn: ["logout"],
});
export const DECRYPTED_CIPHERS = UserKeyDefinition.record<CipherView>(
CIPHERS_MEMORY,
"decryptedCiphers",
{
deserializer: (cipher: Jsonify<CipherView>) => CipherView.fromJSON(cipher),
clearOn: ["logout", "lock"],
},
);
export const FAILED_DECRYPTED_CIPHERS = UserKeyDefinition.array<CipherView>(
CIPHERS_MEMORY,
"failedDecryptedCiphers",

View File

@@ -24,15 +24,18 @@ const fakeUserState = () =>
state$: of(null),
}) as unknown as FakeSingleUserState<AtRiskPasswordCalloutData>;
/** The fields of a `CipherListView` the service reads. */
class MockCipherView {
type = { login: {} };
deletedDate?: string;
constructor(
public id: string,
private deleted: boolean,
deleted: boolean,
public edit: boolean = true,
public viewPassword: boolean = true,
) {}
get isDeleted() {
return this.deleted;
) {
this.deletedDate = deleted ? new Date().toISOString() : undefined;
}
}
@@ -42,7 +45,7 @@ describe("AtRiskPasswordCalloutService", () => {
pendingTasks$: jest.fn(),
completedTasks$: jest.fn(),
};
const mockCipherService = { cipherViews$: jest.fn() };
const mockCipherService = { cipherListViews$: jest.fn() };
const mockStateProvider = { getUser: jest.fn().mockReturnValue(fakeUserState()) };
const userId: UserId = "user1" as UserId;
beforeEach(() => {
@@ -207,7 +210,7 @@ describe("AtRiskPasswordCalloutService", () => {
},
])("$description", async ({ tasks, ciphers, expectedLength, expectedFirstId }) => {
jest.spyOn(mockTaskService, "pendingTasks$").mockReturnValue(of(tasks));
jest.spyOn(mockCipherService, "cipherViews$").mockReturnValue(of(ciphers));
jest.spyOn(mockCipherService, "cipherListViews$").mockReturnValue(of(ciphers));
const result = await firstValueFrom(service.pendingTasks$(userId));
@@ -259,7 +262,7 @@ describe("AtRiskPasswordCalloutService", () => {
];
jest.spyOn(mockTaskService, "pendingTasks$").mockReturnValue(of(tasks));
jest.spyOn(mockCipherService, "cipherViews$").mockReturnValue(of(ciphers));
jest.spyOn(mockCipherService, "cipherListViews$").mockReturnValue(of(ciphers));
const result = await firstValueFrom(service.pendingTasks$(userId));
@@ -287,7 +290,7 @@ describe("AtRiskPasswordCalloutService", () => {
},
])("$description", async ({ tasks, ciphers }) => {
jest.spyOn(mockTaskService, "pendingTasks$").mockReturnValue(of(tasks));
jest.spyOn(mockCipherService, "cipherViews$").mockReturnValue(of(ciphers));
jest.spyOn(mockCipherService, "cipherListViews$").mockReturnValue(of(ciphers));
const result = await firstValueFrom(service.pendingTasks$(userId));
@@ -337,7 +340,7 @@ describe("AtRiskPasswordCalloutService", () => {
beforeEach(() => {
jest.spyOn(mockTaskService, "pendingTasks$").mockReturnValue(of([]));
jest.spyOn(mockTaskService, "completedTasks$").mockReturnValue(of([]));
jest.spyOn(mockCipherService, "cipherViews$").mockReturnValue(of([]));
jest.spyOn(mockCipherService, "cipherListViews$").mockReturnValue(of([]));
});
it("returns false if banner has been dismissed", async () => {
@@ -369,7 +372,7 @@ describe("AtRiskPasswordCalloutService", () => {
};
jest.spyOn(mockTaskService, "completedTasks$").mockReturnValue(of(completedTasks));
jest.spyOn(mockCipherService, "cipherViews$").mockReturnValue(of(ciphers));
jest.spyOn(mockCipherService, "cipherListViews$").mockReturnValue(of(ciphers));
mockStateProvider.getUser.mockReturnValue({ state$: of(state) });
const result = await firstValueFrom(service.showCompletedTasksBanner$(userId));

View File

@@ -9,6 +9,7 @@ import {
} from "@bitwarden/common/platform/state";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { SecurityTask, SecurityTaskType, TaskService } from "@bitwarden/common/vault/tasks";
import { CipherViewLikeUtils } from "@bitwarden/common/vault/utils/cipher-view-like-utils";
import { UserId } from "@bitwarden/user-core";
export type AtRiskPasswordCalloutData = {
@@ -36,18 +37,19 @@ export class AtRiskPasswordCalloutService {
pendingTasks$(userId: UserId): Observable<SecurityTask[]> {
return combineLatest([
this.taskService.pendingTasks$(userId),
this.cipherService.cipherViews$(userId),
// List views: only ids and permissions are needed, not their decrypted contents.
this.cipherService.cipherListViews$(userId),
]).pipe(
map(([tasks, ciphers]) => {
return tasks.filter((t: SecurityTask) => {
const associatedCipher = ciphers.find((c) => c.id === t.cipherId);
const associatedCipher = ciphers.find((c) => CipherViewLikeUtils.getId(c) === t.cipherId);
return (
t.type === SecurityTaskType.UpdateAtRiskCredential &&
associatedCipher &&
associatedCipher.edit &&
associatedCipher.viewPassword &&
!associatedCipher.isDeleted
!CipherViewLikeUtils.isDeleted(associatedCipher)
);
});
}),