all: remove non-fatal UTF-8 validation errors (and non-fatal in general)
Immediately abort (un)marshal operations when encountering invalid UTF-8
data in proto3 strings. No other proto implementation supports non-UTF-8
data in proto3 strings (and many reject it in proto2 strings as well).
Producing invalid output is an interoperability threat (other
implementations won't be able to read it).
The case where existing string data is found to contain non-UTF8 data is
better handled by changing the field to the `bytes` type, which (aside
from UTF-8 validation) is wire-compatible with `string`.
Remove the errors.NonFatal type, since there are no remaining cases
where it is needed. "Non-fatal" errors which produce results and a
non-nil error are problematic because they compose poorly; the better
approach is to take an option like AllowPartial indicating which
conditions to check for.
Change-Id: I9d189ec6ffda7b5d96d094aa1b290af2e3f23736
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/183098
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/encoding/prototext/decode_test.go b/encoding/prototext/decode_test.go
index c681591..32d9be8 100644
--- a/encoding/prototext/decode_test.go
+++ b/encoding/prototext/decode_test.go
@@ -10,7 +10,6 @@
protoV1 "github.com/golang/protobuf/proto"
"google.golang.org/protobuf/encoding/prototext"
- "google.golang.org/protobuf/internal/errors"
pimpl "google.golang.org/protobuf/internal/impl"
"google.golang.org/protobuf/internal/scalar"
"google.golang.org/protobuf/proto"
@@ -157,10 +156,7 @@
desc: "string with invalid UTF-8",
inputMessage: &pb3.Scalars{},
inputText: `s_string: "abc\xff"`,
- wantMessage: &pb3.Scalars{
- SString: "abc\xff",
- },
- wantErr: true,
+ wantErr: true,
}, {
desc: "proto2 message contains unknown field",
inputMessage: &pb2.Scalars{},
@@ -459,11 +455,6 @@
s_string: "abc\xff"
}
`,
- wantMessage: &pb3.Nests{
- SNested: &pb3.Nested{
- SString: "abc\xff",
- },
- },
wantErr: true,
}, {
desc: "oneof set to empty string",
@@ -556,10 +547,7 @@
desc: "repeated contains invalid UTF-8",
inputMessage: &pb2.Repeats{},
inputText: `rpt_string: "abc\xff"`,
- wantMessage: &pb2.Repeats{
- RptString: []string{"abc\xff"},
- },
- wantErr: true,
+ wantErr: true,
}, {
desc: "repeated enums",
inputMessage: &pb2.Enums{},
@@ -878,11 +866,6 @@
value: "abc\xff"
}
`,
- wantMessage: &pb3.Maps{
- Int32ToStr: map[int32]string{
- 101: "abc\xff",
- },
- },
wantErr: true,
}, {
desc: "map field key contains invalid UTF-8",
@@ -892,11 +875,6 @@
value: {}
}
`,
- wantMessage: &pb3.Maps{
- StrToNested: map[string]*pb3.Nested{
- "abc\xff": {},
- },
- },
wantErr: true,
}, {
desc: "map contains unknown field",
@@ -1196,12 +1174,7 @@
desc: "extension field contains invalid UTF-8",
inputMessage: &pb2.Extensions{},
inputText: `[pb2.opt_ext_string]: "abc\xff"`,
- wantMessage: func() proto.Message {
- m := &pb2.Extensions{}
- setExtension(m, pb2.E_OptExtString, "abc\xff")
- return m
- }(),
- wantErr: true,
+ wantErr: true,
}, {
desc: "extensions of repeated fields",
inputMessage: &pb2.Extensions{},
@@ -1466,20 +1439,6 @@
s_string: "abc\xff"
}
`,
- wantMessage: func() proto.Message {
- m := &pb3.Nested{
- SString: "abc\xff",
- }
- var nerr errors.NonFatal
- b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
- if !nerr.Merge(err) {
- t.Fatalf("error in binary marshaling message for Any.value: %v", err)
- }
- return &anypb.Any{
- TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
- Value: b,
- }
- }(),
wantErr: true,
}, {
desc: "Any expanded with unregistered type",