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).
109 lines
3.4 KiB
Python
109 lines
3.4 KiB
Python
#!/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()
|