| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 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 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 36 |
|
| 37 |
PROXY_PORT = 12873 |
| 38 |
claim_ports(PROXY_PORT) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 76 |
|
| 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") |