Commit 492bd3c 1 parent 062fa72 commit 492bd3c Copy full SHA for 492bd3c
File tree 3 files changed +43
-2
lines changed
3 files changed +43
-2
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package ics23
3
3
import (
4
4
"bytes"
5
5
"crypto"
6
+
6
7
// adds sha256 capability to crypto.SHA256
7
8
_ "crypto/sha256"
8
9
// adds sha512 capability to crypto.SHA512
@@ -69,10 +70,20 @@ func (op *InnerOp) Apply(child []byte) ([]byte, error) {
69
70
70
71
// CheckAgainstSpec will verify the InnerOp is in the format defined in spec
71
72
func (op * InnerOp ) CheckAgainstSpec (spec * ProofSpec ) error {
73
+ if op .Hash != spec .InnerSpec .Hash {
74
+ return errors .Errorf ("Unexpected HashOp: %d" , op .Hash )
75
+ }
76
+
72
77
leafPrefix := spec .LeafSpec .Prefix
73
78
if bytes .HasPrefix (op .Prefix , leafPrefix ) {
74
79
return errors .Errorf ("Inner Prefix starts with %X" , leafPrefix )
75
80
}
81
+ if len (op .Prefix ) < int (spec .InnerSpec .MinPrefixLength ) {
82
+ return errors .Errorf ("InnerOp prefix too short (%d)" , len (op .Prefix ))
83
+ }
84
+ if len (op .Prefix ) > int (spec .InnerSpec .MaxPrefixLength ) {
85
+ return errors .Errorf ("InnerOp prefix too long (%d)" , len (op .Prefix ))
86
+ }
76
87
return nil
77
88
}
78
89
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package ics23
2
2
3
3
import (
4
4
"bytes"
5
+
5
6
"github.com/pkg/errors"
6
7
)
7
8
@@ -18,6 +19,7 @@ var IavlSpec = &ProofSpec{
18
19
MinPrefixLength : 4 ,
19
20
MaxPrefixLength : 12 ,
20
21
ChildSize : 33 , // (with length byte)
22
+ Hash : HashOp_SHA256 ,
21
23
},
22
24
}
23
25
@@ -34,6 +36,7 @@ var TendermintSpec = &ProofSpec{
34
36
MinPrefixLength : 1 ,
35
37
MaxPrefixLength : 1 ,
36
38
ChildSize : 32 , // (no length byte)
39
+ Hash : HashOp_SHA256 ,
37
40
},
38
41
}
39
42
@@ -96,6 +99,13 @@ func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
96
99
if err != nil {
97
100
return errors .WithMessage (err , "leaf" )
98
101
}
102
+ if spec .MinDepth > 0 && len (p .Path ) < int (spec .MinDepth ) {
103
+ return errors .Errorf ("InnerOps depth too short: %d" , len (p .Path ))
104
+ }
105
+ if spec .MaxDepth > 0 && len (p .Path ) > int (spec .MaxDepth ) {
106
+ return errors .Errorf ("InnerOps depth too long: %d" , len (p .Path ))
107
+ }
108
+
99
109
for _ , inner := range p .Path {
100
110
if err := inner .CheckAgainstSpec (spec ); err != nil {
101
111
return errors .WithMessage (err , "inner" )
Original file line number Diff line number Diff line change @@ -237,12 +237,18 @@ func TestCheckLeaf(t *testing.T) {
237
237
238
238
func TestCheckAgainstSpec (t * testing.T ) {
239
239
validInner := & InnerOp {
240
- Prefix : fromHex ("aa" ),
240
+ Hash : HashOp_SHA256 ,
241
+ Prefix : fromHex ("aabbccdd" ),
241
242
}
242
243
invalidInner := & InnerOp {
243
- Prefix : fromHex ("00aa" ),
244
+ Hash : HashOp_SHA256 ,
245
+ Prefix : fromHex ("00aabbccdd" ),
244
246
Suffix : fromHex ("bb" ),
245
247
}
248
+ invalidInner2 := & InnerOp {
249
+ Hash : HashOp_SHA512 ,
250
+ Prefix : fromHex ("aabbccdd" ),
251
+ }
246
252
247
253
cases := map [string ]struct {
248
254
proof * ExistenceProof
@@ -315,6 +321,20 @@ func TestCheckAgainstSpec(t *testing.T) {
315
321
spec : IavlSpec ,
316
322
isErr : true ,
317
323
},
324
+ "rejects leaf with invalid inner proof (hash mismatch)" : {
325
+ proof : & ExistenceProof {
326
+ Key : []byte ("food" ),
327
+ Value : []byte ("bar" ),
328
+ Leaf : IavlSpec .LeafSpec ,
329
+ Path : []* InnerOp {
330
+ invalidInner2 ,
331
+ validInner ,
332
+ validInner ,
333
+ },
334
+ },
335
+ spec : IavlSpec ,
336
+ isErr : true ,
337
+ },
318
338
}
319
339
320
340
for name , tc := range cases {
You can’t perform that action at this time.
0 commit comments