hypercore 11.40.0 → 11.40.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/merkle-tree.js +17 -1
- package/lib/replicator.js +1 -1
- package/package.json +1 -1
package/lib/merkle-tree.js
CHANGED
|
@@ -12,6 +12,11 @@ const {
|
|
|
12
12
|
ASSERTION
|
|
13
13
|
} = require('hypercore-errors')
|
|
14
14
|
|
|
15
|
+
// the length can be 2 ** 64 in practice, but js numbers limit it to 2 ** 52
|
|
16
|
+
const MAX_LENGTH = 2 ** 52
|
|
17
|
+
// a proof needs at most two nodes per level, for the 64 levels a tree can have
|
|
18
|
+
const MAX_NODES = 2 * 64
|
|
19
|
+
|
|
15
20
|
class NodeQueue {
|
|
16
21
|
constructor(nodes, root = null) {
|
|
17
22
|
this.i = 0
|
|
@@ -575,7 +580,7 @@ class TreeProof {
|
|
|
575
580
|
|
|
576
581
|
if (head > to) {
|
|
577
582
|
pending.additionalUpgrade = []
|
|
578
|
-
upgradeProof(this, node, seek, to, head, subTree, pending.additionalUpgrade)
|
|
583
|
+
upgradeProof(this, node, seek, to, head, seek ? head : subTree, pending.additionalUpgrade)
|
|
579
584
|
}
|
|
580
585
|
}
|
|
581
586
|
|
|
@@ -643,6 +648,8 @@ class RefitTreeProof extends TreeProof {
|
|
|
643
648
|
|
|
644
649
|
this.insufficient = false
|
|
645
650
|
|
|
651
|
+
if (hasTooManyNodes(proof)) throw INVALID_OPERATION('Too many nodes')
|
|
652
|
+
|
|
646
653
|
const nodes = []
|
|
647
654
|
verifyTree(proof, nodes)
|
|
648
655
|
nodes.push(...proof.upgrade.nodes, ...proof.upgrade.additionalNodes)
|
|
@@ -656,6 +663,8 @@ class RefitTreeProof extends TreeProof {
|
|
|
656
663
|
length = Math.max(length, flat.rightSpan(node.index) / 2 + 1)
|
|
657
664
|
}
|
|
658
665
|
|
|
666
|
+
if (length > MAX_LENGTH) throw INVALID_OPERATION('Upgrade is out of bounds')
|
|
667
|
+
|
|
659
668
|
if (length > this.length) {
|
|
660
669
|
this.upgrade = { start: this.length, length: length - this.length }
|
|
661
670
|
this.length = length
|
|
@@ -1299,6 +1308,13 @@ function upgradeProof(proof, node, seek, from, to, subTree, nodes) {
|
|
|
1299
1308
|
}
|
|
1300
1309
|
}
|
|
1301
1310
|
|
|
1311
|
+
function hasTooManyNodes(proof) {
|
|
1312
|
+
if (proof.block && proof.block.nodes.length > MAX_NODES) return true
|
|
1313
|
+
if (proof.hash && proof.hash.nodes.length > MAX_NODES) return true
|
|
1314
|
+
if (!proof.upgrade) return false
|
|
1315
|
+
return proof.upgrade.nodes.length > MAX_NODES || proof.upgrade.additionalNodes.length > MAX_NODES
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1302
1318
|
function nodesToRoot(index, nodes, head) {
|
|
1303
1319
|
const ite = flat.iterator(index)
|
|
1304
1320
|
|
package/lib/replicator.js
CHANGED