Debian Codegraph @

testsuite/sender-remove-source-root-anchor_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """An absolute --relative cleanup must not be re-anchored at the sender's CWD.
3
4 secure_sender_parent_fd() resolved the parent of an absolute --relative name
5 through the cwd-backed dirfd cache. A source that is a direct child of / has
6 an *empty* parent component, so the cache handed back the sender's own working
7 directory and --remove-source-files unlinked a same-named entry there instead
8 of the file that was really transferred -- silently, with exit status 0.
9
10 The sibling test sender-remove-source-relative-anchor covers the nested case,
11 where the same defect merely fails the cleanup. This one covers the case where
12 it destroys an unrelated file.
13
14 Requires root: reaching the empty-parent case needs a source directly under /.
15 """
16
17 import os
18 import subprocess
19 from pathlib import Path
20
21 from rsyncfns import (
22 SCRATCHDIR, makepath, rmtree, rsync_argv, test_fail, test_skipped,
23 )
24
25 if os.geteuid() != 0:
26 test_skipped('needs root to place a transfer source directly under /')
27
28 SOURCE_DATA = b'REAL-SOURCE-CONTENT'
29 DECOY_DATA = b'CWD-DECOY-CONTENT!!'
30 assert len(SOURCE_DATA) == len(DECOY_DATA) # same size: only the name matches
31
32 base = SCRATCHDIR / 'sender-remove-root-anchor'
33 rmtree(base)
34 dest = base / 'dst'
35 makepath(dest)
36
37 # Unique so a stray file in / can never be mistaken for ours.
38 root_src = Path('/') / f'rsync-root-anchor-probe-{os.getpid()}'
39 decoy = base / root_src.name
40
41 try:
42 try:
43 root_src.write_bytes(SOURCE_DATA)
44 except OSError as e:
45 # e.g. macOS's sealed system volume: / is read-only even for root.
46 test_skipped(f'cannot create a transfer source under /: {e}')
47 decoy.write_bytes(DECOY_DATA)
48 # Same size and mtime, so the sender's "has it changed?" guard passes and
49 # the decoy is a credible removal target. Nanosecond precision matters:
50 # the guard also compares sub-second mtime, so a whole-second copy would
51 # make the sender skip the removal for an unrelated reason.
52 st = root_src.stat()
53 os.utime(decoy, ns=(st.st_atime_ns, st.st_mtime_ns))
54
55 proc = subprocess.run(
56 rsync_argv('-aR', '--remove-source-files', str(root_src), f'{dest}/'),
57 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, cwd=base)
58 ctx = f'rc={proc.returncode}, output={proc.stdout!r}'
59
60 # The security assertion first: whatever else happened, the unrelated
61 # same-named file in the sender's CWD must be untouched.
62 if not decoy.exists():
63 test_fail(f'--remove-source-files deleted a same-named file in the '
64 f'sender CWD instead of the transferred source ({ctx})')
65 if decoy.read_bytes() != DECOY_DATA:
66 test_fail(f'the sender CWD file was modified ({ctx})')
67
68 if proc.returncode != 0:
69 test_fail(f'absolute -R --remove-source-files failed ({ctx})')
70 if root_src.exists():
71 test_fail(f'the requested source under / was not removed ({ctx})')
72
73 copies = sorted(p for p in dest.rglob('*') if p.is_file())
74 if len(copies) != 1 or copies[0].read_bytes() != SOURCE_DATA:
75 test_fail(f'destination did not receive the real source: {copies} ({ctx})')
76 finally:
77 if root_src.exists():
78 root_src.unlink()
79
80 print('absolute --relative cleanup stayed anchored at / and spared the CWD')