From 595bfd57dc8179f9588e2a8d1e0efd1194e6246c Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sat, 28 Nov 2026 16:28:49 +0100 Subject: Ensure one byte in the reply buffer for the terminator --- src/ot_http.c | 3 ++- tests/integration/test_announce.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/ot_http.c b/src/ot_http.c index 20ec1a6..406fec3 100644 --- a/src/ot_http.c +++ b/src/ot_http.c @@ -690,7 +690,8 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct case 7: /* matched "ip" */ { char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; - len = urlencoded_parse(&read_ptr, tmp_buf2, ws->outbuf + G_OUTBUF_SIZE, SCAN_SEARCHPATH_VALUE); + /* One short of the end, for the terminator below */ + len = urlencoded_parse(&read_ptr, tmp_buf2, ws->outbuf + G_OUTBUF_SIZE - 1, SCAN_SEARCHPATH_VALUE); if (len <= 0) HTTPERROR_400_PARAM; tmp_buf2[len] = 0; diff --git a/tests/integration/test_announce.py b/tests/integration/test_announce.py index 0eee0fe..2d21417 100644 --- a/tests/integration/test_announce.py +++ b/tests/integration/test_announce.py @@ -124,6 +124,18 @@ class AnnounceRejects(TrackerTestCase): def test_a_port_that_is_not_a_number_is_rejected(self): self.assert_rejected("info_hash=" + "a" * 20 + "&peer_id=" + "b" * 20 + "&port=abc&left=0") + def test_an_ip_that_fills_the_reply_buffer_is_refused(self): + """The value is decoded into the reply buffer and then terminated, so + the longest one that fits has to leave room for the terminator. 8096 + bytes is what lies between where it is decoded and the buffer's end.""" + connection = self.tracker.connect() + try: + connection.sock.sendall(b"GET /a?ip=" + b"1" * 8096 + b"\n\n") + status, _, _ = connection.reply() + self.assertTrue(status.startswith(b"HTTP/1.0 400"), status) + finally: + connection.close() + def test_two_info_hashes_are_rejected(self): self.assert_rejected("info_hash=" + "a" * 20 + "&info_hash=" + "c" * 20 + "&peer_id=" + "b" * 20 + "&port=6881&left=0") -- cgit v1.3