proto: Prevent Any protos from being deserialized multiple times.
This avoids people specifying protos (inadvertently) as:
some_repeated_any {
[type.googleapis.com/a/path/proto.Type] {
blah: 1
}
[type.googleapis.com/a/path/proto.Type] {
blah: 2
}
}
when they meant:
some_repeated_any {
[type.googleapis.com/a/path/proto.Type] {
blah: 1
}
}
some_repeated_any {
[type.googleapis.com/a/path/proto.Type] {
blah: 2
}
}
diff --git a/proto/any_test.go b/proto/any_test.go
index 83492c5..1a3c22e 100644
--- a/proto/any_test.go
+++ b/proto/any_test.go
@@ -239,7 +239,7 @@
}
}
-func TestMarsahlUnknownAny(t *testing.T) {
+func TestMarshalUnknownAny(t *testing.T) {
m := &pb.Message{
Anything: &anypb.Any{
TypeUrl: "foo",
@@ -260,13 +260,41 @@
func TestAmbiguousAny(t *testing.T) {
pb := &anypb.Any{}
err := proto.UnmarshalText(`
- [type.googleapis.com/proto3_proto.Nested]: <
- bunny: "Monty"
- >
type_url: "ttt/proto3_proto.Nested"
+ value: "\n\x05Monty"
`, pb)
t.Logf("result: %v (error: %v)", expandedMarshaler.Text(pb), err)
if err != nil {
t.Errorf("failed to parse ambiguous Any message: %v", err)
}
}
+
+func TestUnmarshalOverwriteAny(t *testing.T) {
+ pb := &anypb.Any{}
+ err := proto.UnmarshalText(`
+ [type.googleapis.com/a/path/proto3_proto.Nested]: <
+ bunny: "Monty"
+ >
+ [type.googleapis.com/a/path/proto3_proto.Nested]: <
+ bunny: "Rabbit of Caerbannog"
+ >
+ `, pb)
+ want := `line 7: Any message unpacked multiple times, or "type_url" already set`
+ if err.Error() != want {
+ t.Errorf("incorrect error.\nHave: %v\nWant: %v", err.Error(), want)
+ }
+}
+
+func TestUnmarshalAnyMixAndMatch(t *testing.T) {
+ pb := &anypb.Any{}
+ err := proto.UnmarshalText(`
+ value: "\n\x05Monty"
+ [type.googleapis.com/a/path/proto3_proto.Nested]: <
+ bunny: "Rabbit of Caerbannog"
+ >
+ `, pb)
+ want := `line 5: Any message unpacked multiple times, or "value" already set`
+ if err.Error() != want {
+ t.Errorf("incorrect error.\nHave: %v\nWant: %v", err.Error(), want)
+ }
+}
diff --git a/proto/text_parser.go b/proto/text_parser.go
index 0b8c59f..7e6f145 100644
--- a/proto/text_parser.go
+++ b/proto/text_parser.go
@@ -44,6 +44,9 @@
"unicode/utf8"
)
+// Error string emitted when deserializing Any and fields are already set
+const anyRepeatedlyUnpacked = "Any message unpacked multiple times, or %q already set"
+
type ParseError struct {
Message string
Line int // 1-based line number
@@ -508,8 +511,16 @@
if err != nil {
return p.errorf("failed to marshal message of type %q: %v", messageName, err)
}
+ if fieldSet["type_url"] {
+ return p.errorf(anyRepeatedlyUnpacked, "type_url")
+ }
+ if fieldSet["value"] {
+ return p.errorf(anyRepeatedlyUnpacked, "value")
+ }
sv.FieldByName("TypeUrl").SetString(extName)
sv.FieldByName("Value").SetBytes(b)
+ fieldSet["type_url"] = true
+ fieldSet["value"] = true
continue
}