Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"aegir": "^22.0.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-spies": "^1.0.0",
"dirty-chai": "^2.0.1",
"it-pair": "^1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

exports.codes = {
ERR_MISSING_SIGNATURE: 'ERR_MISSING_SIGNATURE',
ERR_INVALID_SIGNATURE: 'ERR_INVALID_SIGNATURE'
}
13 changes: 6 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const errcode = require('err-code')
const PeerId = require('peer-id')
const MulticodecTopology = require('libp2p-interfaces/src/topology/multicodec-topology')

const { codes } = require('./errors')
const message = require('./message')
const Peer = require('./peer')
const utils = require('./utils')
Expand Down Expand Up @@ -267,21 +268,19 @@ class PubsubBaseProtocol extends EventEmitter {

/**
* Validates the given message. The signature will be checked for authenticity.
* Throws an error on invalid messages
* @param {InMessage} message
* @returns {Promise<Boolean>}
* @returns {Promise<void>}
*/
async validate (message) { // eslint-disable-line require-await
// If strict signing is on and we have no signature, abort
if (this.strictSigning && !message.signature) {
this.log('Signing required and no signature was present, dropping message:', message)
return false
throw errcode(new Error('Signing required and no signature was present'), codes.ERR_MISSING_SIGNATURE)
}

// Check the message signature if present
if (message.signature) {
return verifySignature(message)
} else {
return true
if (message.signature && !verifySignature(message)) {
throw errcode(new Error('Invalid message signature'), codes.ERR_INVALID_SIGNATURE)
}
}

Expand Down
16 changes: 7 additions & 9 deletions test/pubsub.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-spies'))
chai.use(require('chai-as-promised'))
const expect = chai.expect
const sinon = require('sinon')

Expand Down Expand Up @@ -91,20 +92,20 @@ describe('pubsub base protocol', () => {

it('_buildMessage normalizes and signs messages', async () => {
const message = {
receivedFrom: peerId.id,
from: peerId.id,
data: 'hello',
seqno: randomSeqno(),
topicIDs: ['test-topic']
}

const signedMessage = await pubsub._buildMessage(message)
const verified = await pubsub.validate(signedMessage)

expect(verified).to.eql(true)
expect(pubsub.validate(signedMessage)).to.not.be.rejected()
})

it('validate with strict signing off will validate a present signature', async () => {
const message = {
receivedFrom: peerId.id,
from: peerId.id,
data: 'hello',
seqno: randomSeqno(),
Expand All @@ -114,22 +115,19 @@ describe('pubsub base protocol', () => {
sinon.stub(pubsub, 'strictSigning').value(false)

const signedMessage = await pubsub._buildMessage(message)
const verified = await pubsub.validate(signedMessage)

expect(verified).to.eql(true)
expect(pubsub.validate(signedMessage)).to.not.be.rejected()
})

it('validate with strict signing requires a signature', async () => {
const message = {
receivedFrom: peerId.id,
from: peerId.id,
data: 'hello',
seqno: randomSeqno(),
topicIDs: ['test-topic']
}

const verified = await pubsub.validate(message)

expect(verified).to.eql(false)
await expect(pubsub.validate(message)).to.be.rejectedWith(Error, 'Signing required and no signature was present')
})
})

Expand Down