Skip to content

PYTHON-4232 Add Fuzzing Tests - #3128

Open
blink1073 wants to merge 10 commits into
mongodb:mainfrom
blink1073:PYTHON-4232
Open

blink1073 wants to merge 10 commits into
mongodb:mainfrom
blink1073:PYTHON-4232

Conversation

@blink1073

@blink1073 blink1073 commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

PYTHON-4232

Changes in this PR

Adds coverage-guided fuzz tests for bson: each target checks that the C extension and pure-Python implementations agree on arbitrary input, with committed reproducers for known bugs. Runs on every PR plus a daily Evergreen task; no driver source is changed.

Test Plan

  • just lint, just lint-manual, and just typing pass.
  • Both Evergreen runner paths verified locally; a deliberately broken target fails as expected.
  • An Evergreen patch build will confirm real atheris execution (not runnable on local hardware).

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)? Internal test infrastructure only, no user-facing change.
  • Is there test coverage? This PR adds the fuzz tests themselves and changes no driver source.
  • Is any followup work tracked in a JIRA ticket? If so, add link(s). (PYTHON-6169, PYTHON-6178)

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Adds atheris-based fuzz targets for bson that run the C extension and
pure-Python implementations on arbitrary input and compare their
results.  Known-bug reproducers in fuzz/regression/ run on every PR;
a nightly Evergreen task runs time-boxed discovery fuzzing against a
seed corpus built at setup time from the vendored BSON spec corpus.
No driver source is changed.

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.

🟡 Changes recommended

The targets lack BSON parser coverage feedback, miss decoded-value differences, and skip a committed regression input.

7 open findings
What changed in this PR

This PR adds BSON fuzz testing to the driver’s test infrastructure without changing driver source code.

Changes:

  • Adds three C-versus-pure-Python BSON fuzz targets, a seed corpus builder, and regression inputs.
  • Runs regression inputs on PRs and time-boxed fuzzing daily through Evergreen.
  • Documents local use and adds the Atheris dependency.
File Description
uv.lock Locks Atheris for fuzz testing.
pyproject.toml Adds the fuzz dependency group and lint exceptions.
fuzz/​README.md Documents targets, inputs, and commands.
fuzz/​fuzz_is_valid.py Tests BSON validity across implementations.
fuzz/​fuzz_decode.py Tests single-document decoding and re-encoding.
fuzz/​fuzz_decode_all.py Tests multi-document decoding and re-encoding.
fuzz/​build_corpus.py Builds BSON seed inputs.
CONTRIBUTING.md Adds contributor instructions for fuzz tests.
AGENTS.md Describes the new fuzz directory.
.gitignore Ignores generated fuzz scratch files.
.evergreen/​scripts/​utils.py Registers the fuzz test category.
.evergreen/​scripts/​setup_tests.py Prepares the fuzz corpus and environment.
.evergreen/​scripts/​run_tests.py Runs regression and timed fuzz targets.
.evergreen/​scripts/​generate_config.py Defines Evergreen fuzz tasks and variants.
.evergreen/​generated_configs/​variants.yml Adds fuzz build variants.
.evergreen/​generated_configs/​tasks.yml Adds fuzz tasks.
.evergreen/​generated_configs/​functions.yml Passes the fuzz time setting.

🧠 Review effort: Lite


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread .evergreen/scripts/run_tests.py
Comment thread fuzz/README.md
Comment thread fuzz/fuzz_decode.py Outdated
Comment thread fuzz/fuzz_decode.py Outdated
Comment thread fuzz/fuzz_decode.py Outdated
Comment thread fuzz/fuzz_decode_all.py
Comment thread fuzz/fuzz_decode_all.py Outdated

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.

🟡 Changes recommended

The documented fuzz group cannot install on several supported environments, and the seed builder omits valid raw BSON variants.

3 open findings
5 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Include all raw BSON variants as fuzzing seeds

fuzz/​build_corpus.py:86

The builder only adds canonical_bson bytes, although valid spec cases also include degenerate_bson (for example, test/bson_corpus/array.json:18) and converted_bson (test/bson_corpus/undefined.json:11). Encoding their Extended JSON does not retain these distinct wire representations, so nightly fuzzing misses those cases. Add all available raw BSON variants as separate seeds.

🧠 Review effort: Lite

Comment thread pyproject.toml
@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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.

🔵 Needs a closer look

Unresolved moderate findings affect fuzz coverage, artifact retention, resource cleanup, and exception handling.

3 open findings
Previously missed (1)

In code that hasn't changed since last review

Medium severity Do not swallow decode exceptions in is_valid

fuzz/​fuzz_is_valid.py:70

is_valid catches every exception and returns False, so a True result guarantees that decode will not raise any exception, not just InvalidBSON. Swallowing ValueError, struct.error, and the other exceptions here means this target will miss a regression where is_valid() returns True but decode() fails with one of them. Let any exception from decode fail the fuzz case (while retaining the diagnostic).

This issue also appears on line 82 of the same file.

🧠 Review effort: Lite

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.

🟡 Changes recommended

The fuzz runner must invoke targets with the intended uv interpreter.

4 open findings

🧠 Review effort: Lite

Comment thread .evergreen/scripts/run_tests.py Outdated

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.

🔵 Needs a closer look

Evergreen fuzz failures do not currently archive fuzz-scratch/ reproducers.

0 open findings

4 resolved since last review

🧠 Review effort: Lite

@blink1073
blink1073 marked this pull request as ready for review October 15, 2026 20:51
@blink1073
blink1073 requested a review from a team as a code owner October 15, 2026 20:51
@blink1073
blink1073 requested a review from NoahStapp October 15, 2026 20:51

This branch has not been deployed

No deployments
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.

2 participants