Skip to content

Fix BSON conformance issue#5185

Open
nlohmann wants to merge 3 commits into
developfrom
bson-sizes
Open

Fix BSON conformance issue#5185
nlohmann wants to merge 3 commits into
developfrom
bson-sizes

Conversation

@nlohmann
Copy link
Copy Markdown
Owner

Summary

Fixes two BSON specification conformance issues in nlohmann::json::from_bson():

  1. The 4-byte int32 document_size at the start of a BSON document or array
    is now enforced as a boundary. Documents with document_size < 5 are
    rejected, and parsing must end exactly at the declared boundary (after
    consuming the trailing 0x00 document terminator).
  2. The terminator byte of a length-prefixed BSON string (element type 0x02)
    is now required to be 0x00. Previously any non-EOF byte was accepted.

Changes

  • include/nlohmann/detail/input/binary_reader.hpp
    • parse_bson_internal(), parse_bson_array(): validate
      document_size >= 5; track the document start position; verify parsing
      consumes exactly document_size bytes.
    • get_bson_string(): require the terminator byte to be 0x00.
  • tests/src/unit-bson.cpp: regression tests covering the newly-rejected
    inputs.

Notes

The binary-format readers (from_bson, from_cbor, from_msgpack,
from_ubjson, from_bjdata) are not designed to validate untrusted input.
An attacker who can submit bytes to from_bson() can already cause arbitrary
JSON values to be returned via a spec-conformant payload. These changes close
conformance gaps that should be enforced regardless.

A GitHub Security Advisory will be published referencing this PR.

Reported by @manop55555.

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
@nlohmann nlohmann added the review needed It would be great if someone could review the proposed changes. label May 19, 2026
Comment on lines +314 to +323
{
{
0.0L, -0.0L, 1.0L, -1.0L,
0.5L, -0.5L, 1.5L, -2.25L,
1.23e45L, 1.23e-45L,
(std::numeric_limits<long double>::min)(),
std::numeric_limits<long double>::lowest(),
(std::numeric_limits<long double>::max)()
}
};
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This is an unrelated whitespace change that was done by astyle.

nlohmann added 2 commits May 19, 2026 14:32
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

L review needed It would be great if someone could review the proposed changes. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant