Skip to content

fix(NODE-7642): respect Binary position in toExtendedJSON - #907

Open
spokodev wants to merge 1 commit into
mongodb:mainfrom
spokodev:w32/bson-binary-ejson-position
Open

spokodev wants to merge 1 commit into
mongodb:mainfrom
spokodev:w32/bson-binary-ejson-position

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

Binary.toExtendedJSON() base64-encodes the entire backing buffer instead of the written region [0, position), corrupting Extended JSON output for any Binary built incrementally with the documented put() / write() API.

new Binary() allocates a 256-byte buffer and tracks how much is used in position. Every other accessor (value(), toJSON(), toString(), inspect()) slices to position, but toExtendedJSON() encoded this.buffer in full. So a Binary with 3 bytes written serialized to a 344-character base64 string of mostly zero padding instead of the 4 characters for those 3 bytes.

const bin = new BSON.Binary();
bin.put(1); bin.put(2); bin.put(3);
EJSON.stringify(bin);            // $binary.base64 was 'AQIDAAAA...' (256 bytes) instead of 'AQID'
EJSON.parse(EJSON.stringify(bin)).value(); // [1,2,3,0,0,...] instead of [1,2,3]

The BSON path already slices correctly, so BSON and Extended JSON representations of the same object disagreed. This violates MongoDB Extended JSON v2, where $binary.base64 is the payload, not the internal allocation.

Fix

-    const base64String = ByteUtils.toBase64(this.buffer);
+    const base64String = ByteUtils.toBase64(this.buffer.subarray(0, this.position));

Matches what value() / toJSON() / toString() already do.

Test

Added a toExtendedJSON() case to test/node/binary.test.ts (mirrors the existing toString / toJSON "should respect position" tests). It fails before the change (full-buffer base64) and passes after. The full node suite stays green.

@spokodev
spokodev requested a review from a team as a code owner July 2, 2026 09:54
@PavelSafronov

PavelSafronov commented Jul 2, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for taking the time to create this PR and providing so much detail. This PR will be tracked in NODE-7642 and the team will prioritize it in the next triage session.

@PavelSafronov PavelSafronov changed the title fix: respect Binary position in toExtendedJSON fix(NODE-7649): respect Binary position in toExtendedJSON Jul 2, 2026
@PavelSafronov PavelSafronov added tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR External Submission labels Jul 2, 2026
@PavelSafronov PavelSafronov changed the title fix(NODE-7649): respect Binary position in toExtendedJSON fix(NODE-7642): respect Binary position in toExtendedJSON Jul 6, 2026
Binary.toExtendedJSON base64-encoded the entire backing buffer instead
of the content region [0, position). A Binary built incrementally via
put()/write() (whose backing buffer is larger than its content, as
documented on the position field) therefore serialized trailing
allocator bytes through EJSON.stringify, corrupting the value and
breaking EJSON round-trips. This aligns toExtendedJSON with value(),
toJSON(), toString() and inspect(), which all slice to position.
@spokodev
spokodev force-pushed the w32/bson-binary-ejson-position branch from 445a564 to 63718c6 Compare October 5, 2026 19:15
@spokodev

spokodev commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

@tadjik1, #908 was approved on 5 October; here is where its two siblings stand.

This one is now rebased onto main (it was 16 commits behind). npm test passes, the two new tests run, and with the src/binary.ts change reverted, "should respect position when converting to Extended JSON" fails.

#911 (NODE-7661) is already current with main and behaves the same way: with its fix reverted, "matches the serialized byte length" fails.

Both will need evergreen authorized on their current heads.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Submission tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants