1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-04 05:08:06 +02:00

[SG-720] Trim c null characters getting padded at end of messages (#3724)

* Trim everything at the end of decrypted payload before parsing

* Clarify comment

* Use char code check for nulls

* Extract trim code to function

* make char codes constants
This commit is contained in:
Robyn MacCallum 2022-10-10 13:07:12 -04:00 committed by GitHub
parent f6b2b75ad8
commit fe1a895e6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -182,12 +182,25 @@ export class NativeMessageHandlerService {
this.ddgSharedSecret = SymmetricCryptoKey.fromJSON({ keyB64: storedKey });
}
return JSON.parse(
await this.cryptoService.decryptToUtf8(
try {
let decryptedResult = await this.cryptoService.decryptToUtf8(
message.encryptedCommand as EncString,
this.ddgSharedSecret
)
);
);
decryptedResult = this.trimNullCharsFromMessage(decryptedResult);
return JSON.parse(decryptedResult);
} catch {
this.sendResponse({
messageId: message.messageId,
version: NativeMessagingVersion.Latest,
payload: {
error: "cannot-decrypt",
},
});
return;
}
}
private async sendEncryptedResponse(
@ -218,4 +231,23 @@ export class NativeMessageHandlerService {
private sendResponse(response: EncryptedMessageResponse | UnencryptedMessageResponse) {
ipcRenderer.send("nativeMessagingReply", response);
}
// Trim all null bytes padded at the end of messages. This happens with C encryption libraries.
private trimNullCharsFromMessage(message: string): string {
const charNull = 0;
const charRightCurlyBrace = 125;
const charRightBracket = 93;
for (let i = message.length - 1; i >= 0; i--) {
if (message.charCodeAt(i) === charNull) {
message = message.substring(0, message.length - 1);
} else if (
message.charCodeAt(i) === charRightCurlyBrace ||
message.charCodeAt(i) === charRightBracket
) {
break;
}
}
return message;
}
}