Debian Codegraph @

testsuite/malicious-dot-dir-delete-scope_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A malicious pull server must not enlarge --delete to the transfer root.
3
4 For a scoped single-file pull, the synthetic ``.`` entry is not part of the
5 requested source. The receiver nevertheless exempts it from its requested-name
6 filter. A hostile sender can encode it as a top-level content directory and
7 make the generator run delete_in_dir("."), sweeping receiver-owned siblings.
8 """
9
10 import socket
11 import subprocess
12 import threading
13
14 from rsyncfns import (
15 SCRATCHDIR, claim_ports, makepath, require_tcp, rmtree, rsync_argv,
16 test_fail,
17 )
18 import rsync_proto as rp
19
20 PORT = 13014
21
22 require_tcp("the malicious sender needs a real TCP socket; run with --use-tcp")
23 claim_ports(PORT)
24
25 base = SCRATCHDIR / 'malicious-dot-dir-delete-scope'
26 rmtree(base)
27 dest = base / 'dest'
28 makepath(dest / 'private')
29 sentinel = dest / 'private' / 'must-survive'
30 sentinel.write_text('receiver-owned file outside the requested leaf\n')
31
32 # Control the intended scope: an honest single-file source with identical
33 # receiver options does not make --delete recurse over destination siblings.
34 honest_src = base / 'honest-src'
35 makepath(honest_src)
36 (honest_src / 'requested').write_text('honest source\n')
37 control = subprocess.run(
38 rsync_argv('-r', '--delete', '--no-inc-recursive',
39 str(honest_src / 'requested'), str(dest) + '/'),
40 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30)
41 if control.returncode or not sentinel.exists():
42 test_fail('native --delete single-file control unexpectedly swept the '
43 f'destination root:\n{control.stdout}{control.stderr}')
44
45 lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46 lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
47 lsock.bind(('127.0.0.1', PORT))
48 lsock.listen(1)
49 lsock.settimeout(30)
50 state = {}
51
52
53 def serve():
54 try:
55 csock, _ = lsock.accept()
56 s = rp.DaemonReceiver(csock)
57 s.handshake()
58
59 # XMIT_TOP_DIR without XMIT_NO_CONTENT_DIR maps the synthetic dot to
60 # FLAG_CONTENT_DIR. "requested" is the only name the client asked for.
61 flist = (
62 rp.FileEntry('.', mode=rp.S_IFDIR | 0o755,
63 extra_flags=rp.XMIT_TOP_DIR).encode()
64 + rp.FileEntry('requested', mode=rp.S_IFREG | 0o644,
65 length=0).encode()
66 + rp.end_of_flist(0, s.protocol)
67 )
68 # No file bytes are needed: delete_in_dir(".") runs in the generator
69 # before the transfer response is consumed.
70 s.send_data(flist + s.w_ndx(rp.NDX_DONE) * 3)
71 s.drain(timeout=5)
72 s.close()
73 except Exception as exc: # noqa: BLE001 - reported below
74 state['err'] = repr(exc)
75
76
77 t = threading.Thread(target=serve, daemon=True)
78 t.start()
79 try:
80 proc = subprocess.run(
81 rsync_argv('-r', '--delete', '--no-inc-recursive',
82 f'rsync://127.0.0.1:{PORT}/mod/requested', str(dest) + '/'),
83 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, timeout=30)
84 finally:
85 t.join(timeout=10)
86 lsock.close()
87
88 if not sentinel.exists():
89 test_fail(
90 'malicious sender marked the exempt synthetic dot as a content '
91 'directory and expanded a scoped single-file --delete to the entire '
92 'destination root')
93 if state.get('err'):
94 test_fail(f"Python sender failed before exercising receiver: {state['err']}\n"
95 f"receiver output:\n{proc.stdout}{proc.stderr}")
96
97 print('malicious-dot-dir-delete-scope: receiver did not let a forged dot '
98 'content directory expand --delete beyond the requested leaf')