Skip to content

Commit

Permalink
fix: security issue in pub key registration (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
UrAvgDeveloper authored Feb 28, 2023
1 parent 3137243 commit 85b25f5
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
9 changes: 8 additions & 1 deletion packages/background/src/messaging/memorandum-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export const registerPubKey = async (
walletAddress: string,
channelId: string,
privacySetting: PrivacySetting,
chatReadReceiptSetting?: boolean
chatReadReceiptSetting?: boolean,
signingPubKey?: string,
signature?: string,
signedObjBase64?: string
): Promise<void> => {
try {
await client.mutate({
Expand All @@ -50,6 +53,9 @@ export const registerPubKey = async (
channelId,
privacySetting,
readReceipt: chatReadReceiptSetting,
signingPubKey,
signature,
signedObjBase64,
},
},
context: {
Expand All @@ -60,6 +66,7 @@ export const registerPubKey = async (
});
} catch (e) {
console.log(e);
throw new Error("Pub key registration failed");
}
};

Expand Down
41 changes: 40 additions & 1 deletion packages/background/src/messaging/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ export class MessagingService {
const privateKey = new PrivateKey(Buffer.from(sk));
const pubKey = toHex(privateKey.publicKey.compressed);

const encoder = new TextEncoder();

const encoded = encoder.encode(pubKey);
const signDoc = {
chain_id: "",
account_number: "0",
sequence: "0",
fee: {
gas: "0",
amount: [],
},
msgs: [
{
type: "sign/MsgSignData",
value: {
signer: address,
data: toBase64(encoded),
},
},
],
memo: "",
};

const regPubKey = await this.lookupPublicKey(accessToken, address);
if (
!regPubKey.privacySetting ||
Expand All @@ -77,14 +100,30 @@ export class MessagingService {
regPubKey.privacySetting !== privacySetting ||
regPubKey.chatReadReceiptSetting !== chatReadReceiptSetting
) {
const {
signature,
signed,
} = await this.keyRingService.requestSignAmino(
env,
"",
chainId,
address,
signDoc,
{ isADR36WithString: true }
);

await registerPubKey(
accessToken,
pubKey,
address,
MESSAGE_CHANNEL_ID,
privacySetting,
chatReadReceiptSetting
chatReadReceiptSetting,
signature.pub_key.value,
signature.signature,
Buffer.from(JSON.stringify(signed)).toString("base64")
);

this._publicKeyCache.set(address, {
publicKey: pubKey,
privacySetting,
Expand Down
16 changes: 7 additions & 9 deletions packages/extension/src/components/chat/chat-init-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import React, { useState } from "react";
import { useSelector } from "react-redux";
import { useHistory } from "react-router";
import { store } from "@chatStore/index";
import { setMessageError } from "@chatStore/messages-slice";
import { setMessagingPubKey, userDetails } from "@chatStore/user-slice";
import {
resetUser,
setMessagingPubKey,
userDetails,
} from "@chatStore/user-slice";
import privacyIcon from "@assets/hello.png";
import { useStore } from "../../stores";
import style from "./style.module.scss";
Expand Down Expand Up @@ -57,16 +60,11 @@ export const ChatInitPopup = ({
);

store.dispatch(setMessagingPubKey(messagingPubKey));
history.replace("/chat");
} catch (e) {
// Show error toaster
console.error("error", e);
store.dispatch(
setMessageError({
type: "setup",
message: "Something went wrong, Please try again in sometime.",
level: 3,
})
);
store.dispatch(resetUser({}));
// Redirect to home
history.replace("/");
} finally {
Expand Down

0 comments on commit 85b25f5

Please sign in to comment.