@@ -14,15 +14,13 @@ import {
14
14
import type { PeerId } from '@libp2p/interface-peer-id'
15
15
import type { IncomingStreamData } from '@libp2p/interface-registrar'
16
16
import type { Connection } from '@libp2p/interface-connection'
17
- import type { PubSub , Message , StrictNoSign , StrictSign , PubSubInit , PubSubEvents , PeerStreams , PubSubRPCMessage , PubSubRPC , PubSubRPCSubscription , SubscriptionChangeData , PublishResult } from '@libp2p/interface-pubsub'
17
+ import { PubSub , Message , StrictNoSign , StrictSign , PubSubInit , PubSubEvents , PeerStreams , PubSubRPCMessage , PubSubRPC , PubSubRPCSubscription , SubscriptionChangeData , PublishResult , TopicValidatorFn , TopicValidatorResult } from '@libp2p/interface-pubsub'
18
18
import { PeerMap , PeerSet } from '@libp2p/peer-collections'
19
19
import { Components , Initializable } from '@libp2p/components'
20
20
import type { Uint8ArrayList } from 'uint8arraylist'
21
21
22
22
const log = logger ( 'libp2p:pubsub' )
23
23
24
- export interface TopicValidator { ( topic : string , message : Message ) : Promise < void > }
25
-
26
24
/**
27
25
* PubSubBaseProtocol handles the peers and connections logic for pubsub routers
28
26
* and specifies the API that pubsub routers should have.
@@ -59,7 +57,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
59
57
* Keyed by topic
60
58
* Topic validators are functions with the following input:
61
59
*/
62
- public topicValidators : Map < string , TopicValidator >
60
+ public topicValidators : Map < string , TopicValidatorFn >
63
61
public queue : Queue
64
62
public multicodecs : string [ ]
65
63
public components : Components = new Components ( )
@@ -420,7 +418,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
420
418
421
419
// Ensure the message is valid before processing it
422
420
try {
423
- await this . validate ( msg )
421
+ await this . validate ( from , msg )
424
422
} catch ( err : any ) {
425
423
log ( 'Message is invalid, dropping it. %O' , err )
426
424
return
@@ -524,7 +522,7 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
524
522
* Validates the given message. The signature will be checked for authenticity.
525
523
* Throws an error on invalid messages
526
524
*/
527
- async validate ( message : Message ) { // eslint-disable-line require-await
525
+ async validate ( from : PeerId , message : Message ) { // eslint-disable-line require-await
528
526
const signaturePolicy = this . globalSignaturePolicy
529
527
switch ( signaturePolicy ) {
530
528
case 'StrictNoSign' :
@@ -570,9 +568,11 @@ export abstract class PubSubBaseProtocol<Events extends { [s: string]: any } = P
570
568
}
571
569
572
570
const validatorFn = this . topicValidators . get ( message . topic )
573
-
574
571
if ( validatorFn != null ) {
575
- await validatorFn ( message . topic , message )
572
+ const result = await validatorFn ( from , message )
573
+ if ( result === TopicValidatorResult . Reject || result === TopicValidatorResult . Ignore ) {
574
+ throw errcode ( new Error ( 'Message validation failed' ) , codes . ERR_TOPIC_VALIDATOR_REJECT )
575
+ }
576
576
}
577
577
}
578
578
0 commit comments