diff --git a/encoding/protojson/encode_test.go b/encoding/protojson/encode_test.go index 5e1570a4..f1a05fe1 100644 --- a/encoding/protojson/encode_test.go +++ b/encoding/protojson/encode_test.go @@ -1255,6 +1255,18 @@ func TestMarshal(t *testing.T) { {}, [] ]`, + }, { + desc: "Value with NaN", + input: structpb.NewNumberValue(math.NaN()), + wantErr: true, + }, { + desc: "Value with -Inf", + input: structpb.NewNumberValue(math.Inf(-1)), + wantErr: true, + }, { + desc: "Value with +Inf", + input: structpb.NewNumberValue(math.Inf(+1)), + wantErr: true, }, { desc: "Struct with nil map", input: &structpb.Struct{}, diff --git a/encoding/protojson/well_known_types.go b/encoding/protojson/well_known_types.go index 7a2fde33..72924a90 100644 --- a/encoding/protojson/well_known_types.go +++ b/encoding/protojson/well_known_types.go @@ -7,6 +7,7 @@ package protojson import ( "bytes" "fmt" + "math" "strconv" "strings" "time" @@ -495,6 +496,11 @@ func (e encoder) marshalKnownValue(m pref.Message) error { if fd == nil { return errors.New("%s: none of the oneof fields is set", genid.Value_message_fullname) } + if fd.Number() == genid.Value_NumberValue_field_number { + if v := m.Get(fd).Float(); math.IsNaN(v) || math.IsInf(v, 0) { + return errors.New("%s: invalid %v value", genid.Value_NumberValue_field_fullname, v) + } + } return e.marshalSingular(m.Get(fd), fd) }