From d81ac8801fbdce4261d45d04441540c388de89b4 Mon Sep 17 00:00:00 2001 From: Hermes Date: Thu, 19 Nov 2026 07:27:04 +0000 Subject: [PATCH] tests: malformed requests, and the port-theft that broke two runs A new probe sends twelve malformed requests (negative and overflowing Content-Length, a 64-bit chunk size, TE and CL together, a bad chunk, a NUL in the path, a 4 KB path, 48 KB of headers, TRACE, a missing version) and requires the router to answer /status after each one. It does; the probe is part of the suite now. The two mysterious bind failures (8380 in run 10, 8154 in run 15) had a host cause: net.ipv4.ip_local_port_range is 1024-65535 here, so outgoing connections were handed the test's own ports and bind() failed with EADDRINUSE -- the suite then blamed the router. The ranges are now in net.ipv4.ip_local_reserved_ports (/etc/sysctl.d/90-junction-test-ports.conf), and run.sh warns when a host has the wide range without the reservation. Full suite: 538 OK, 0 FAIL, PASS (was 537; the probe is the new check). --- tests/malformed.py | 108 +++++++++++++++++++++++++++++++++++++++++++++ tests/run.sh | 27 +++++++++++- 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 tests/malformed.py diff --git a/tests/malformed.py b/tests/malformed.py new file mode 100644 index 0000000..000ace1 --- /dev/null +++ b/tests/malformed.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python3 +"""Malformed requests against junction, and the router must survive them. + +Each payload is sent on a fresh connection; the answer is read with a short +timeout. After every payload the router must still answer GET /status. What is +checked is survival, not a particular status: a parser must not die, hang or +(under ASAN) read out of bounds on garbage a peer can send. +""" +import os +import socket +import subprocess +import sys +import tempfile +import time + +JUNCTION = os.environ.get("JUNCTION_BIN", "./junction") +PORT = 18340 + +PAYLOADS = { + "baseline": b"GET /status HTTP/1.1\r\nHost: x\r\n\r\n", + "cl-negative": b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Length: -1\r\n\r\n", + "cl-overflow": b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nContent-Length: 99999999999999999999\r\n\r\n", + "chunk-huge": b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\nffffffffffffffff\r\n", + "te-and-cl": b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\nContent-Length: 5\r\n\r\n0\r\n\r\n", + "bad-chunk": b"POST /v1/chat/completions HTTP/1.1\r\nHost: x\r\nTransfer-Encoding: chunked\r\n\r\nzz\r\n", + "nul-path": b"GET /sta\x00tus HTTP/1.1\r\nHost: x\r\n\r\n", + "long-path": b"GET /" + b"a" * 4000 + b" HTTP/1.1\r\nHost: x\r\n\r\n", + "long-headers": b"GET /status HTTP/1.1\r\nHost: x\r\n" + b"X-a: b\r\n" * 6000 + b"\r\n", + "trace": b"TRACE / HTTP/1.1\r\nHost: x\r\n\r\n", + "no-version": b"GET /status\r\n\r\n", + "space-method": b"G ET / HTTP/1.1\r\nHost: x\r\n\r\n", +} + + +def send(data, timeout=4.0): + s = socket.create_connection(("127.0.0.1", PORT), timeout=timeout) + s.settimeout(timeout) + try: + s.sendall(data) + s.shutdown(socket.SHUT_WR) + except OSError: + pass + out = b"" + try: + while len(out) < 4096: + chunk = s.recv(4096) + if not chunk: + break + out += chunk + except (socket.timeout, OSError): + pass + s.close() + return out + + +def alive(): + try: + return send(b"GET /status HTTP/1.1\r\nHost: x\r\n\r\n", 3.0).startswith( + b"HTTP/1.1 200") + except OSError: + return False + + +def main(): + tmp = tempfile.mkdtemp(prefix="jmalformed-") + conf = os.path.join(tmp, "j.conf") + with open(conf, "w") as f: + f.write(f"""listen 127.0.0.1:{PORT} +probe_interval 0 +endpoint a {{ base http://127.0.0.1:9/v1 }} +req for "*" pass a +""") + proc = subprocess.Popen([JUNCTION, "--config", conf], + stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) + for _ in range(100): + try: + socket.create_connection(("127.0.0.1", PORT), 0.2).close() + break + except OSError: + time.sleep(0.05) + + dead = [] + for name, data in PAYLOADS.items(): + try: + send(data) + except OSError: + pass + if not alive(): + dead.append(name) + break + + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + subprocess.run(["rm", "-rf", tmp]) + + if dead: + print(f"FAIL: the router did not survive {dead[0]}") + err = proc.stderr.read(2000) if proc.poll() is not None else b"" + print(err.decode("latin-1")[-1200:]) + sys.exit(1) + print(f"PASS: {len(PAYLOADS)} malformed requests, the router stayed alive") + + +if __name__ == "__main__": + main() diff --git a/tests/run.sh b/tests/run.sh index db959c8..c2e3cba 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -66,6 +66,19 @@ wait_log() { return 1 } +# Hosts with a wide ephemeral range (1024-65535, common after PVE tuning) hand +# out ports such as 8380 or 8154 to outgoing connections. The bind then fails +# with EADDRINUSE and the suite blames the router; reserving the test ranges in +# net.ipv4.ip_local_reserved_ports fixes it (see the note in tests/run.sh and +# /etc/sysctl.d/90-junction-test-ports.conf on hosts that run this suite). +case "$(cat /proc/sys/net/ipv4/ip_local_port_range 2>/dev/null)" in + "1024"*) + case "$(cat /proc/sys/net/ipv4/ip_local_reserved_ports 2>/dev/null)" in + *8135-8440*) ;; + *) echo "warning: ephemeral ports may take a test port (EADDRINUSE); reserve 8135-8440,9443,9990-9998,18000-18399" >&2 ;; + esac ;; +esac + # Ein Port, der gerade freigegeben wurde, ist nicht sofort wieder belegbar: nach SIGTERM # hält junction den Listener noch restart_grace_s offen und antwortet "restarting". # Wer den Port wiederverwenden will, wartet, bis er wirklich frei ist. @@ -97,7 +110,7 @@ log_has() { # answer requests with somebody else's config), so fail loudly and early. # 8157 gehört auf dieser Maschine dem Hermes-Gateway — der Port wird von keinem # Test benutzt, also darf er die Suite nicht aufhalten. -for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8204 8205 8206 8207 8208 8209 8210 9443 9990 9991 9992 9993 9994 9995 9996 9997 9998 8215 8216; do +for port in 8361 8362 8363 8364 8357 8358 8359 8360 8355 8356 8347 8348 8349 8350 8351 8352 8353 8354 8345 8346 8335 8336 8337 8338 8339 8340 8341 8342 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8395 8396 8397 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8150 8151 8152 8153 8154 8155 8156 8158 8159 8160 8161 8162 8163 8164 8165 8166 8365 8366 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8167 8168 8170 8171 8172 8173 8175 8180 8181 8194 8176 8177 8185 8186 8187 8188 8189 8190 8204 8205 8206 8207 8208 8209 8210 9443 9990 9991 9992 9993 9994 9995 9996 9997 9998 8215 8216 18340; do if ss -tln 2>/dev/null | grep -q ":$port "; then echo "port $port is busy — a leftover junction/stub is still running:" ss -tlnp 2>/dev/null | grep ":$port " | sed 's/^/ /' @@ -950,6 +963,18 @@ else grep -vE '^\[jtest\]' "$TMP/mcp_conc.log" | sed 's/^/ /' | tail -14 fi +# Fehlerhafte Anfragen: ein Parser darf an Garbage, die ein Peer schicken +# kann, weder sterben noch hängen bleiben. +echo +echo "== Fehlerhafte Anfragen ==" +if JUNCTION_BIN="$PWD/junction" python3 "$(dirname "$0")/malformed.py" \ + > "$TMP/malformed.log" 2>&1; then + ok "$(tail -1 "$TMP/malformed.log")" +else + fail "malformed-request probe failed:" + sed 's/^/ /' "$TMP/malformed.log" | tail -12 +fi + # Steuerkanal: Regeln im Betrieb setzen und löschen, Endpunkte pausieren, # Konfiguration schreiben — jeweils mit Prüfung, dass der Router danach anders handelt. echo