feat(relay): validate waku message at gossip layer

This commit is contained in:
fryorcraken.eth 2023-03-10 12:43:29 +11:00
parent 9debf5aea7
commit 96847374d6
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 101 additions and 0 deletions

View File

@ -23,6 +23,7 @@ import { TopicOnlyDecoder } from "../message/topic_only_message.js";
import { pushOrInitMapSet } from "../push_or_init_map.js";
import * as constants from "./constants.js";
import { messageValidator } from "./message_validator.js";
const log = debug("waku:relay");
@ -170,9 +171,15 @@ class Relay extends GossipSub implements IRelay {
}
);
this.topicValidators.set(pubSubTopic, messageValidator);
super.subscribe(pubSubTopic);
}
unsubscribe(pubSubTopic: TopicStr): void {
super.unsubscribe(pubSubTopic);
this.topicValidators.delete(pubSubTopic);
}
getMeshPeers(topic?: TopicStr): PeerIdStr[] {
return super.getMeshPeers(topic ?? this.pubSubTopic);
}

View File

@ -0,0 +1,59 @@
import { TopicValidatorResult } from "@libp2p/interface-pubsub";
import type { UnsignedMessage } from "@libp2p/interface-pubsub";
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
import { expect } from "chai";
import fc from "fast-check";
import { createEncoder } from "../message/version_0.js";
import { messageValidator } from "./message_validator.js";
describe("Message Validator", () => {
it("Accepts a valid Waku Message", async () => {
await fc.assert(
fc.asyncProperty(
fc.uint8Array({ minLength: 1 }),
fc.string({ minLength: 1 }),
fc.string({ minLength: 1 }),
async (payload, pubSubTopic, contentTopic) => {
const peerId = await createSecp256k1PeerId();
const encoder = createEncoder({ contentTopic });
const bytes = await encoder.toWire({ payload });
const message: UnsignedMessage = {
type: "unsigned",
topic: pubSubTopic,
data: bytes,
};
const result = messageValidator(peerId, message);
expect(result).to.eq(TopicValidatorResult.Accept);
}
)
);
});
it("Rejects garbage", async () => {
await fc.assert(
fc.asyncProperty(
fc.uint8Array(),
fc.string(),
async (data, pubSubTopic) => {
const peerId = await createSecp256k1PeerId();
const message: UnsignedMessage = {
type: "unsigned",
topic: pubSubTopic,
data,
};
const result = messageValidator(peerId, message);
expect(result).to.eq(TopicValidatorResult.Reject);
}
)
);
});
});

View File

@ -0,0 +1,35 @@
import type { PeerId } from "@libp2p/interface-peer-id";
import type { Message } from "@libp2p/interface-pubsub";
import { TopicValidatorResult } from "@libp2p/interface-pubsub";
import { proto_message as proto } from "@waku/proto";
import debug from "debug";
const log = debug("waku:relay");
export function messageValidator(
peer: PeerId,
message: Message
): TopicValidatorResult {
const startTime = performance.now();
log(`validating message from ${peer} received on ${message.topic}`);
let result = TopicValidatorResult.Accept;
try {
const protoMessage = proto.WakuMessage.decode(message.data);
if (
!protoMessage.contentTopic ||
!protoMessage.contentTopic.length ||
!protoMessage.payload ||
!protoMessage.payload.length
) {
result = TopicValidatorResult.Reject;
}
} catch (e) {
result = TopicValidatorResult.Reject;
}
const endTime = performance.now();
log(`Validation time (must be <100ms): ${endTime - startTime}ms`);
return result;
}