fix: harden Protobuf Deserialization and Refactor Utilities (#1820)

* refactor: reorganize browser utility functions and improve buffer handling

* fix: harden protobuf deserialization and clean up code

* refactor: simplify data handling in GroupCipher and SenderKey classes

* fix: Ensure consistent Buffer hydration and remove redundant code

* refactor: update decodeAndHydrate calls to use proto types for improved type safety
This commit is contained in:
João Lucas de Oliveira Lopes
2025-09-25 22:45:30 -03:00
committed by GitHub
parent 202c39f3fe
commit 3b46d43beb
23 changed files with 194 additions and 160 deletions
+3 -10
View File
@@ -19,7 +19,7 @@ export class GroupCipher {
this.senderKeyName = senderKeyName
}
public async encrypt(paddedPlaintext: Uint8Array | string): Promise<Uint8Array> {
public async encrypt(paddedPlaintext: Uint8Array): Promise<Uint8Array> {
const record = await this.senderKeyStore.loadSenderKey(this.senderKeyName)
if (!record) {
throw new Error('No SenderKeyRecord found for encryption')
@@ -107,16 +107,9 @@ export class GroupCipher {
}
}
private async getCipherText(
iv: Uint8Array | string,
key: Uint8Array | string,
plaintext: Uint8Array | string
): Promise<Buffer> {
private async getCipherText(iv: Uint8Array, key: Uint8Array, plaintext: Uint8Array): Promise<Buffer> {
try {
const ivBuffer = typeof iv === 'string' ? Buffer.from(iv, 'base64') : iv
const keyBuffer = typeof key === 'string' ? Buffer.from(key, 'base64') : key
const plaintextBuffer = typeof plaintext === 'string' ? Buffer.from(plaintext) : plaintext
return encrypt(keyBuffer, plaintextBuffer, ivBuffer)
return encrypt(key, plaintext, iv)
} catch (e) {
throw new Error('InvalidMessageException')
}