Debian Codegraph @

testsuite/rrsync-no-overwrite-logfile_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """rrsync -no-overwrite must not let the peer append to an existing file."""
3
4 import os
5 import shlex
6 import signal
7 import subprocess
8 import sys
9
10 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
11 signal.signal(signal.SIGUSR2, signal.SIG_IGN)
12 if '--shell' in sys.argv:
13 i = sys.argv.index('--shell') + 2
14 env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])}
15 signal.signal(signal.SIGUSR1, signal.SIG_DFL)
16 signal.signal(signal.SIGUSR2, signal.SIG_DFL)
17 wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT']
18 flags = env.get('RRSYNC_FLAGS', '-wo -no-overwrite -no-lock').split()
19 os.execve(wrapper, [wrapper, *flags, root], env)
20
21 from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg
22
23 base = SCRATCHDIR / 'rrsync-no-overwrite-logfile'
24 rmtree(base)
25 src = base / 'src'
26 root = base / 'root'
27 makepath(src, root)
28 (src / 'new').write_bytes(b'NEW')
29 protected = root / 'protected'
30 protected.write_bytes(b'POLICY\n')
31
32 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
33 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
34 # has to be wrapped before patched_rrsync() sees it.
35 shim = base / 'rsync-shim'
36 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
37 shim.chmod(0o755)
38 wrapper = patched_rrsync(base, rsync_path=str(shim))
39 rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell'
40 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
41 got = subprocess.run(
42 rsync_argv('-rI', '-M--log-file=protected',
43 '-M--log-format=ATTACKER-CONTROLLED',
44 '-e', rsh, f'{src}/new', 'ignored:'),
45 env=env, capture_output=True, text=True, timeout=20)
46
47 assert got.returncode != 0 and 'option --log-file has been disabled' in got.stderr, (
48 f'rrsync -no-overwrite accepted --log-file: rc={got.returncode}, '
49 f'stdout={got.stdout!r}, stderr={got.stderr!r}')
50 assert protected.read_bytes() == b'POLICY\n'
51
52 safe = subprocess.run(
53 rsync_argv('-rI', '-e', rsh, f'{src}/new', 'ignored:'),
54 env=env, capture_output=True, text=True, timeout=20)
55 assert safe.returncode == 0 and (root / 'new').read_bytes() == b'NEW', (
56 f'ordinary upload failed: rc={safe.returncode}, stderr={safe.stderr!r}')
57
58 # The refusal must be CONDITIONAL on -no-overwrite. Run the identical option
59 # through a wrapper without it: if that is refused too, the option is simply
60 # disabled for every rrsync deployment and the assertion above proves nothing
61 # about the policy. A fresh root keeps this from disturbing the checks above.
62 plain_root = base / 'root-plain'
63 rmtree(plain_root)
64 makepath(plain_root)
65 (plain_root / 'protected').write_bytes(b'POLICY\n')
66 plain_env = {**env, 'RRSYNC_ROOT': str(plain_root), 'RRSYNC_FLAGS': '-wo -no-lock'}
67 plain = subprocess.run(
68 rsync_argv('-rI', '-M--log-file=protected', '-M--log-format=X', '-e', rsh, f'{src}/new', 'ignored:'),
69 env=plain_env, capture_output=True, text=True, timeout=20)
70 assert plain.returncode == 0, (
71 'without -no-overwrite the same option must be accepted, or the refusal '
72 'above is unconditional and says nothing about the policy: '
73 f'rc={plain.returncode}, stderr={plain.stderr!r}')