Skip to content

Commit

Permalink
feat: Reuse stored papers/contacts if sync when vault locked
Browse files Browse the repository at this point in the history
We need the encryption key to convert papers/contacts. So if we try to do a sync when the vault is locked, we will have no papers/contacts. In some case, another sync is triggered when we unvault the lock, but if we unlock the vault when the previous sync is still in progress AND previous sync already passed the papers/contacts conversion (which failed), it can result to empty papers/contacts.

In this case, we want to reuse stored papers/contacts.
  • Loading branch information
zatteo committed Apr 19, 2024
1 parent 52bfa43 commit 5a926a9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
15 changes: 14 additions & 1 deletion libs/common/src/vault/services/sync/sync.service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { CozyClientService } from "../../../../../../apps/browser/src/popup/services/cozyClient.service";
import { fetchContactsAndConvertAsCiphers } from "../../../../../../libs/cozy/contactCipher";
import { fetchPapersAndConvertAsCiphers } from "../../../../../../libs/cozy/paperCipher";
Expand Down Expand Up @@ -110,7 +111,6 @@ export class SyncService implements SyncServiceAbstraction {
await this.syncProfile(response.profile);
await this.syncFolders(response.folders);
await this.syncCollections(response.collections);
await this.syncCiphers(response.ciphers);

// Cozy customization
await this.cozyClientService.getClientInstance();
Expand All @@ -132,11 +132,24 @@ export class SyncService implements SyncServiceAbstraction {

const [papersPromise, contactsPromise] = await Promise.allSettled(fetchPromises);

/*
Because syncCiphers replace previous ciphers, we need to do syncCiphers :
- after we converted papers and contacts because papers and contacts conversion
can try to reuse previous ciphers
- before we upsert papers and contacts because we want to have
bitwarden ciphers and papers and contacts ciphers
*/
await this.syncCiphers(response.ciphers);

if (papersPromise.status === "fulfilled") {
console.log(`${papersPromise.value.length} contacts ciphers will be added`);

await this.cipherService.upsert(papersPromise.value);
}

if (contactsPromise.status === "fulfilled") {
console.log(`${contactsPromise.value.length} papers ciphers will be added`);

await this.cipherService.upsert(contactsPromise.value);
}
// Cozy customization end
Expand Down
13 changes: 9 additions & 4 deletions libs/cozy/contactCipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherType } from "@bitwarden/common/vault/enums/cipher-type";
import { CipherData } from "@bitwarden/common/vault/models/data/cipher.data";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";

Expand All @@ -27,6 +28,10 @@ const convertContactsAsCiphers = async (

contactsCiphers.push(cipherData);
} catch (e) {
if (e.message === "No encryption key provided.") {
throw e;
}

console.log(`Error during conversion of contact ${contact.id}`, contact, e);
}
}
Expand All @@ -52,13 +57,13 @@ export const fetchContactsAndConvertAsCiphers = async (
contacts
);

console.log(`${contactsCiphers.length} contacts ciphers will be added`);

return contactsCiphers;
} catch (e) {
console.log("Error while fetching contacts and converting them as ciphers", e);
console.log("Error while fetching contacts and converting them as ciphers. Fallbacking to stored contacts.", e);

return [];
return (await cipherService.getAll())
.filter((cipher: any) => cipher.type === CipherType.Contact)
.map((cipher: any) => cipher.toCipherData());
}
};

Expand Down
13 changes: 9 additions & 4 deletions libs/cozy/paperCipher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { StateService } from "@bitwarden/common/abstractions/state.service";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherType } from "@bitwarden/common/vault/enums/cipher-type";
import { CipherData } from "@bitwarden/common/vault/models/data/cipher.data";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";

Expand Down Expand Up @@ -56,6 +57,10 @@ const convertPapersAsCiphers = async (

papersCiphers.push(cipherData);
} catch (e) {
if (e.message === "No encryption key provided.") {
throw e;
}

console.log(`Error during conversion of paper ${paper.id}`, paper, e);
}
}
Expand All @@ -82,13 +87,13 @@ export const fetchPapersAndConvertAsCiphers = async (
papers
);

console.log(`${papersCiphers.length} papers ciphers will be added`);

return papersCiphers;
} catch (e) {
console.log("Error while fetching papers and converting them as ciphers", e);
console.log("Error while fetching papers and converting them as ciphers. Fallbacking to stored papers.", e);

return [];
return (await cipherService.getAll())
.filter((cipher: any) => cipher.type === CipherType.Paper)
.map((cipher: any) => cipher.toCipherData());
}
};

Expand Down

0 comments on commit 5a926a9

Please sign in to comment.