@@ -15,6 +15,9 @@ const (
1515 MinimumProofVersion = 0
1616 // LatestProofVersion is the latest supported proof version.
1717 LatestProofVersion = 1
18+
19+ // maxProofDepth is the maximum depth of a proof.
20+ maxProofDepth = 128
1821)
1922
2023const (
@@ -313,7 +316,7 @@ func (pv *ProofVerifier) verifyProofOpts(ctx context.Context, root hash.Hash, pr
313316 }
314317
315318 var res verifyResult
316- idx , rootPtr , err := pv .verifyProof (ctx , proof , 0 , opts , & res )
319+ idx , rootPtr , err := pv .verifyProof (ctx , proof , 0 , 0 , opts , & res )
317320 if err != nil {
318321 return nil , err
319322 }
@@ -341,13 +344,16 @@ func (pv *ProofVerifier) verifyProofOpts(ctx context.Context, root hash.Hash, pr
341344 return & res , nil
342345}
343346
344- func (pv * ProofVerifier ) verifyProof (ctx context.Context , proof * Proof , idx int , opts * verifyOpts , res * verifyResult ) (int , * node.Pointer , error ) {
347+ func (pv * ProofVerifier ) verifyProof (ctx context.Context , proof * Proof , idx int , depth int , opts * verifyOpts , res * verifyResult ) (int , * node.Pointer , error ) {
345348 if ctx .Err () != nil {
346349 return - 1 , nil , ctx .Err ()
347350 }
348351 if idx >= len (proof .Entries ) {
349352 return - 1 , nil , errors .New ("verifier: malformed proof" )
350353 }
354+ if depth > maxProofDepth {
355+ return - 1 , nil , errors .New ("verifier: max proof depth exceeded" )
356+ }
351357
352358 entry := proof .Entries [idx ]
353359 if entry == nil {
@@ -377,7 +383,7 @@ func (pv *ProofVerifier) verifyProof(ctx context.Context, proof *Proof, idx int,
377383 case 1 :
378384 // In version 1, the leaf node is added separately, as a child.
379385 // Leaf.
380- pos , nd .LeafNode , err = pv .verifyProof (ctx , proof , pos , opts , res )
386+ pos , nd .LeafNode , err = pv .verifyProof (ctx , proof , pos , depth + 1 , opts , res )
381387 if err != nil {
382388 return - 1 , nil , err
383389 }
@@ -387,12 +393,12 @@ func (pv *ProofVerifier) verifyProof(ctx context.Context, proof *Proof, idx int,
387393 }
388394
389395 // Left.
390- pos , nd .Left , err = pv .verifyProof (ctx , proof , pos , opts , res )
396+ pos , nd .Left , err = pv .verifyProof (ctx , proof , pos , depth + 1 , opts , res )
391397 if err != nil {
392398 return - 1 , nil , err
393399 }
394400 // Right.
395- pos , nd .Right , err = pv .verifyProof (ctx , proof , pos , opts , res )
401+ pos , nd .Right , err = pv .verifyProof (ctx , proof , pos , depth + 1 , opts , res )
396402 if err != nil {
397403 return - 1 , nil , err
398404 }
0 commit comments