mirror of
https://github.com/bitwarden/browser
synced 2025-12-10 21:33:27 +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;
|
jDoc.data = null;
|
||||||
expect((await importer.parse(JSON.stringify(jDoc))).success).toEqual(false);
|
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 encKeyValidation = new EncString(jdoc.encKeyValidation_DO_NOT_EDIT);
|
||||||
|
|
||||||
const encKeyValidationDecrypt = await this.encryptService.decryptString(
|
try {
|
||||||
encKeyValidation,
|
await this.encryptService.decryptString(encKeyValidation, this.key);
|
||||||
this.key,
|
return true;
|
||||||
);
|
} catch {
|
||||||
if (encKeyValidationDecrypt === null) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private cannotParseFile(jdoc: BitwardenPasswordProtectedFileFormat): boolean {
|
private cannotParseFile(jdoc: BitwardenPasswordProtectedFileFormat): boolean {
|
||||||
|
|||||||
Reference in New Issue
Block a user