Skip to content

Commit d8aac26

Browse files
committed
proto, jsonpb: fix handling of extensions
Check whether the parsed extension type actually matches the target message type. Change-Id: Ib21226b0bc217e33ebf56a1961ebc20732b5c64e Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/220438 Reviewed-by: Damien Neil <[email protected]>
1 parent a9f2576 commit d8aac26

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

jsonpb/decode.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ func (u *Unmarshaler) unmarshalMessage(m protoreflect.Message, in []byte) error
361361
}
362362
delete(jsonObject, name)
363363
fd := xt.TypeDescriptor()
364+
if fd.ContainingMessage().FullName() != m.Descriptor().FullName() {
365+
return fmt.Errorf("extension field %q does not extend message %q", xname, m.Descriptor().FullName())
366+
}
364367

365368
// Unmarshal the field value.
366369
if raw == nil || (string(raw) == "null" && !isSingularWellKnownValue(fd)) {

jsonpb/json_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,8 @@ var unmarshalingShouldError = []struct {
910910
{"StringValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &pb2.KnownTypes{}},
911911
{"StructValue containing invalid character", `{"str": "\U00004E16\U0000754C"}`, &stpb.Struct{}},
912912
{"repeated proto3 enum with non array input", `{"rFunny":"PUNS"}`, &pb3.Message{RFunny: []pb3.Message_Humour{}}},
913+
{"unknown extension field", `{"[ext_unknown]": "value"}`, &pb2.Real{}},
914+
{"extension field for wrong message", `{"[jsonpb_test.name]": "value"}`, &pb2.Complex{}},
913915
}
914916

915917
func TestUnmarshalingBadInput(t *testing.T) {

proto/text_decode.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ func (p *textParser) unmarshalExtensionOrAny(m protoreflect.Message, seen map[pr
236236
return p.errorf("unrecognized extension %q", name)
237237
}
238238
fd := xt.TypeDescriptor()
239+
if fd.ContainingMessage().FullName() != m.Descriptor().FullName() {
240+
return p.errorf("extension field %q does not extend message %q", name, m.Descriptor().FullName())
241+
}
239242

240243
if err := p.checkForColon(fd); err != nil {
241244
return err

proto/text_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,10 @@ var unmarshalTextTests = []UnmarshalTextTest{
11961196
buildExtStructTest(`count: 42 [proto2_test.Ext.more] {data:"Hello, world!"}`),
11971197
buildExtDataTest(`count: 42 [proto2_test.Ext.text]:"Hello, world!" [proto2_test.Ext.number]:1729`),
11981198
buildExtRepStringTest(`count: 42 [proto2_test.greeting]:"bula" [proto2_test.greeting]:"hola"`),
1199+
{
1200+
in: `[proto2_test.complex]:<>`,
1201+
err: `line 1.20: extension field "proto2_test.complex" does not extend message "proto2_test.MyMessage"`,
1202+
},
11991203

12001204
// Big all-in-one
12011205
{

0 commit comments

Comments
 (0)