Skip to content

PYTHON-5968 Reject extra fields in Extended JSON timestamps - #2961

Merged
blink1073 merged 4 commits into
mongodb:mainfrom
winklemad:fix-json-timestamp-extra-fields
Jul 28, 2026
Merged

PYTHON-5968 Reject extra fields in Extended JSON timestamps#2961
blink1073 merged 4 commits into
mongodb:mainfrom
winklemad:fix-json-timestamp-extra-fields

Conversation

@winklemad

@winklemad winklemad commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

PYTHON-5968

Changes in this PR

bson.json_util.loads now rejects $timestamp value documents that contain anything other than the required t and i components. Previously, unexpected fields could be silently discarded while constructing bson.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_fields
  • python3 -m unittest test.test_json_util test.test_bson_corpus
  • ruff check bson/json_util.py test/test_json_util.py
  • ruff format --check bson/json_util.py test/test_json_util.py
  • python3 -m compileall -q bson/json_util.py test/test_json_util.py
  • git diff --check

All listed checks passed.

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add links.

No follow-up work is currently identified. As an external contributor without an existing JIRA ticket, I retained the template PYTHON-XXXX placeholder for a MongoDB employee to update.

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation?
  • Is all relevant documentation updated?

@winklemad
winklemad requested a review from a team as a code owner July 26, 2026 00:34
@winklemad
winklemad requested a review from blink1073 July 26, 2026 00:34
@blink1073
blink1073 requested a review from Copilot July 27, 2026 15:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 $timestamp during JSON parsing.
  • Add regression tests for $timestamp documents 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.

Comment thread bson/json_util.py
@blink1073

Copy link
Copy Markdown
Member

Hi @winklemad, thank you for the fix! I agree with the copilot suggestion above. We should also add tests for the two new guards.

@winklemad

Copy link
Copy Markdown
Contributor Author

Thanks @blink1073 — agreed on both.

For the wrapper: I'll add the same if len(doc) != 1 guard the other _parse_* helpers use, so {"$timestamp": {"t": 1, "i": 2}, "extra": 1} is rejected instead of silently dropping the extra field once object_hook matches on $timestamp. And I'll add a Mapping check on the inner value before set(tsp), so something like {"$timestamp": ["t", "i"]} fails with a clear error instead of the confusing list indices must be integers it hits today.

I'll add regression tests for both new guards next to the existing one. Thanks for the review!

@winklemad

Copy link
Copy Markdown
Contributor Author

Pushed both guards plus the two regression tests in a9d62cc — the outer-wrapper len(doc) != 1 check and the Mapping guard on the value. Ready for another look, thanks!

@Jibola
Jibola marked this pull request as draft July 27, 2026 18:14
@Jibola
Jibola marked this pull request as ready for review July 27, 2026 18:14
@blink1073 blink1073 changed the title PYTHON-XXXX Reject extra fields in Extended JSON timestamps PYTHON-5968 Reject extra fields in Extended JSON timestamps Jul 27, 2026
@blink1073
blink1073 requested a review from Copilot July 27, 2026 20:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@blink1073 blink1073 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@winklemad

Copy link
Copy Markdown
Contributor Author

Thanks @blink1073 — really appreciate the quick review, and for tidying up the formatting and sorting the ticket on your end.

@blink1073
blink1073 merged commit 48238a1 into mongodb:main Jul 28, 2026
87 of 89 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants