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

BSON and EJSON incorrectly handle dates outside of JS's max date range

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: BSON, EJSON

      Use Case

      BSON and EJSON currently have questionable behavior when serializing dates:

      EJSON

      > ejson.stringify({ d : new Date(2 ** 60 ) })
      '{"d":{"$date":{"$numberLong":"NaN"}}}'
      > ejson.parse(ejson.stringify({ d : new Date(2 ** 60 ) }))
      Uncaught BSONError: $numberLong string "NaN" is in an invalid format
      

      BSON

      > bson.serialize({ a: new Date(2 ** 60 ) })
      <Buffer 10 00 00 00 09 61 00 00 00 00 00 00 00 00 00 00>
      > bson.deserialize(bson.serialize({ a: new Date(2 ** 60 ) }))
      { a: 1970-01-01T00:00:00.000Z }
      

      Both of these behaviors are problematic because they allow users to serialize data but then prevent the user from correctly deserializing it. Anything we serialize (with bson or EJSON) with errors should be deserializable with no loss of precision or modifications of data.

      *BSON and EJSON should throw an error when attempting to serialize an invalid date.

      This behavior has existed since bson@1.x.

      User Impact

      • Unsure. It seems unlikely that users are round-tripping invalid dates through BSON, but it's possible.

      Dependencies

      • n/a

      Unknowns

      • Does this need to be done in a major release?

      Acceptance Criteria

      Implementation Requirements

      • Throw an error when serializing a date if `Date.getTime()` returns `NaN`. This applies to both BSON and EJSON.

      Testing Requirements

      Documentation Requirements

      n/a

      Follow Up Requirements

      n/a

            Assignee:
            Unassigned Unassigned
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: