Skip to content

GODRIVER-1504 Restrict top-level keys from being parsed as extjson #340

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

Merged
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
4 changes: 4 additions & 0 deletions bson/bsonrw/extjson_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func newExtJSONParser(r io.Reader, canonical bool) *extJSONParser {
func (ejp *extJSONParser) peekType() (bsontype.Type, error) {
var t bsontype.Type
var err error
initialState := ejp.s

ejp.advanceState()
switch ejp.s {
Expand Down Expand Up @@ -113,6 +114,9 @@ func (ejp *extJSONParser) peekType() (bsontype.Type, error) {
case jpsInvalidState:
err = ejp.err
case jpsSawKey:
if initialState == jpsStartState {
return bsontype.EmbeddedDocument, nil
}
t = wrapperKeyBSONType(ejp.k)

switch t {
Expand Down
4 changes: 4 additions & 0 deletions bson/bsonrw/extjson_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ func TestExtJSONParserPeekType(t *testing.T) {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
ejp := newExtJSONParser(strings.NewReader(tc.input), true)
// Manually set the parser's starting state to jpsSawColon so peekType will read ahead to find the extjson
// type of the value. If not set, the parser will be in jpsStartState and advance to jpsSawKey, which will
// cause it to return without peeking the extjson type.
ejp.s = jpsSawColon

for i, eTyp := range tc.typs {
errF := tc.errFs[i]
Expand Down
1 change: 1 addition & 0 deletions bson/extjson_prose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestExtJSON(t *testing.T) {
{"timestamp - negative int32 value", `{"":{"$timestamp":{"t":0,"i":-1}}}`, false, timestampNegativeInt32Err},
{"timestamp - negative int64 value", `{"":{"$timestamp":{"t":0,"i":-2147483649}}}`, false, timestampNegativeInt64Err},
{"timestamp - value overflows uint32", `{"":{"$timestamp":{"t":0,"i":4294967296}}}`, false, timestampLargeValueErr},
{"top level key is not treated as special", `{"$code": "foo"}`, false, nil},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Expand Down