diff --git a/encoding/protojson/decode.go b/encoding/protojson/decode.go index e8552f00..cb4f7fbc 100644 --- a/encoding/protojson/decode.go +++ b/encoding/protojson/decode.go @@ -184,6 +184,9 @@ func (o UnmarshalOptions) unmarshalFields(m pref.Message, skipTypeURL bool) erro } if extType != nil { fd = extType.TypeDescriptor() + if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() { + return errors.New("message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName()) + } } } else { // The name can either be the JSON name or the proto field name. diff --git a/encoding/prototext/decode.go b/encoding/prototext/decode.go index 1bf25c27..99ef5dcd 100644 --- a/encoding/prototext/decode.go +++ b/encoding/prototext/decode.go @@ -128,6 +128,9 @@ func (o UnmarshalOptions) unmarshalMessage(tmsg [][2]text.Value, m pref.Message) } if xt != nil { fd = xt.TypeDescriptor() + if !messageDesc.ExtensionRanges().Has(fd.Number()) || fd.ContainingMessage().FullName() != messageDesc.FullName() { + return errors.New("message %v cannot be extended by %v", messageDesc.FullName(), fd.FullName()) + } } }