mirror of
https://github.com/bitwarden/browser
synced 2025-12-06 00:13:28 +00:00
PM-23906 Wrap sdk callsite with try/catch to handle errors appropriately (#16410)
* wrap sdk callsite in try/catch to handle error appropriately `encryptService.decryptString()` calls code in the internal SDK which when provided an invalid key returns `CryptoError::InvalidMac`. The originating callsite has been wrapped in a try/catch in order to intercept the error and return false so that logic in parse() may return a more appropriate error message in the UI. * add unit test and explanatory comment * remove misleading comment * remove null comparison and unused variable
This commit is contained in:
@@ -136,5 +136,17 @@ describe("BitwardenPasswordProtectedImporter", () => {
|
||||
jDoc.data = null;
|
||||
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
||||
});
|
||||
|
||||
it("returns invalidFilePassword errorMessage if decryptString throws", async () => {
|
||||
encryptService.decryptString.mockImplementation(() => {
|
||||
throw new Error("SDK error");
|
||||
});
|
||||
i18nService.t.mockReturnValue("invalidFilePassword");
|
||||
|
||||
const result = await importer.parse(JSON.stringify(jDoc));
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.errorMessage).toBe("invalidFilePassword");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -90,14 +90,12 @@ export class BitwardenPasswordProtectedImporter extends BitwardenJsonImporter im
|
||||
|
||||
const encKeyValidation = new EncString(jdoc.encKeyValidation_DO_NOT_EDIT);
|
||||
|
||||
const encKeyValidationDecrypt = await this.encryptService.decryptString(
|
||||
encKeyValidation,
|
||||
this.key,
|
||||
);
|
||||
if (encKeyValidationDecrypt === null) {
|
||||
try {
|
||||
await this.encryptService.decryptString(encKeyValidation, this.key);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private cannotParseFile(jdoc: BitwardenPasswordProtectedFileFormat): boolean {
|
||||
|
||||
Reference in New Issue
Block a user