Debian Codegraph @

testsuite/proxy-response-line-too-long_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Python rewrite of testsuite/proxy-response-line-too-long.test.
3 #
4 # Regression test for the off-by-one stack OOB write in
5 # establish_proxy_connection() when a malicious or man-in-the-middle
6 # HTTP proxy returned a first response line of 1023+ bytes without a
7 # '\n' terminator. Post-fix, rsync must reject this with "proxy
8 # response line too long" and exit non-zero without dying from a signal.
9
10 import os
11 import shutil
12 import socket
13 import subprocess
14 import sys
15 import threading
16 import time
17
18 from rsyncfns import (
19 SCRATCHDIR, claim_ports, require_tcp, rsync_argv, test_fail, test_skipped,
20 )
21
22
23 # This test has no stdio-pipe equivalent: it binds a real loopback socket to
24 # stand in for a malicious HTTP proxy. Honour the secure default (no listening
25 # sockets) by only running it under --use-tcp.
26 require_tcp("fake-proxy listener needs a real TCP socket; run with --use-tcp")
27
28 if shutil.which('python3') is None:
29 test_skipped("python3 not available")
30
31 workdir = SCRATCHDIR / 'workdir'
32 workdir.mkdir(parents=True, exist_ok=True)
33 os.chdir(workdir)
34
35 # Reserve our proxy port across any concurrent test processes; another
36 # test asking for the same port blocks here until we exit.
37 PROXY_PORT = 12873
38 claim_ports(PROXY_PORT)
39
40 # In-process listener: bind to the claimed port, accept one client, read
41 # up to end-of-headers (or 64 KiB), reply with exactly 1023 'X' bytes and
42 # no '\n', then close. A thread is simpler than spawning python3 again
43 # and has the same effect on the rsync side.
44 listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
45 listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
46 listener.bind(('127.0.0.1', PROXY_PORT))
47 port = PROXY_PORT
48 listener.listen(1)
49
50
51 def _serve_one():
52 conn, _ = listener.accept()
53 conn.settimeout(5)
54 try:
55 data = b""
56 while b"\r\n\r\n" not in data and len(data) < 65536:
57 chunk = conn.recv(8192)
58 if not chunk:
59 break
60 data += chunk
61 except socket.timeout:
62 pass
63 conn.sendall(b"X" * 1023)
64 try:
65 conn.shutdown(socket.SHUT_RDWR)
66 except OSError:
67 pass
68 conn.close()
69
70
71 t = threading.Thread(target=_serve_one)
72 t.daemon = True
73 t.start()
74
75 # Run rsync against the malicious proxy. The proxy intercepts CONNECT
76 # and never forwards, so the upstream URL is irrelevant.
77 env = os.environ.copy()
78 env['RSYNC_PROXY'] = f'127.0.0.1:{port}'
79 proc = subprocess.run(
80 rsync_argv('rsync://example.invalid:873/whatever/', f'{workdir}/out/'),
81 capture_output=True, text=True, env=env,
82 )
83
84 t.join(timeout=15)
85 listener.close()
86
87 status = proc.returncode
88 err = proc.stderr
89
90 if status >= 128:
91 sys.stderr.write(err)
92 test_fail(f"rsync killed by signal (status={status}) -- possible stack OOB regression")
93
94 if status == 0:
95 sys.stderr.write(err)
96 test_fail("rsync returned success despite malformed proxy response")
97
98 if 'proxy response line too long' not in err:
99 sys.stderr.write(err)
100 test_fail("expected 'proxy response line too long' in rsync stderr")
101
102 print("OK: over-long proxy response line rejected cleanly without crashing")