@@ -886,6 +886,89 @@ 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+
941+ def test_record_consuming_more_than_rdlength_dropped_and_resyncs ():
942+ """A record whose decoded fields overrun its rdlength must drop and resync.
943+
944+ The first answer is a HINFO with ``rdlength=2`` and rdata ``\\ x01x`` (one
945+ char string ``"x"``). The second character string's length byte then comes
946+ from the next record's name (``\\ x00``, root domain), so the HINFO would
947+ silently parse as ``cpu="x", os=""`` but leave the offset one byte past
948+ the declared end, smearing the second record's framing. With the per-record
949+ boundary check the bogus HINFO is dropped and the second record decodes.
950+ """
951+ packet = (
952+ b"\x00 \x00 \x84 \x00 \x00 \x00 \x00 \x02 \x00 \x00 \x00 \x00 "
953+ b"\x04 test\x05 local\x00 "
954+ b"\x00 \x0d \x80 \x01 "
955+ b"\x00 \x00 \x11 \x94 "
956+ b"\x00 \x02 "
957+ b"\x01 x"
958+ b"\x00 "
959+ b"\x00 \x0c \x00 \x01 "
960+ b"\x00 \x00 \x11 \x94 "
961+ b"\x00 \x02 "
962+ b"\xc0 \x0c "
963+ )
964+ parsed = r .DNSIncoming (packet )
965+ answers = parsed .answers ()
966+ assert not any (isinstance (a , r .DNSHinfo ) for a in answers )
967+ ptrs = [a for a in answers if isinstance (a , r .DNSPointer )]
968+ assert len (ptrs ) == 1
969+ assert ptrs [0 ].alias == "test.local."
970+
971+
889972def test_records_same_packet_share_fate ():
890973 """Test records in the same packet all have the same created time."""
891974 out = r .DNSOutgoing (const ._FLAGS_QR_QUERY | const ._FLAGS_AA )
0 commit comments