Debian Codegraph @

testsuite/rrsync-write-only-files-from_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A forced rrsync -wo endpoint must not return a local --files-from 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 os.execve(wrapper, [wrapper, '-wo', '-no-lock', root], env)
19
20 from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg
21
22 base = SCRATCHDIR / 'rrsync-write-only-files-from'
23 rmtree(base)
24 src = base / 'src'
25 root = base / 'root'
26 makepath(src, root)
27 secret_name = 'SERVER-ONLY-SECRET-NAME'
28 (root / 'protected-list').write_text(secret_name + '\n')
29
30 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
31 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
32 # has to be wrapped before patched_rrsync() sees it.
33 shim = base / 'rsync-shim'
34 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
35 shim.chmod(0o755)
36 wrapper = patched_rrsync(base, rsync_path=str(shim))
37 rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell'
38 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
39 got = subprocess.run(
40 rsync_argv('-r', '--files-from=:protected-list',
41 '-e', rsh, f'{src}/', 'ignored:'),
42 env=env, capture_output=True, text=True, timeout=20)
43
44 assert got.returncode != 0 and 'write-only server cannot read' in got.stderr, (
45 f'write-only rrsync accepted remote --files-from: rc={got.returncode}, '
46 f'stdout={got.stdout!r}, stderr={got.stderr!r}')
47 assert secret_name not in got.stdout and secret_name not in got.stderr, (
48 f'write-only rrsync returned the protected record: rc={got.returncode}, '
49 f'stdout={got.stdout!r}, stderr={got.stderr!r}')
50
51 # Control: a CLIENT-local --files-from upload must still work. A second,
52 # unlisted source file makes this prove list selection as well: with only one
53 # file present an ordinary recursive upload would look identical, so the
54 # control would pass even if the list were ignored entirely.
55 (src / 'allowed').write_bytes(b'NEW')
56 (src / 'unlisted').write_bytes(b'NOPE')
57 local_list = base / 'local-list'
58 local_list.write_text('allowed\n')
59 safe = subprocess.run(
60 rsync_argv('-r', f'--files-from={local_list}',
61 '-e', rsh, f'{src}/', 'ignored:'),
62 env=env, capture_output=True, text=True, timeout=20)
63 assert safe.returncode == 0 and (root / 'allowed').read_bytes() == b'NEW', (
64 f'local --files-from upload failed: rc={safe.returncode}, '
65 f'stdout={safe.stdout!r}, stderr={safe.stderr!r}')
66 assert not (root / 'unlisted').exists(), (
67 'the client-local list was not honoured: an unlisted file was uploaded, '
68 'so this control does not show that --files-from selected anything')