proto, internal/impl: reject invalid field numbers in map items

Change-Id: I44a44a36538f6f8b94078b43711d865edb6244f5
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/212257
Reviewed-by: Herbie Ong <herbie@google.com>
This commit is contained in:
Damien Neil 2019-12-20 09:43:20 -08:00
parent 2c0824b512
commit f2427c09d6
3 changed files with 21 additions and 0 deletions

View File

@ -5,6 +5,7 @@
package impl
import (
"errors"
"reflect"
"sort"
@ -120,6 +121,9 @@ func consumeMap(b []byte, mapv reflect.Value, wtyp wire.Type, mapi *mapInfo, opt
if n < 0 {
return 0, wire.ParseError(n)
}
if num > wire.MaxValidNumber {
return 0, errors.New("invalid field number")
}
b = b[n:]
err := errUnknown
switch num {
@ -169,6 +173,9 @@ func consumeMapOfMessage(b []byte, mapv reflect.Value, wtyp wire.Type, mapi *map
if n < 0 {
return 0, wire.ParseError(n)
}
if num > wire.MaxValidNumber {
return 0, errors.New("invalid field number")
}
b = b[n:]
err := errUnknown
switch num {

View File

@ -183,6 +183,9 @@ func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp wire.Type, mapv protorefle
if n < 0 {
return 0, wire.ParseError(n)
}
if num > wire.MaxValidNumber {
return 0, errors.New("invalid field number")
}
b = b[n:]
err = errUnknown
switch num {

View File

@ -1608,4 +1608,15 @@ var testInvalidMessages = []testProto{
pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(1008),
}.Marshal(),
},
{
desc: "invalid field number in map",
decodeTo: []proto.Message{(*testpb.TestAllTypes)(nil)},
wire: pack.Message{
pack.Tag{56, pack.BytesType}, pack.LengthPrefix(pack.Message{
pack.Tag{1, pack.VarintType}, pack.Varint(1056),
pack.Tag{2, pack.VarintType}, pack.Varint(1156),
pack.Tag{pack.MaxValidNumber + 1, pack.VarintType}, pack.Varint(0),
}),
}.Marshal(),
},
}