-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
Use Case
As a... consumer of the experimental BSON.onDemand API (including the Node driver's on-demand document reader)
I want... parseToElements to reject string-family elements whose declared length is invalid
So that... the on-demand and standard parsers agree on what a valid BSON document is, and malformed bytes surface as an error rather than a silently empty or truncated value
User Experience
onDemand.parseToElements throws a BSONOffsetError for a string, javascript or symbol element (and the string half of dbPointer) whose declared size is less than 1 or whose final byte is not 0x00 — matching the bad string length in bson check the standard deserializer already applies (src/parser/deserializer.ts L392-397). Valid documents are unaffected, including zero-length binData, which is legal and must keep parsing.
No hang, crash or memory-safety issue, and negative sizes are already rejected on both paths by getNonnegativeInt32LE. Today the on-demand path accepts two shapes the standard path rejects: a string declaring size 0 (read back as ""), and a string whose declared length does not end on a null terminator (read back truncated).
Dependencies
Follow-up to NODE-7611 (PR #924), which fixed the non-terminating scan and added a "value must fit within its document" guard but left this per-type check out. Not part of the three gaps NODE-7598 covers, so it does not hold that ticket open.
Risks/Unknowns
The length calculation for string, binData, dbPointer, javascript and symbol shares one branch in parse_to_elements.ts, so a blanket "size must be >= 1" would wrongly reject an empty Binary. The check has to be per-type. parseToElements is on the driver's hot path for every response document, so validation must stay O(1) per element.
Acceptance Criteria
Implementation Requirements
In src/parser/on_demand/parse_to_elements.ts, require a declared size of at least 1 and a 0x00 at the last byte of the value for string, javascript, symbol and the string component of dbPointer. binData continues to allow a size of 0. Errors are BSONOffsetError with the element's offset.
Testing Requirements
Unit tests in test/node/parser/on_demand/parse_to_elements.test.ts covering size 0, a declared length whose final byte is not a terminator, each affected type, and a zero-length Binary that must still parse. Assert that parseToElements and BSON.deserialize agree on these inputs.
Documentation Requirements
None.
Follow Up Requirements
None.