validate BSON document size against the bytes read#5287
Open
Angadi56 wants to merge 1 commit into
Open
Conversation
Signed-off-by: Angadi Yashaswini <angadi@digiscrypt.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The BSON reader takes the document length from the int32 at the start of every document and array, but never checks it. Parsing is driven entirely by the trailing
0x00, so the declared length is read into a local and discarded. That lets a nested document claim a length that disagrees with where its terminator actually falls: an embedded document can declare far more bytes than it holds, terminate early, and the elements that its length said belonged to it get read as members of the enclosing document instead. On an embedded size ofFF FF FF 7Fin front of{"a":null}and then an"h"field,from_bsonreturns{"d":{"a":null},"h":true}where a length-aware parser rejects the input, so two BSON readers disagree on the shape of the same bytes. I found it while lining the BSON path up against the other decoders, which all validate their length fields. The fix records the offset before the size prefix and, once the element list and its terminator are read, checks that the bytes consumed match the declared size, in bothparse_bson_internalandparse_bson_array. A negative or below-minimum size is caught by the same comparison, since at least the 5-byte minimum is always read.make amalgamate.