Skip to content

Commit eb84c8e

Browse files
committed
Improve log message when receiving an invalid or corrupt packet
- The choked wording makes it seems like there is a problem in zeroconf when the usual case was an invalid packet send or corrupt wire data.
1 parent 7df7e4a commit eb84c8e

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

tests/test_protocol.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -822,16 +822,18 @@ def test_dns_compression_invalid_skips_bad_name_compress_in_question():
822822
assert len(parsed.questions) == 4
823823

824824

825-
def test_dns_compression_all_invalid():
825+
def test_dns_compression_all_invalid(caplog):
826826
"""Test our wire parser can skip all invalid data."""
827827
packet = (
828828
b'\x00\x00\x84\x00\x00\x00\x00\x01\x00\x00\x00\x00!roborock-vacuum-s5e_miio416'
829829
b'112328\x00\x00/\x80\x01\x00\x00\x00x\x00\t\xc0P\x00\x05@\x00\x00\x00\x00'
830830
)
831-
parsed = r.DNSIncoming(packet)
831+
parsed = r.DNSIncoming(packet, ("2.4.5.4", 5353))
832832
assert len(parsed.questions) == 0
833833
assert len(parsed.answers) == 0
834834

835+
assert " Unable to parse; skipping record" in caplog.text
836+
835837

836838
def test_invalid_next_name_ignored():
837839
"""Test our wire parser does not throw an an invalid next name.
@@ -918,15 +920,16 @@ def test_dns_compression_points_beyond_packet():
918920
assert len(parsed.answers) == 1
919921

920922

921-
def test_dns_compression_generic_failure():
923+
def test_dns_compression_generic_failure(caplog):
922924
"""Test our wire parser does not loop forever when dns compression is corrupt."""
923925
packet = (
924926
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x06domain\x05local\x00\x00\x01'
925927
b'\x80\x01\x00\x00\x00\x01\x00\x04\xc0\xa8\xd0\x05-\x0c\x00\x01\x80\x01\x00\x00'
926928
b'\x00\x01\x00\x04\xc0\xa8\xd0\x06'
927929
)
928-
parsed = r.DNSIncoming(packet)
930+
parsed = r.DNSIncoming(packet, ("1.2.3.4", 5353))
929931
assert len(parsed.answers) == 1
932+
assert "Received invalid packet from ('1.2.3.4', 5353)" in caplog.text
930933

931934

932935
def test_label_length_attack():

zeroconf/_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def datagram_received(
274274
)
275275
return
276276

277-
msg = DNSIncoming(data, scope, now)
277+
msg = DNSIncoming(data, (addr, port), scope, now)
278278
if msg.valid:
279279
log.debug(
280280
'Received from %r:%r [socket %s]: %r (%d bytes) as [%r]',

zeroconf/_protocol/incoming.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"""
2222

2323
import struct
24-
from typing import Callable, Dict, List, Optional, Set, cast
24+
from typing import Callable, Dict, List, Optional, Set, Tuple, cast
2525

2626
from . import DNSMessage
2727
from .._dns import DNSAddress, DNSHinfo, DNSNsec, DNSPointer, DNSQuestion, DNSRecord, DNSService, DNSText
@@ -67,9 +67,16 @@ class DNSIncoming(DNSMessage, QuietLogger):
6767
'valid',
6868
'now',
6969
'scope_id',
70+
'source',
7071
)
7172

72-
def __init__(self, data: bytes, scope_id: Optional[int] = None, now: Optional[float] = None) -> None:
73+
def __init__(
74+
self,
75+
data: bytes,
76+
source: Optional[Tuple[str, int]] = None,
77+
scope_id: Optional[int] = None,
78+
now: Optional[float] = None,
79+
) -> None:
7380
"""Constructor from string holding bytes of packet"""
7481
super().__init__(0)
7582
self.offset = 0
@@ -86,6 +93,7 @@ def __init__(self, data: bytes, scope_id: Optional[int] = None, now: Optional[fl
8693
self.valid = False
8794
self._read_others = False
8895
self.now = now or current_time_millis()
96+
self.source = source
8997
self.scope_id = scope_id
9098
self._parse_data(self._initial_parse)
9199

@@ -102,7 +110,12 @@ def _parse_data(self, parser_call: Callable) -> None:
102110
try:
103111
parser_call()
104112
except DECODE_EXCEPTIONS:
105-
self.log_exception_warning('Choked at offset %d while unpacking %r', self.offset, self.data)
113+
self.log_exception_warning(
114+
'Received invalid packet from %s at offset %d while unpacking %r',
115+
self.source,
116+
self.offset,
117+
self.data,
118+
)
106119

107120
@property
108121
def answers(self) -> List[DNSRecord]:

0 commit comments

Comments
 (0)