Skip to content

Commit 1e2c93a

Browse files
committed
rlp: reject non-minimal input strings
Input strings of length 1 containing a byte < 56 are non-minimal and should be encoded as a single byte instead. Reject such strings.
1 parent 6e9f803 commit 1e2c93a

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

‎rlp/decode.go‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,11 @@ func decodeByteSlice(s *Stream, val reflect.Value) error {
305305
return decodeListSlice(s, val, decodeUint)
306306
}
307307
b, err := s.Bytes()
308-
if err == nil {
309-
val.SetBytes(b)
308+
if err != nil {
309+
return wrapStreamError(err, val.Type())
310310
}
311-
return err
311+
val.SetBytes(b)
312+
return nil
312313
}
313314

314315
func decodeByteArray(s *Stream, val reflect.Value) error {
@@ -333,6 +334,10 @@ func decodeByteArray(s *Stream, val reflect.Value) error {
333334
return err
334335
}
335336
zero(val, int(size))
337+
// Reject cases where single byte encoding should have been used.
338+
if size == 1 && slice[0] < 56 {
339+
return wrapStreamError(ErrCanonSize, val.Type())
340+
}
336341
case List:
337342
return decodeListArray(s, val, decodeUint)
338343
}
@@ -477,7 +482,7 @@ var (
477482
// Actual Errors
478483
ErrExpectedString = errors.New("rlp: expected String or Byte")
479484
ErrExpectedList = errors.New("rlp: expected List")
480-
ErrCanonInt = errors.New("rlp: non-canonical (leading zero bytes) integer")
485+
ErrCanonInt = errors.New("rlp: non-canonical integer format")
481486
ErrCanonSize = errors.New("rlp: non-canonical size information")
482487
ErrElemTooLarge = errors.New("rlp: element is larger than containing list")
483488
ErrValueTooLarge = errors.New("rlp: value size exceeds available input length")
@@ -576,6 +581,9 @@ func (s *Stream) Bytes() ([]byte, error) {
576581
if err = s.readFull(b); err != nil {
577582
return nil, err
578583
}
584+
if size == 1 && b[0] < 56 {
585+
return nil, ErrCanonSize
586+
}
579587
return b, nil
580588
default:
581589
return nil, ErrExpectedString
@@ -631,11 +639,17 @@ func (s *Stream) uint(maxbits int) (uint64, error) {
631639
return 0, errUintOverflow
632640
}
633641
v, err := s.readUint(byte(size))
634-
if err == ErrCanonSize {
642+
switch {
643+
case err == ErrCanonSize:
635644
// Adjust error because we're not reading a size right now.
636-
err = ErrCanonInt
645+
return 0, ErrCanonInt
646+
case err != nil:
647+
return 0, err
648+
case size > 0 && v < 56:
649+
return 0, ErrCanonSize
650+
default:
651+
return v, nil
637652
}
638-
return v, err
639653
default:
640654
return 0, ErrExpectedString
641655
}

‎rlp/decode_test.go‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,16 @@ func TestStreamErrors(t *testing.T) {
9393
{"00", calls{"ListEnd"}, nil, errNotInList},
9494
{"C401020304", calls{"List", "Uint", "ListEnd"}, nil, errNotAtEOL},
9595

96-
// Leading zero bytes are rejected when reading integers.
96+
// Non-canonical integers (e.g. leading zero bytes).
9797
{"00", calls{"Uint"}, nil, ErrCanonInt},
9898
{"820002", calls{"Uint"}, nil, ErrCanonInt},
99+
{"8133", calls{"Uint"}, nil, ErrCanonSize},
100+
{"8156", calls{"Uint"}, nil, nil},
99101

100102
// Size tags must use the smallest possible encoding.
101103
// Leading zero bytes in the size tag are also rejected.
104+
{"8100", calls{"Uint"}, nil, ErrCanonSize},
105+
{"8100", calls{"Bytes"}, nil, ErrCanonSize},
102106
{"B800", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
103107
{"B90000", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
104108
{"B90055", calls{"Kind"}, withoutInputLimit, ErrCanonSize},
@@ -112,7 +116,7 @@ func TestStreamErrors(t *testing.T) {
112116
{"", calls{"Kind"}, nil, io.EOF},
113117
{"", calls{"Uint"}, nil, io.EOF},
114118
{"", calls{"List"}, nil, io.EOF},
115-
{"8105", calls{"Uint", "Uint"}, nil, io.EOF},
119+
{"8158", calls{"Uint", "Uint"}, nil, io.EOF},
116120
{"C0", calls{"List", "ListEnd", "List"}, nil, io.EOF},
117121

118122
// Input limit errors.
@@ -129,11 +133,11 @@ func TestStreamErrors(t *testing.T) {
129133
// down toward zero in Stream.remaining, reading too far can overflow
130134
// remaining to a large value, effectively disabling the limit.
131135
{"C40102030401", calls{"Raw", "Uint"}, withCustomInputLimit(5), io.EOF},
132-
{"C4010203048102", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge},
136+
{"C4010203048158", calls{"Raw", "Uint"}, withCustomInputLimit(6), ErrValueTooLarge},
133137

134138
// Check that the same calls are fine without a limit.
135139
{"C40102030401", calls{"Raw", "Uint"}, withoutInputLimit, nil},
136-
{"C4010203048102", calls{"Raw", "Uint"}, withoutInputLimit, nil},
140+
{"C4010203048158", calls{"Raw", "Uint"}, withoutInputLimit, nil},
137141

138142
// Unexpected EOF. This only happens when there is
139143
// no input limit, so the reader needs to be 'dumbed down'.
@@ -295,13 +299,13 @@ var decodeTests = []decodeTest{
295299
// integers
296300
{input: "05", ptr: new(uint32), value: uint32(5)},
297301
{input: "80", ptr: new(uint32), value: uint32(0)},
298-
{input: "8105", ptr: new(uint32), value: uint32(5)},
299302
{input: "820505", ptr: new(uint32), value: uint32(0x0505)},
300303
{input: "83050505", ptr: new(uint32), value: uint32(0x050505)},
301304
{input: "8405050505", ptr: new(uint32), value: uint32(0x05050505)},
302305
{input: "850505050505", ptr: new(uint32), error: "rlp: input string too long for uint32"},
303306
{input: "C0", ptr: new(uint32), error: "rlp: expected input string or byte for uint32"},
304307
{input: "00", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
308+
{input: "8105", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
305309
{input: "820004", ptr: new(uint32), error: "rlp: non-canonical integer (leading zero bytes) for uint32"},
306310
{input: "B8020004", ptr: new(uint32), error: "rlp: non-canonical size information for uint32"},
307311

@@ -319,10 +323,16 @@ var decodeTests = []decodeTest{
319323
// byte slices
320324
{input: "01", ptr: new([]byte), value: []byte{1}},
321325
{input: "80", ptr: new([]byte), value: []byte{}},
326+
322327
{input: "8D6162636465666768696A6B6C6D", ptr: new([]byte), value: []byte("abcdefghijklm")},
323328
{input: "C0", ptr: new([]byte), value: []byte{}},
324329
{input: "C3010203", ptr: new([]byte), value: []byte{1, 2, 3}},
325330

331+
{
332+
input: "8105",
333+
ptr: new([]byte),
334+
error: "rlp: non-canonical size information for []uint8",
335+
},
326336
{
327337
input: "C3820102",
328338
ptr: new([]byte),
@@ -346,10 +356,15 @@ var decodeTests = []decodeTest{
346356
ptr: new([5]byte),
347357
error: "rlp: input string too long for [5]uint8",
348358
},
359+
{
360+
input: "8105",
361+
ptr: new([5]byte),
362+
error: "rlp: non-canonical size information for [5]uint8",
363+
},
349364

350365
// byte array reuse (should be zeroed)
351366
{input: "850102030405", ptr: &sharedByteArray, value: [5]byte{1, 2, 3, 4, 5}},
352-
{input: "8101", ptr: &sharedByteArray, value: [5]byte{1}}, // kind: String
367+
{input: "01", ptr: &sharedByteArray, value: [5]byte{1}}, // kind: String
353368
{input: "850102030405", ptr: &sharedByteArray, value: [5]byte{1, 2, 3, 4, 5}},
354369
{input: "01", ptr: &sharedByteArray, value: [5]byte{1}}, // kind: Byte
355370
{input: "C3010203", ptr: &sharedByteArray, value: [5]byte{1, 2, 3, 0, 0}},
@@ -369,9 +384,10 @@ var decodeTests = []decodeTest{
369384
// big ints
370385
{input: "01", ptr: new(*big.Int), value: big.NewInt(1)},
371386
{input: "89FFFFFFFFFFFFFFFFFF", ptr: new(*big.Int), value: veryBigInt},
372-
{input: "820001", ptr: new(big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
373387
{input: "10", ptr: new(big.Int), value: *big.NewInt(16)}, // non-pointer also works
374388
{input: "C0", ptr: new(*big.Int), error: "rlp: expected input string or byte for *big.Int"},
389+
{input: "820001", ptr: new(big.Int), error: "rlp: non-canonical integer (leading zero bytes) for *big.Int"},
390+
{input: "8105", ptr: new(big.Int), error: "rlp: non-canonical size information for *big.Int"},
375391

376392
// structs
377393
{input: "C0", ptr: new(simplestruct), value: simplestruct{0, ""}},
@@ -404,7 +420,7 @@ var decodeTests = []decodeTest{
404420
{input: "80", ptr: new(*uint), value: (*uint)(nil)},
405421
{input: "C0", ptr: new(*uint), value: (*uint)(nil)},
406422
{input: "07", ptr: new(*uint), value: uintp(7)},
407-
{input: "8108", ptr: new(*uint), value: uintp(8)},
423+
{input: "8158", ptr: new(*uint), value: uintp(0x58)},
408424
{input: "C109", ptr: new(*[]uint), value: &[]uint{9}},
409425
{input: "C58403030303", ptr: new(*[][]byte), value: &[][]byte{{3, 3, 3, 3}}},
410426

0 commit comments

Comments
 (0)