1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 00:33:44 +00:00

[PS-11868] Require key for enc string decryption (#10981)

* Specify enc string decryption key and service.

* Fix issue with identifying `this` type within extended classes

* Folder decryption example

* Test enc string changes

* Fix test name

* test decrypt with key
This commit is contained in:
Matt Gibson
2024-09-30 06:34:03 -07:00
committed by GitHub
parent cc9a72616a
commit a6b9088940
7 changed files with 358 additions and 4 deletions

View File

@@ -1,11 +1,14 @@
import { Jsonify, Opaque } from "type-fest";
import { EncryptService } from "../../abstractions/encrypt.service";
import { EncryptionType, EXPECTED_NUM_PARTS_BY_ENCRYPTION_TYPE } from "../../enums";
import { Encrypted } from "../../interfaces/encrypted";
import { Utils } from "../../misc/utils";
import { SymmetricCryptoKey } from "./symmetric-crypto-key";
export const DECRYPT_ERROR = "[error: cannot decrypt]";
export class EncString implements Encrypted {
encryptedString?: EncryptedString;
encryptionType?: EncryptionType;
@@ -167,11 +170,24 @@ export class EncString implements Encrypted {
const encryptService = Utils.getContainerService().getEncryptService();
this.decryptedValue = await encryptService.decryptToUtf8(this, key);
} catch (e) {
this.decryptedValue = "[error: cannot decrypt]";
this.decryptedValue = DECRYPT_ERROR;
}
return this.decryptedValue;
}
async decryptWithKey(key: SymmetricCryptoKey, encryptService: EncryptService) {
try {
if (key == null) {
throw new Error("No key to decrypt EncString");
}
this.decryptedValue = await encryptService.decryptToUtf8(this, key);
} catch (e) {
this.decryptedValue = DECRYPT_ERROR;
}
return this.decryptedValue;
}
private async getKeyForDecryption(orgId: string) {
const cryptoService = Utils.getContainerService().getCryptoService();
return orgId != null