PYTHON-5968 Reject extra fields in Extended JSON timestamps - #2961
Conversation
There was a problem hiding this comment.
Pull request overview
This PR tightens Extended JSON parsing for BSON timestamps by making bson.json_util.loads reject $timestamp value documents that contain keys other than the required t (time) and i (increment), preventing silent discarding of unexpected fields.
Changes:
- Enforce strict key validation for
$timestampduring JSON parsing. - Add regression tests for
$timestampdocuments with extra/invalid fields. - Update changelog and contributors list to reflect the fix and contribution.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
bson/json_util.py |
Adds stricter $timestamp parsing validation. |
test/test_json_util.py |
Adds regression tests covering invalid $timestamp key sets. |
doc/changelog.rst |
Documents the behavior change in the upcoming release notes. |
doc/contributors.rst |
Adds the contributor entry. |
|
Hi @winklemad, thank you for the fix! I agree with the copilot suggestion above. We should also add tests for the two new guards. |
|
Thanks @blink1073 — agreed on both. For the wrapper: I'll add the same I'll add regression tests for both new guards next to the existing one. Thanks for the review! |
|
Pushed both guards plus the two regression tests in a9d62cc — the outer-wrapper |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Thanks @blink1073 — really appreciate the quick review, and for tidying up the formatting and sorting the ticket on your end. |
PYTHON-5968
Changes in this PR
bson.json_util.loadsnow rejects$timestampvalue documents that contain anything other than the requiredtandicomponents. Previously, unexpected fields could be silently discarded while constructingbson.timestamp.Timestamp.This adds regressions for both additional fields and same-length invalid key sets, and updates the changelog and contributor list.
AI assistance disclosure: I used OpenAI Codex to help investigate the existing behavior, search for duplicates, implement the change, and run validation. I reviewed and understand every changed line and can explain and maintain the contribution.
Test Plan
python3 -m unittest test.test_json_util.TestJsonUtil.test_timestamp test.test_json_util.TestJsonUtil.test_timestamp_with_invalid_fieldspython3 -m unittest test.test_json_util test.test_bson_corpusruff check bson/json_util.py test/test_json_util.pyruff format --check bson/json_util.py test/test_json_util.pypython3 -m compileall -q bson/json_util.py test/test_json_util.pygit diff --checkAll listed checks passed.
Checklist
Checklist for Author
No follow-up work is currently identified. As an external contributor without an existing JIRA ticket, I retained the template
PYTHON-XXXXplaceholder for a MongoDB employee to update.Checklist for Reviewer