Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions trie/zk_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import (

var magicHash []byte = []byte("THIS IS THE MAGIC INDEX FOR ZKTRIE")

// maxZkTrieDepth is the maximum depth limit for zkTrie recursion to prevent stack overflow and DoS attacks.
// The theoretical maximum depth of zkTrie is 256.
const maxZkTrieDepth = 256

// wrap zktrie for trie interface
type ZkTrie struct {
*zktrie.ZkTrie
Expand Down Expand Up @@ -254,6 +258,11 @@ func (t *ZkTrie) countLeaves(root *zkt.Hash, cb func(key, value []byte), depth i
return 0
}

// Check recursion depth limit to prevent stack overflow and DoS attacks
if depth > maxZkTrieDepth {
panic(fmt.Sprintf("countLeaves: exceeded max depth limit, depth=%d, maxDepth=%d", depth, maxZkTrieDepth))
}

rootNode, err := t.ZkTrie.Tree().GetNode(root)
if err != nil {
panic("countLeaves cannot get rootNode")
Expand Down