Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Oct 10, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions apps/desktop/src/services/nativeMessageHandler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -218,4 +231,19 @@ export class NativeMessageHandlerService {
private sendResponse(response: EncryptedMessageResponse | UnencryptedMessageResponse) {
ipcRenderer.send("nativeMessagingReply", response);
}

private trimNullCharsFromMessage(message: string): string {
// Trim all null bytes padded at the end of messages. This happens with C encryption libraries.
differsthecat marked this conversation as resolved.
Show resolved Hide resolved
for (let i = message.length - 1; i >= 0; i--) {
// char code 0 is null
if (message.charCodeAt(i) === 0) {
differsthecat marked this conversation as resolved.
Show resolved Hide resolved
message = message.substring(0, message.length - 1);
}
// char code 125 is } and 93 is ] which are valid json ending characters, stop checking
else if (message.charCodeAt(i) === 125 || message.charCodeAt(i) === 93) {
break;
}
}
return message;
}
}