Harden BsonBinaryReader.skipValue() size validation#1990
Open
rozza wants to merge 3 commits into
Open
Conversation
The skip path in doSkipValue() was more permissive than the canonical decode path, accepting malformed BSON sizes that full decoding rejects. This allowed RawBsonDocument metadata accessors (size(), containsKey(), get()) to accept attacker-controlled malformed data as valid fields. Add type-specific validation helpers to doSkipValue(): - readStringSize(): rejects size 0 (STRING, SYMBOL, JAVASCRIPT, DB_POINTER) - readContainerSize(min): rejects undersized containers (DOCUMENT, ARRAY, JAVASCRIPT_WITH_SCOPE) that would produce negative skips - readJavaScriptWithScopeSize(): validates nested code/scope sizes and total-size consistency - addTrailingBytes(): guards against int overflow for BINARY and DB_POINTER BINARY size 0 remains valid (empty binary is spec-compliant). JAVA-6220
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens BsonBinaryReader.skipValue() so its skip-path size handling more closely matches the canonical decode-path validation, preventing RawBsonDocument metadata accessors (size(), containsKey(), get()) from accepting certain malformed BSON payloads as valid (JAVA-6220).
Changes:
- Add type-specific size validation helpers in
BsonBinaryReader.doSkipValue()(strings, containers, JavaScript-with-scope, overflow-safe trailing bytes). - Add focused
BsonBinaryReaderTestcoverage for malformed sizes (zero-length strings/symbol/js/dbpointer, undersized containers, JS-with-scope consistency checks, overflow guards) and minimal-valid regressions. - Add
RawBsonDocumentTestregression tests demonstrating that metadata accessors reject malformed zero-length string values when iteration requiresskipValue().
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| bson/src/main/org/bson/BsonBinaryReader.java | Tightens skip-path validation via helpers for string/container sizes, JS-with-scope consistency, and overflow-safe skip calculations. |
| bson/src/test/unit/org/bson/BsonBinaryReaderTest.java | Adds comprehensive unit tests exercising newly hardened skipValue() validation paths plus minimal-valid regressions. |
| bson/src/test/unit/org/bson/RawBsonDocumentTest.java | Adds regression tests ensuring RawBsonDocument metadata accessors reject malformed documents that require skipValue() iteration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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 skip path in doSkipValue() was more permissive than the canonical decode path, accepting malformed BSON sizes that full decoding rejects. This allowed RawBsonDocument metadata accessors (size(), containsKey(), get()) to accept attacker-controlled malformed data as valid fields.
Add type-specific validation helpers to doSkipValue():
BINARY size 0 remains valid (empty binary is spec-compliant).
JAVA-6220