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

feat: core replay protection #3621

Open
wants to merge 8 commits into
base: v2.0
Choose a base branch
from
9 changes: 8 additions & 1 deletion packages/core/src/controllers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export class Crypto implements ICrypto {
public encode: ICrypto["encode"] = async (topic, payload, opts) => {
this.isInitialized();
const params = validateEncoding(opts);
const message = safeJsonStringify(payload);
const protectedPayload = { topic, ...payload };
const message = safeJsonStringify(protectedPayload);
if (isTypeOneEnvelope(params)) {
const selfPublicKey = params.senderPublicKey;
const peerPublicKey = params.receiverPublicKey;
Expand All @@ -130,6 +131,12 @@ export class Crypto implements ICrypto {
const symKey = this.getSymKey(topic);
const message = decrypt({ symKey, encoded });
const payload = safeJsonParse(message);
if (typeof payload.topic !== "undefined" && payload.topic !== topic) {
ganchoradkov marked this conversation as resolved.
Show resolved Hide resolved
if (payload.topic !== topic) {
throw new Error("Mismatched topic decoded from message");
}
delete payload.topic;
}
return payload;
} catch (error) {
this.logger.error(
Expand Down
Loading