|
| 1 | +/* eslint-env mocha */ |
| 2 | +import { generateKeyPair, publicKeyToProtobuf } from '@libp2p/crypto/keys' |
| 3 | +import { start, stop } from '@libp2p/interface' |
| 4 | +import { defaultLogger } from '@libp2p/logger' |
| 5 | +import { peerIdFromPrivateKey } from '@libp2p/peer-id' |
| 6 | +import { expect } from 'aegir/chai' |
| 7 | +import { stubInterface } from 'sinon-ts' |
| 8 | +import { concat as uint8ArrayConcat } from 'uint8arrays/concat' |
| 9 | +import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string' |
| 10 | +import { floodsub } from '../src/index.ts' |
| 11 | +import { RPC } from '../src/message/rpc.ts' |
| 12 | +import { PeerStreams } from '../src/peer-streams.ts' |
| 13 | +import { SignPrefix } from '../src/sign.ts' |
| 14 | +import { randomSeqno, toRpcMessage } from '../src/utils.ts' |
| 15 | +import type { PubSubRPCMessage } from '../src/floodsub.ts' |
| 16 | +import type { FloodSub } from '../src/index.ts' |
| 17 | +import type { PeerId, PrivateKey, PublicKey } from '@libp2p/interface' |
| 18 | +import type { Registrar } from '@libp2p/interface-internal' |
| 19 | + |
| 20 | +const topic = 'test-topic' |
| 21 | +const data = uint8ArrayFromString('hello') |
| 22 | + |
| 23 | +// a wire message claiming to be from `from`, signed by `signer` and carrying |
| 24 | +// `key` in its key field |
| 25 | +async function wireMessage (from: PeerId, signer: PrivateKey, key: PublicKey): Promise<PubSubRPCMessage> { |
| 26 | + const unsigned = toRpcMessage({ |
| 27 | + type: 'signed', |
| 28 | + from, |
| 29 | + data, |
| 30 | + sequenceNumber: randomSeqno(), |
| 31 | + topic, |
| 32 | + signature: new Uint8Array(0), |
| 33 | + key |
| 34 | + }) |
| 35 | + delete unsigned.signature |
| 36 | + delete unsigned.key |
| 37 | + |
| 38 | + const signature = await signer.sign(uint8ArrayConcat([SignPrefix, RPC.Message.encode(unsigned)])) |
| 39 | + |
| 40 | + return { ...unsigned, signature, key: publicKeyToProtobuf(key) } |
| 41 | +} |
| 42 | + |
| 43 | +describe('forged messages over the wire', () => { |
| 44 | + let pubsub: FloodSub |
| 45 | + let sender: PeerStreams |
| 46 | + let attackerKey: PrivateKey |
| 47 | + let delivered: number |
| 48 | + |
| 49 | + beforeEach(async () => { |
| 50 | + const privateKey = await generateKeyPair('Ed25519') |
| 51 | + |
| 52 | + pubsub = floodsub()({ |
| 53 | + peerId: peerIdFromPrivateKey(privateKey), |
| 54 | + privateKey, |
| 55 | + registrar: stubInterface<Registrar>(), |
| 56 | + logger: defaultLogger() |
| 57 | + }) |
| 58 | + await start(pubsub) |
| 59 | + pubsub.subscribe(topic) |
| 60 | + |
| 61 | + attackerKey = await generateKeyPair('Ed25519') |
| 62 | + sender = new PeerStreams(peerIdFromPrivateKey(attackerKey)) |
| 63 | + delivered = 0 |
| 64 | + pubsub.addEventListener('message', () => { delivered++ }) |
| 65 | + }) |
| 66 | + |
| 67 | + afterEach(async () => { |
| 68 | + await stop(pubsub) |
| 69 | + }) |
| 70 | + |
| 71 | + async function receive (message: PubSubRPCMessage): Promise<void> { |
| 72 | + // @ts-expect-error private method |
| 73 | + await pubsub['processRpc'](sender, { subscriptions: [], messages: [message] }) |
| 74 | + // @ts-expect-error private field |
| 75 | + await pubsub.queue.onIdle() |
| 76 | + } |
| 77 | + |
| 78 | + it('delivers a message signed by its author', async () => { |
| 79 | + await receive(await wireMessage(sender.peerId, attackerKey, attackerKey.publicKey)) |
| 80 | + |
| 81 | + expect(delivered).to.equal(1) |
| 82 | + }) |
| 83 | + |
| 84 | + it('delivers a message relayed from a different Ed25519 author', async () => { |
| 85 | + const authorKey = await generateKeyPair('Ed25519') |
| 86 | + |
| 87 | + await receive(await wireMessage(peerIdFromPrivateKey(authorKey), authorKey, authorKey.publicKey)) |
| 88 | + |
| 89 | + expect(delivered).to.equal(1) |
| 90 | + }) |
| 91 | + |
| 92 | + it('delivers a message from an RSA author carrying its key', async () => { |
| 93 | + const authorKey = await generateKeyPair('RSA', 2048) |
| 94 | + |
| 95 | + await receive(await wireMessage(peerIdFromPrivateKey(authorKey), authorKey, authorKey.publicKey)) |
| 96 | + |
| 97 | + expect(delivered).to.equal(1) |
| 98 | + }) |
| 99 | + |
| 100 | + it('drops a message signed by the attacker claiming an Ed25519 author', async () => { |
| 101 | + const victim = peerIdFromPrivateKey(await generateKeyPair('Ed25519')) |
| 102 | + |
| 103 | + await receive(await wireMessage(victim, attackerKey, attackerKey.publicKey)) |
| 104 | + |
| 105 | + expect(delivered).to.equal(0) |
| 106 | + }) |
| 107 | + |
| 108 | + // dropped as unsigned by `isSigned` before signature verification, since the |
| 109 | + // key does not derive to the author |
| 110 | + it('drops a message signed by the attacker claiming an RSA author', async () => { |
| 111 | + const victim = peerIdFromPrivateKey(await generateKeyPair('RSA', 2048)) |
| 112 | + |
| 113 | + await receive(await wireMessage(victim, attackerKey, attackerKey.publicKey)) |
| 114 | + |
| 115 | + expect(delivered).to.equal(0) |
| 116 | + }) |
| 117 | + |
| 118 | + it('drops a message signed by its Ed25519 author that carries a different key', async () => { |
| 119 | + const authorKey = await generateKeyPair('Ed25519') |
| 120 | + |
| 121 | + await receive(await wireMessage(peerIdFromPrivateKey(authorKey), authorKey, attackerKey.publicKey)) |
| 122 | + |
| 123 | + expect(delivered).to.equal(0) |
| 124 | + }) |
| 125 | +}) |
0 commit comments