Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-6349

Node v22.7.0 can produce invalid UTF-8 strings that can result in malformed BSON

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • Not Needed

      Due to https://github.com/nodejs/node/issues/54543, when the following is run in Node v22.7.0, invalid BSON can be produced.

      import { BSON } from "mongodb";
      
      const value = 'bébé';
      async function run() {
        console.log(BSON.serialize({ _id: 1, message: value }));
        let i = 0;
        while (Buffer.from(value).length === 6 && i < 20000) { i++ }
      
        console.log(BSON.serialize({ _id: 1, message: value }));
      }
      run().catch(console.dir);
      
      // output
      <Buffer 22 00 00 00 10 5f 69 64 00 01 00 00 00 02 6d 65 73 73 61 67 65 00 07 00 00 00 62 c3 a9 62 c3 a9 00 00>
      <Buffer 20 00 00 00 10 5f 69 64 00 01 00 00 00 02 6d 65 73 73 61 67 65 00 05 00 00 00 62 e9 62 e9 00 00>
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: