fix: bound record payload reads against rdlength overrun#1756
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1756 +/- ##
=======================================
Coverage 99.77% 99.77%
=======================================
Files 33 33
Lines 3497 3508 +11
Branches 489 493 +4
=======================================
+ Hits 3489 3500 +11
Misses 5 5
Partials 3 3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@bluetoothbot review |
There was a problem hiding this comment.
Pull request overview
This PR hardens DNSIncoming record parsing to avoid silently accepting truncated/over-advertised RDATA payloads by adding explicit bounds checks in _read_string / _read_character_string, and adds regression tests for TXT, HINFO, and A record truncation cases.
Changes:
- Add packet-end bounds checks to
_read_string()and_read_character_string()to raiseIncomingDecodeErroron overrun. - Add regression tests ensuring malformed TXT/HINFO/A records do not surface in parsed answers.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/zeroconf/_protocol/incoming.py |
Adds bounds checks to prevent slice-truncation from producing corrupted RDATA. |
tests/test_protocol.py |
Adds tests for malformed/truncated TXT, HINFO, and A records. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Good catch, addressed in c6111f9; added a per-record post-check in |
PR Review — fix: bound record payload reads against rdlength overrunSolid fix. The two-layer approach — packet-level bounds in 🟢 Suggestions1. Test docstrings retell production-side rationale (`tests/test_protocol.py`, L895-901)Per CLAUDE.md: "Test docstrings retelling the production-side story... A test docstring should name what the test pins, in one sentence — not re-explain the bug, the fix, or the surrounding flow."
Suggested form: keep just the first sentence ( Checklist
SummarySolid fix. The two-layer approach — packet-level bounds in |
Summary
The TXT, HINFO, and A payload readers trusted the declared rdlength without checking it against the buffer; an attacker on the LAN could send a record with rdlength=65535 and only a few real bytes of payload, and we would silently truncate the slice and stuff the corrupt record into the cache. Adds the bounds check
_read_stringand_read_character_stringshould have had all along; the existing_read_otherscatch then skips the record, matching how malformed NSEC bitmaps already get rejected.Details
offset,length, and_data_lenare alreadyunsigned intinincoming.pxd; I checked the generated.c, the hot path staysscore-0in thecython -aannotation._read_string/_read_character_stringskip remaining-byte validation, enabling silent record-data corruption #1752.Test plan
poetry run pytest tests/(384 passed, 3 skipped)poetry run pre-commit run --files src/zeroconf/_protocol/incoming.py tests/test_protocol.pytests/test_protocol.pycover TXT, HINFO, and A record overrun.