Debian Codegraph @

testsuite/rrsync-no-overwrite-backup-collision_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """rrsync -no-overwrite must not let backup publication overwrite a protected file.
3
4 --ignore-existing protects the live destination, but a backup published onto a
5 name that already exists deletes what is there (backup.c make_backup()), and
6 deleting a file backs it up first (delete.c). So a --delete of an unrelated
7 file can land on a protected name that the source itself also carries.
8 """
9
10 import os
11 import shlex
12 import signal
13 import subprocess
14 import sys
15
16 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
17 signal.signal(signal.SIGUSR2, signal.SIG_IGN)
18 if '--shell' in sys.argv:
19 i = sys.argv.index('--shell') + 2
20 env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])}
21 signal.signal(signal.SIGUSR1, signal.SIG_DFL)
22 signal.signal(signal.SIGUSR2, signal.SIG_DFL)
23 wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT']
24 flags = env.get('RRSYNC_FLAGS', '-wo -no-overwrite -no-lock').split()
25 os.execve(wrapper, [wrapper, *flags, root], env)
26
27 from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg
28
29 base = SCRATCHDIR / 'rrsync-no-overwrite-backup-collision'
30 rmtree(base)
31 src = base / 'src'
32 makepath(src)
33 # In the source too, so --ignore-existing is what is supposed to protect it.
34 (src / 'victim.bak').write_bytes(b'SOURCE')
35
36
37 def plant(root):
38 makepath(root)
39 (root / 'victim').write_bytes(b'OLD-LIVE') # absent from src: --delete removes it
40 (root / 'victim.bak').write_bytes(b'POLICY') # the protected file
41
42
43 root = base / 'root'
44 plant(root)
45
46 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
47 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
48 # has to be wrapped before patched_rrsync() sees it.
49 shim = base / 'rsync-shim'
50 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
51 shim.chmod(0o755)
52 wrapper = patched_rrsync(base, rsync_path=str(shim))
53 rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell'
54 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
55
56 # The stock client sends -b inside the remote short-option bundle, so the
57 # refusal has to come from short_disabled, not the long-option table.
58 argv = rsync_argv('-r', '--delete', '--backup', '--suffix=.bak', '-e', rsh, f'{src}/', 'ignored:')
59 got = subprocess.run(argv, env=env, capture_output=True, text=True, timeout=20)
60
61 state = (root / 'victim.bak').read_bytes() if (root / 'victim.bak').exists() else None
62 assert got.returncode != 0 and 'option -b has been disabled' in got.stderr, (
63 f'rrsync -no-overwrite accepted backup mode: rc={got.returncode}, '
64 f'stdout={got.stdout!r}, stderr={got.stderr!r}')
65 assert state == b'POLICY', (
66 f'backup publication overwrote a protected file: state={state!r}, '
67 f'stderr={got.stderr!r}')
68
69 # An ordinary upload must still work.
70 (src / 'new').write_bytes(b'NEW')
71 safe = subprocess.run(
72 rsync_argv('-rI', '-e', rsh, f'{src}/new', 'ignored:'),
73 env=env, capture_output=True, text=True, timeout=20)
74 assert safe.returncode == 0 and (root / 'new').read_bytes() == b'NEW', (
75 f'ordinary upload failed: rc={safe.returncode}, stderr={safe.stderr!r}')
76
77 # The refusal must be CONDITIONAL on -no-overwrite -- and the identical run
78 # through a wrapper without it also demonstrates that the destructive primitive
79 # is real, which the assertions above cannot show once the refusal aborts the
80 # transfer. --ignore-existing is passed by hand here: it is what -no-overwrite
81 # forces, so this shows the collision defeating that protection rather than the
82 # transfer simply writing its own victim.bak. A fresh root keeps this from
83 # disturbing the checks above.
84 plain_root = base / 'root-plain'
85 rmtree(plain_root)
86 plant(plain_root)
87 plain_env = {**env, 'RRSYNC_ROOT': str(plain_root), 'RRSYNC_FLAGS': '-wo -no-lock'}
88 plain_argv = rsync_argv('-r', '--delete', '--backup', '--suffix=.bak',
89 '--ignore-existing', '-e', rsh, f'{src}/', 'ignored:')
90 plain = subprocess.run(plain_argv, env=plain_env, capture_output=True, text=True, timeout=20)
91 plain_state = (plain_root / 'victim.bak').read_bytes() if (plain_root / 'victim.bak').exists() else None
92 assert plain.returncode == 0, (
93 'without -no-overwrite the same options must be accepted, or the refusal '
94 'above is unconditional and says nothing about the policy: '
95 f'rc={plain.returncode}, stderr={plain.stderr!r}')
96 assert plain_state == b'OLD-LIVE', (
97 'the backup collision did not overwrite the protected file even without '
98 '-no-overwrite, so this test no longer exercises the primitive it claims '
99 f'to: state={plain_state!r}, stderr={plain.stderr!r}')