From 70485a0ecf6b1ae9520deb4af0490fdb48643836 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 21:05:13 +0100 Subject: Prevent amplification attacks, only send id mis-match errors for sensible packets lengths --- tests/integration/test_udp.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests/integration/test_udp.py') diff --git a/tests/integration/test_udp.py b/tests/integration/test_udp.py index 3afe4ef..b60bd56 100644 --- a/tests/integration/test_udp.py +++ b/tests/integration/test_udp.py @@ -44,6 +44,17 @@ class UdpProtocol(TrackerTestCase): self.assertEqual(action, 3, "action 3 is the error packet") self.assertIn(b"Connection ID missmatch", answer[8:]) + def test_a_wrong_connection_id_is_not_answered_with_more_than_was_sent(self): + """The sender's address is unverified at this point, so the error + must not be a way to throw bytes at somebody else.""" + scrape = struct.pack(">QII", 0, 2, 3) + self.hash_for("udpbadscrape") + answer = self.tracker.udp_raw(scrape) + self.assertIsNotNone(answer, "a whole scrape is long enough to be told") + self.assertLessEqual(len(answer), len(scrape)) + self.assertIsNone(self.tracker.udp_raw(scrape[:16])) + self.assertIsNone(self.tracker.udp_raw(scrape[: len(answer) - 1])) + self.assertIsNotNone(self.tracker.udp_raw(scrape[: len(answer)])) + def test_a_packet_shorter_than_the_header_is_ignored(self): self.assertIsNone(self.tracker.udp_raw(b"\x00" * 15)) -- cgit v1.3