-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: BSON
-
None
BSON::ExtJSON.parse does not validate the types inside a $dbPointer wrapper. The $dbPointer branch (lib/bson/ext_json.rb:262-266) checks only that the keys are exactly $ref and $id, then calls DbPointer.new(value['$ref'], parse_hash(value['$id'])). parse_hash returns whatever the nested wrapper produces, so a $binary id yields a BSON::Binary and a plain object yields a Hash. DbPointer#initialize (lib/bson/db_pointer.rb:33-36) stores both unchecked; the String/ObjectId contract exists only in doc comments.
DbPointer#to_bson (lib/bson/db_pointer.rb:81-85) then calls id.to_bson(buffer) duck-typed. The DBPointer wire format (type 0x0C) is a string followed by exactly 12 raw bytes, but Binary#to_bson (lib/bson/binary.rb:274-280) writes an int32 length, a subtype byte, then the payload at whatever length was supplied. The enclosing document length prefix is stamped after serialization, so the document stays internally consistent and the driver sends it.
Every conforming parser, including this library's own DbPointer.from_bson, reads the string and then exactly 12 bytes, and interprets the remaining bytes as fresh BSON elements. The result is fields appearing in the document that the application never wrote, and the application's own trailing fields being consumed or replaced.
Confirmed end to end on bson 5.2.0 with driver 2.22.0 against MongoDB 9.0.0-rc0. An application that parses Extended JSON and writes the parsed value into a document inserts a document that the server accepts, stores and returns with an extra field the application never wrote, and without the field the application did write. Server-side count queries match the injected value. When the parsed value is the last field of its document or subdocument, the injection needs no knowledge of the surrounding layout at all, because the document terminator absorbs the tail. The same works for a value nested inside a subdocument.
Ids of other shapes fail, but not cleanly: serialization raises NoMethodError, RangeError or BSON::Error::BSONDecodeError rather than a parse error. A non-String or null $ref raises NoMethodError from put_string.
The extended JSON specification defines $id as an ObjectId (extended-json.md line 97). The Python driver enforces this explicitly in _parse_canonical_dbpointer, raising "Bad $dbPointer, $id must be an ObjectId". The Go driver validates both $ref and $id types and stores an ObjectID (bson/extjson_wrappers.go:120-167). Java's BsonDbPointer takes String and ObjectId with null checks, and libbson's bson_append_dbpointer takes a bson_oid_t pointer. bson-ruby is the only implementation without the check.
The code is unchanged since it was added in RUBY-2086, so all releases from 4.8.0 through 5.2.0 are affected, on master, 4-stable and 4.15-stable. Both the C extension and the JRuby paths are affected, because the duck-typed dispatch is in Ruby. Neither the driver nor Mongoid reaches this path with untrusted input; the only in-driver caller is the FLE schema map file at lib/mongo/crypt/handle.rb:181.
Fix: in the $dbPointer branch, require $ref to be a String and $id to be a BSON::ObjectId, raising Error::ExtJSONParseError otherwise. As defense in depth, enforce the 12-byte length in the serialization path so that no DbPointer or ObjectId can emit a wrong-sized id regardless of how it was constructed; that also covers the related ObjectId repair path reported in SECBUG-4391. The BSON corpus dbpointer.json fixtures contain no parseErrors cases, so add fixtures for the rejected inputs.
Reported as SECBUG-4392.