strict mode for message signing

This commit is contained in:
vyzo 2018-08-29 17:19:34 +03:00
parent 9fa8f64fd3
commit 3788f504a5
2 changed files with 11 additions and 2 deletions

View File

@ -905,7 +905,7 @@ func TestWithSigning(t *testing.T) {
defer cancel() defer cancel()
hosts := getNetHosts(t, ctx, 2) hosts := getNetHosts(t, ctx, 2)
psubs := getPubsubs(ctx, hosts, WithMessageSigning()) psubs := getPubsubs(ctx, hosts, WithMessageSigning(true))
connect(t, hosts[0], hosts[1]) connect(t, hosts[0], hosts[1])

View File

@ -92,6 +92,8 @@ type PubSub struct {
// key for signing messages; nil when signing is disabled (default for now) // key for signing messages; nil when signing is disabled (default for now)
signKey crypto.PrivKey signKey crypto.PrivKey
// strict mode rejects all unsigned messages prior to validation
signStrict bool
ctx context.Context ctx context.Context
} }
@ -190,9 +192,10 @@ func WithValidateThrottle(n int) Option {
} }
} }
func WithMessageSigning() Option { func WithMessageSigning(strict bool) Option {
return func(p *PubSub) error { return func(p *PubSub) error {
p.signKey = p.host.Peerstore().PrivKey(p.host.ID()) p.signKey = p.host.Peerstore().PrivKey(p.host.ID())
p.signStrict = strict
return nil return nil
} }
} }
@ -457,6 +460,12 @@ func msgID(pmsg *pb.Message) string {
// pushMsg pushes a message performing validation as necessary // pushMsg pushes a message performing validation as necessary
func (p *PubSub) pushMsg(vals []*topicVal, src peer.ID, msg *Message) { func (p *PubSub) pushMsg(vals []*topicVal, src peer.ID, msg *Message) {
// reject unsigned messages when strict before we even process the id
if p.signStrict && msg.Signature == nil {
log.Debugf("dropping unsigned message from %s", src)
return
}
id := msgID(msg.Message) id := msgID(msg.Message)
if p.seenMessage(id) { if p.seenMessage(id) {
return return