Skip to content

Commit dee2cf1

Browse files
committed
PYTHON-XXXX Reject extra fields in JSON timestamps
1 parent 78f3a79 commit dee2cf1

4 files changed

Lines changed: 17 additions & 0 deletions

File tree

bson/json_util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,8 @@ def _parse_binary(doc: Any, json_options: JSONOptions) -> Union[Binary, uuid.UUI
789789

790790
def _parse_timestamp(doc: Any, dummy0: Any) -> Timestamp:
791791
tsp = doc["$timestamp"]
792+
if set(tsp) != {"t", "i"}:
793+
raise TypeError(f'$timestamp must include exactly "t" and "i" components: {doc}')
792794
return Timestamp(tsp["t"], tsp["i"])
793795

794796

doc/changelog.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ PyMongo 4.18 brings a number of changes including:
2525
the bytes remaining in the array now raises
2626
:class:`~bson.errors.InvalidBSON` instead of reading past the end of the
2727
buffer.
28+
- Fixed :func:`bson.json_util.loads` to reject ``$timestamp`` values containing
29+
fields other than ``t`` and ``i``.
2830

2931
Changes in Version 4.17.0 (2026/04/20)
3032
--------------------------------------

doc/contributors.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ The following is a list of people who have contributed to
109109
- Noah Stapp (NoahStapp)
110110
- Cal Jacobson (cj81499)
111111
- Sophia Yang (sophiayangDB)
112+
- Madan Kumar (winklemad)

test/test_json_util.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,18 @@ def test_timestamp(self):
405405
self.assertEqual(dct, rtdct)
406406
self.assertEqual('{"ts": {"$timestamp": {"t": 4, "i": 13}}}', res)
407407

408+
def test_timestamp_with_invalid_fields(self):
409+
invalid_values = [
410+
'{"t": 4, "i": 13, "extra": 1}',
411+
'{"t": 4, "unexpected": 13}',
412+
]
413+
for value in invalid_values:
414+
with self.subTest(value=value):
415+
with self.assertRaisesRegex(
416+
TypeError, r'\$timestamp must include exactly "t" and "i" components'
417+
):
418+
json_util.loads(f'{{"ts": {{"$timestamp": {value}}}}}')
419+
408420
def test_uuid_default(self):
409421
# Cannot directly encode native UUIDs with the default
410422
# uuid_representation.

0 commit comments

Comments
 (0)