Skip to content

Commit

Permalink
add max inbout size validation spec
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Oct 5, 2022
1 parent ccc50d9 commit afa37c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,11 +912,19 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
}
})
} catch (err) {
this.log.error(err)
this.onPeerDisconnected(peerId)
this.handlePeerReadStreamError(err as Error, peerId)
}
}

/**
* Handle error when read stream pipe throws, less of the functional use but more
* to for testing purposes to spy on the error handling
* */
private handlePeerReadStreamError(err: Error, peerId: PeerId): void {
this.log.error(err)
this.onPeerDisconnected(peerId)
}

/**
* Handles an rpc request from a peer
*/
Expand Down
35 changes: 34 additions & 1 deletion test/gossip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe('gossip', () => {
init: {
scoreParams: {
IPColocationFactorThreshold: GossipsubDhi + 3
}
},
maxInboundDataLength: 4000000
}
})
})
Expand Down Expand Up @@ -78,6 +79,38 @@ describe('gossip', () => {
nodeASpy.pushGossip.restore()
})

it('should reject incoming messages bigger than maxInboundDataLength limit', async function () {
this.timeout(10e4)
const nodeA = nodes[0]
const nodeB = nodes[1]

const twoNodes = [nodeA, nodeB]
const topic = 'Z'
// add subscriptions to each node
twoNodes.forEach((n) => n.getPubSub().subscribe(topic))

// every node connected to every other
await connectAllPubSubNodes(twoNodes)

// wait for subscriptions to be transmitted
await Promise.all(twoNodes.map(async (n) => await pEvent(n.getPubSub(), 'subscription-change')))

// await mesh rebalancing
await Promise.all(twoNodes.map(async (n) => await pEvent(n.getPubSub(), 'gossipsub:heartbeat')))

// set spy. NOTE: Forcing private property to be public
const nodeBSpy = nodeB.getPubSub() as Partial<GossipSub> as SinonStubbedInstance<{
handlePeerReadStreamError: GossipSub['handlePeerReadStreamError']
}>
sinon.spy(nodeBSpy, 'handlePeerReadStreamError')

// This should lead to handlePeerReadStreamError at nodeB
await nodeA.getPubSub().publish(topic, new Uint8Array(5000000))
await pEvent(nodeA.getPubSub(), 'gossipsub:heartbeat')
const expectedError = nodeBSpy.handlePeerReadStreamError.getCalls()[0]?.args[0]
expect(expectedError !== undefined && (expectedError as unknown as { code: string }).code, 'ERR_MSG_DATA_TOO_LONG')
})

it('should send piggyback control into other sent messages', async function () {
this.timeout(10e4)
const nodeA = nodes[0]
Expand Down

0 comments on commit afa37c0

Please sign in to comment.