Skip to content

Commit 0c63b29

Browse files
authored
Merge pull request #1227 from waku-org/feat/gossipsub-waku-message-validation
2 parents 9debf5a + 9684737 commit 0c63b29

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

packages/core/src/lib/relay/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { TopicOnlyDecoder } from "../message/topic_only_message.js";
2323
import { pushOrInitMapSet } from "../push_or_init_map.js";
2424

2525
import * as constants from "./constants.js";
26+
import { messageValidator } from "./message_validator.js";
2627

2728
const log = debug("waku:relay");
2829

@@ -170,9 +171,15 @@ class Relay extends GossipSub implements IRelay {
170171
}
171172
);
172173

174+
this.topicValidators.set(pubSubTopic, messageValidator);
173175
super.subscribe(pubSubTopic);
174176
}
175177

178+
unsubscribe(pubSubTopic: TopicStr): void {
179+
super.unsubscribe(pubSubTopic);
180+
this.topicValidators.delete(pubSubTopic);
181+
}
182+
176183
getMeshPeers(topic?: TopicStr): PeerIdStr[] {
177184
return super.getMeshPeers(topic ?? this.pubSubTopic);
178185
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { TopicValidatorResult } from "@libp2p/interface-pubsub";
2+
import type { UnsignedMessage } from "@libp2p/interface-pubsub";
3+
import { createSecp256k1PeerId } from "@libp2p/peer-id-factory";
4+
import { expect } from "chai";
5+
import fc from "fast-check";
6+
7+
import { createEncoder } from "../message/version_0.js";
8+
9+
import { messageValidator } from "./message_validator.js";
10+
11+
describe("Message Validator", () => {
12+
it("Accepts a valid Waku Message", async () => {
13+
await fc.assert(
14+
fc.asyncProperty(
15+
fc.uint8Array({ minLength: 1 }),
16+
fc.string({ minLength: 1 }),
17+
fc.string({ minLength: 1 }),
18+
async (payload, pubSubTopic, contentTopic) => {
19+
const peerId = await createSecp256k1PeerId();
20+
21+
const encoder = createEncoder({ contentTopic });
22+
const bytes = await encoder.toWire({ payload });
23+
24+
const message: UnsignedMessage = {
25+
type: "unsigned",
26+
topic: pubSubTopic,
27+
data: bytes,
28+
};
29+
30+
const result = messageValidator(peerId, message);
31+
32+
expect(result).to.eq(TopicValidatorResult.Accept);
33+
}
34+
)
35+
);
36+
});
37+
38+
it("Rejects garbage", async () => {
39+
await fc.assert(
40+
fc.asyncProperty(
41+
fc.uint8Array(),
42+
fc.string(),
43+
async (data, pubSubTopic) => {
44+
const peerId = await createSecp256k1PeerId();
45+
46+
const message: UnsignedMessage = {
47+
type: "unsigned",
48+
topic: pubSubTopic,
49+
data,
50+
};
51+
52+
const result = messageValidator(peerId, message);
53+
54+
expect(result).to.eq(TopicValidatorResult.Reject);
55+
}
56+
)
57+
);
58+
});
59+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { PeerId } from "@libp2p/interface-peer-id";
2+
import type { Message } from "@libp2p/interface-pubsub";
3+
import { TopicValidatorResult } from "@libp2p/interface-pubsub";
4+
import { proto_message as proto } from "@waku/proto";
5+
import debug from "debug";
6+
7+
const log = debug("waku:relay");
8+
9+
export function messageValidator(
10+
peer: PeerId,
11+
message: Message
12+
): TopicValidatorResult {
13+
const startTime = performance.now();
14+
log(`validating message from ${peer} received on ${message.topic}`);
15+
let result = TopicValidatorResult.Accept;
16+
17+
try {
18+
const protoMessage = proto.WakuMessage.decode(message.data);
19+
20+
if (
21+
!protoMessage.contentTopic ||
22+
!protoMessage.contentTopic.length ||
23+
!protoMessage.payload ||
24+
!protoMessage.payload.length
25+
) {
26+
result = TopicValidatorResult.Reject;
27+
}
28+
} catch (e) {
29+
result = TopicValidatorResult.Reject;
30+
}
31+
32+
const endTime = performance.now();
33+
log(`Validation time (must be <100ms): ${endTime - startTime}ms`);
34+
return result;
35+
}

0 commit comments

Comments
 (0)