1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 17:23:37 +00:00

Update CipherService

This commit is contained in:
Justin Baur
2024-05-24 16:41:04 -04:00
parent 363d71c1ac
commit 12e9df5fc9

View File

@@ -135,8 +135,7 @@ export class CipherService implements CipherServiceAbstraction {
this.addEditCipherInfo$ = this.addEditCipherInfoState.state$; this.addEditCipherInfo$ = this.addEditCipherInfoState.state$;
} }
async setDecryptedCipherCache(value: CipherView[]) { async setDecryptedCipherCache(value: CipherView[], userId: UserId) {
const userId = await firstValueFrom(this.stateProvider.activeUserId$);
// Sometimes we might prematurely decrypt the vault and that will result in no ciphers // 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. // 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. // We still want to set null though, that is the indicator that the cache isn't valid and we should do decryption.
@@ -367,9 +366,15 @@ export class CipherService implements CipherServiceAbstraction {
return await this.getDecryptedCiphers(); return await this.getDecryptedCiphers();
} }
decCiphers = await this.decryptCiphers(await this.getAll()); const activeUserId = await firstValueFrom(this.stateProvider.activeUserId$);
await this.setDecryptedCipherCache(decCiphers); if (activeUserId == null) {
return [];
}
decCiphers = await this.decryptCiphers(await this.getAll(), activeUserId);
await this.setDecryptedCipherCache(decCiphers, activeUserId);
return decCiphers; return decCiphers;
} }
@@ -377,10 +382,10 @@ export class CipherService implements CipherServiceAbstraction {
return Object.values(await firstValueFrom(this.cipherViews$)); return Object.values(await firstValueFrom(this.cipherViews$));
} }
private async decryptCiphers(ciphers: Cipher[]) { private async decryptCiphers(ciphers: Cipher[], userId: UserId) {
const orgKeys = await this.cryptoService.getOrgKeys(); const keys = await firstValueFrom(this.cryptoService.cipherDecryptionKeys$(userId, true));
const userKey = await this.cryptoService.getUserKeyWithLegacySupport();
if (Object.keys(orgKeys).length === 0 && userKey == null) { if (keys == null || (keys.userKey == null && Object.keys(keys.orgKeys).length === 0)) {
// return early if there are no keys to decrypt with // return early if there are no keys to decrypt with
return; return;
} }
@@ -398,7 +403,10 @@ export class CipherService implements CipherServiceAbstraction {
const decCiphers = ( const decCiphers = (
await Promise.all( await Promise.all(
Object.entries(grouped).map(([orgId, groupedCiphers]) => Object.entries(grouped).map(([orgId, groupedCiphers]) =>
this.encryptService.decryptItems(groupedCiphers, orgKeys[orgId] ?? userKey), this.encryptService.decryptItems(
groupedCiphers,
keys.orgKeys[orgId as OrganizationId] ?? keys.userKey,
),
), ),
) )
) )