1
0
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:
John Harrington
2025-09-16 10:22:05 -07:00
committed by GitHub
parent f94e798920
commit 001f8fa579
2 changed files with 16 additions and 6 deletions

View File

@@ -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");
});
});
});

View File

@@ -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 {