@@ -886,6 +886,58 @@ def test_nsec_bitmap_truncated_window_header_rejected():
886886 assert not any (isinstance (a , r .DNSNsec ) for a in answers )
887887
888888
889+ def test_txt_rdlength_overruns_packet_rejected ():
890+ """A TXT record with rdlength past the buffer must not enter the cache.
891+
892+ Python slicing silently truncates when the slice end exceeds the buffer,
893+ so without a bounds check in ``_read_string`` a malformed wire frame
894+ would land in the cache carrying a payload shorter than the rdlength
895+ declared, leaving the parser desynchronized for downstream records.
896+ """
897+ packet = (
898+ b"\x00 \x00 \x84 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 "
899+ b"\x04 test\x05 local\x00 "
900+ b"\x00 \x10 \x80 \x01 "
901+ b"\x00 \x00 \x11 \x94 "
902+ b"\xff \xff "
903+ b"\x05 hello"
904+ )
905+ parsed = r .DNSIncoming (packet )
906+ assert parsed .valid
907+ assert parsed .answers () == []
908+
909+
910+ def test_hinfo_character_string_length_overruns_record_rejected ():
911+ """A HINFO character string declaring more bytes than remain must be rejected."""
912+ packet = (
913+ b"\x00 \x00 \x84 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 "
914+ b"\x04 test\x05 local\x00 "
915+ b"\x00 \x0d \x80 \x01 "
916+ b"\x00 \x00 \x11 \x94 "
917+ b"\x00 \x07 "
918+ b"\x03 cpu"
919+ b"\xff \xff \xff "
920+ )
921+ parsed = r .DNSIncoming (packet )
922+ assert parsed .valid
923+ assert not any (isinstance (a , r .DNSHinfo ) for a in parsed .answers ())
924+
925+
926+ def test_a_record_rdlength_overruns_packet_rejected ():
927+ """An A record whose 4-byte address would walk past the buffer must be rejected."""
928+ packet = (
929+ b"\x00 \x00 \x84 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \x00 "
930+ b"\x04 test\x05 local\x00 "
931+ b"\x00 \x01 \x80 \x01 "
932+ b"\x00 \x00 \x11 \x94 "
933+ b"\x00 \x04 "
934+ b"\xc0 \xa8 "
935+ )
936+ parsed = r .DNSIncoming (packet )
937+ assert parsed .valid
938+ assert not any (isinstance (a , r .DNSAddress ) for a in parsed .answers ())
939+
940+
889941def test_records_same_packet_share_fate ():
890942 """Test records in the same packet all have the same created time."""
891943 out = r .DNSOutgoing (const ._FLAGS_QR_QUERY | const ._FLAGS_AA )
0 commit comments