Debian Codegraph @

testsuite/proxy-protocol-trusted-peer_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 import socket
3
4 from rsyncfns import (
5 SCRATCHDIR, claim_ports, require_tcp, start_rsyncd, test_fail,
6 )
7
8 PORT = 12933
9 require_tcp("PROXY protocol peer policy needs a real TCP daemon; run with --use-tcp")
10 claim_ports(PORT)
11
12 mod = SCRATCHDIR / 'proxy-proto-mod'
13 mod.mkdir(parents=True, exist_ok=True)
14 conf = SCRATCHDIR / 'proxy-proto.conf'
15 log = SCRATCHDIR / 'proxy-proto.log'
16 conf.write_text(f"""\
17 pid file = {SCRATCHDIR}/rsyncd.pid
18 proxy protocol = true
19 use chroot = no
20 log file = {log}
21
22 [mod]
23 path = {mod}
24 read only = yes
25 """)
26 start_rsyncd(conf, PORT)
27
28 with socket.create_connection(('127.0.0.1', PORT), timeout=5) as s:
29 s.sendall(b"PROXY TCP4 10.9.8.7 127.0.0.1 12345 873\r\n")
30 try:
31 data = s.recv(1024)
32 except ConnectionResetError:
33 data = b''
34
35 if b'@RSYNCD' in data:
36 test_fail("daemon trusted a PROXY header from an unlisted direct peer")
37 log_text = log.read_text(errors='replace')
38 if 'proxy protocol rejected' not in log_text:
39 test_fail("daemon did not log rejection of untrusted PROXY protocol peer")
40 # This config sets "proxy protocol = true" with no "proxy protocol hosts", which
41 # fail-closes (rejects every peer); the daemon must warn about that at startup.
42 if 'proxy protocol hosts" is unset' not in log_text:
43 test_fail("daemon did not warn at startup that proxy-protocol with no "
44 "trusted-proxy hosts rejects all connections")
45
46 print("proxy-protocol-trusted-peer: untrusted direct peer cannot spoof PROXY source IP")