Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port fix for CVE-2024-24786 #152

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/protojson/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (d *Decoder) Read() (Token, error) {

case ObjectClose:
if len(d.openStack) == 0 ||
d.lastToken.kind == comma ||
d.lastToken.kind&(Name|comma) != 0 ||
d.openStack[len(d.openStack)-1] != ObjectOpen {
return Token{}, d.newSyntaxError(tok.pos, unexpectedFmt, tok.RawString())
}
Expand Down
15 changes: 15 additions & 0 deletions internal/protojson/json/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,21 @@ func TestDecoder(t *testing.T) {
{E: errEOF},
},
},
{
in: `{""`,
want: []R{
{V: ObjectOpen},
{E: errEOF},
},
},
{
in: `{"":`,
want: []R{
{V: ObjectOpen},
{V: Name{""}},
{E: errEOF},
},
},
{
in: `{"34":"89",}`,
want: []R{
Expand Down
17 changes: 17 additions & 0 deletions internal/protojson/json_unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2503,6 +2503,23 @@ func TestUnmarshal(t *testing.T) {
inputText: `{"foo":{"bar":[{"baz":[{}]]}}`,
umo: protojson.UnmarshalOptions{RecursionLimit: 5, DiscardUnknown: true},
wantErr: "exceeded max recursion depth",
}, {
desc: "Object missing value: no DiscardUnknown",
inputMessage: &testpb.TestAllTypes{},
inputText: `{"":}`,
umo: protojson.UnmarshalOptions{RecursionLimit: 5, DiscardUnknown: false},
wantErr: `(line 1:2): unknown field ""`,
}, {
desc: "Object missing value: DiscardUnknown",
inputMessage: &testpb.TestAllTypes{},
inputText: `{"":}`,
umo: protojson.UnmarshalOptions{RecursionLimit: 5, DiscardUnknown: true},
wantErr: `(line 1:5): unexpected token`,
}, {
desc: "Object missing value: Any",
inputMessage: &anypb.Any{},
inputText: `{"":}`,
wantErr: `(line 1:5): unexpected token`,
}}

for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions internal/protojson/well_known_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ func (d decoder) skipJSONValue() error {
if open > d.opts.RecursionLimit {
return errors.New("exceeded max recursion depth")
}
case json.EOF:
// This can only happen if there's a bug in Decoder.Read.
// Avoid an infinite loop if this does happen.
return errors.New("unexpected EOF")
}
if open == 0 {
return nil
Expand Down
Loading