-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: js-bson
-
None
There's a few issues here:
The constructor for Binary seems to want to expect that it will only receive a few valid types (string, Array, Uint8Array, Number).
https://github.com/mongodb/js-bson/blob/ffed2c45377a253a054355f56e681867f58ca93e/lib/bson/binary.js#L42-L57
However this isn't the case, because any non-null object that isn't a string or number will just get assigned to the buffer, e.g.
> console.log(new bson.Binary(/foo/)) Binary { _bsontype: 'Binary', sub_type: 0, position: undefined, buffer: /foo/ }
Additionally, the extended json code assumes that an input Binary object will always contain a Buffer:
https://github.com/mongodb-js/mongodb-extjson/blob/a911f8d22c33dc433995a1a25ffd69fc0053a022/lib/bson/binary.js#L13
This means that Binary objects that were constructed with any type other than a Buffer will generate invalid output from stringify().
To fix this, either the Binary type's constructor should be more strict about what types it accepts (or convert input types into Buffer) or the stringify() code should be capable of generating the valid base64 from a Array/Uint8Array, etc.