-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: BSON, Security, Wire Protocol
-
None
Summary
An attacker whose raw document bytes an application forwards to MongoDB can smuggle extra command fields, silently changing what the database reads or writes.
Impact
Attacker-controlled BSON fields are injected into MongoDB commands outside the slot the application intended, enabling query/write manipulation (e.g. multi/upsert flips, bypassDocumentValidation, extra command options) executed under the application's database credentials.
Severity: high (upper bound critical)
Exploitability: exploitable — Deterministic and fully attacker-controlled: craft a valid ≥5-byte document, set its size prefix smaller than the byte slice, append arbitrary BSON elements — no race, no memory layout, no special mode.
Customer data: customer_content (api_response)
Location
- bson/src/main/org/bson/BsonBinaryWriter.java:345 in pipe(byte[], int, int)
- bson/src/main/org/bson/codecs/RawBsonDocumentCodec.java:49 in encode
- bson/src/main/org/bson/RawBsonDocument.java:105 in RawBsonDocument(byte[], int, int)
- driver-core/src/main/com/mongodb/internal/connection/CommandMessage.java:429 in writeCommand
- driver-core/src/main/com/mongodb/internal/connection/SplittablePayload.java:234 in WriteRequestEncoder.encode
Reproduction / trigger path
Analyzed trigger path from static analysis — not an executed PoC. Confirm with a concrete repro before handing off.
Attacker controls: Full control of the raw document bytes, including the embedded size prefix and the smuggled trailing elements; a prefix larger than the slice instead swallows subsequent legitimate fields into the attacker's document, corrupting or repurposing them.
- (source) bson/src/main/org/bson/RawBsonDocument.java:105 in RawBsonDocument(byte[], int, int) — Application wraps externally supplied raw BSON bytes; constructor checks bounds and length >= 5 but never the embedded size prefix, so a mismatched document is accepted.
- (hop) bson/src/main/org/bson/codecs/BsonDocumentCodec.java:106 in encode — Any RawBsonDocument anywhere in a command/filter/pipeline tree is dispatched to RawBsonDocumentCodec.
- (hop) bson/src/main/org/bson/codecs/RawBsonDocumentCodec.java:49 in encode — 5.8 fast path: writer is a plain BsonBinaryWriter on the wire path, so the backing array, offset, and length are passed straight to the byte-array pipe overload.
- (hop) driver-core/src/main/com/mongodb/internal/connection/CommandMessage.java:429 in writeCommand — Command document is encoded with an undecorated BsonBinaryWriter directly into the OP_MSG output buffer that is sent to the MongoDB server (update/delete filters via SplittablePayload.java:234 take the same undecorated writer).
- (sink) bson/src/main/org/bson/BsonBinaryWriter.java:353 in pipe(byte[], int, int) — All `length` bytes written verbatim; the inner document's self-declared size prefix disagrees with its actual extent, so the server's parser treats the trailing attacker bytes as additional sibling command fields.
Root cause
- BsonBinaryWriter.pipe(byte[], int, int) validates only length >= 5 (bson/src/main/org/bson/BsonBinaryWriter.java:347) and writes all `length` bytes verbatim (line 353); it never compares the embedded 4-byte BSON size prefix inside `bytes` to `length`, and validateSize (line 408) checks only the actual written length after the write.
- RawBsonDocument's byte-array constructors validate bounds and minimum size but not the prefix (bson/src/main/org/bson/RawBsonDocument.java:105-114), so a prefix/length mismatch survives construction.
- RawBsonDocumentCodec.encode's 5.8 fast path (bson/src/main/org/bson/codecs/RawBsonDocumentCodec.java:49) passes the backing array straight to pipe whenever the writer is a plain BsonBinaryWriter, which it is for the command document (driver-core/src/main/com/mongodb/internal/connection/CommandMessage.java:427-429) and for update/delete statement filters (driver-core/src/main/com/mongodb/internal/connection/SplittablePayload.java:234); BsonDocumentCodec.encode:106-108 routes any nested RawBsonDocument there.
- Enclosing document and message sizes are backpatched from actual byte positions (driver-core/src/main/com/mongodb/internal/connection/BsonWriterHelper.java:159-161), so the OP_MSG frames correctly while the inner document's declared size disagrees with its real extent — the server skips by the declared prefix and parses the trailing attacker bytes as additional sibling command/statement fields.
Ownership
Assigned teams: @mongodb/dbx-java
Filed from Aegis finding 656fe2af450d (scan scan-1094d087f7de) · primitive: Attacker-declared inner size prefix desynchronizes server-side BSON framing, injecting sibling fields into the outbound database command. · categories: NoSQL Injection, Data Corruption, Missing Authorization