Debian Codegraph @

testsuite/rrsync-debug-denied_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """rrsync must not let a peer turn on the server's --debug output.
3
4 Defence in depth, not the fix: what actually stops a merge file's contents
5 coming back is the redaction in exclude.c (see filter-merge-content-echo).
6
7 This closes ONE spelling of the control -- an explicit -M--debug -- and no
8 more. It does not close the FILTER trace family: options.c maps verbosity
9 onto the debug flags, so -vvv still raises a restricted server to FILTER2 and
10 forwards it, and the trace metadata (merge-file openability, the server's
11 absolute paths, mergelist bookkeeping) comes back with it. Rule TEXT stays
12 redacted at every verbosity, which is the property that matters and which
13 filter-merge-content-echo pins. Do not read this test as sealing -vvv.
14
15 It costs nothing: server_options() forwards --info but never --debug, so no
16 stock client sends one, and the only way it reaches a server is a peer asking
17 for it by name with -M.
18 """
19
20 import os
21 import signal
22 import subprocess
23 import sys
24
25 signal.signal(signal.SIGUSR1, signal.SIG_IGN)
26 signal.signal(signal.SIGUSR2, signal.SIG_IGN)
27 if '--shell' in sys.argv:
28 index = sys.argv.index('--shell') + 2
29 env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[index:])}
30 signal.signal(signal.SIGUSR1, signal.SIG_DFL)
31 signal.signal(signal.SIGUSR2, signal.SIG_DFL)
32 wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT']
33 os.execve(wrapper, [wrapper, '-wo', root], env)
34
35 from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv
36
37 base = SCRATCHDIR / 'rrsync-debug-denied'
38 rmtree(base)
39 src = base / 'src'
40 root = base / 'root'
41 makepath(src, root)
42 (src / 'f').write_bytes(b'hi\n')
43
44 # RSYNC may be a multi-word command (the runner's --protocol=N, or valgrind)
45 # while rrsync hands its RSYNC to execlp() as a single executable name, so it
46 # has to be wrapped before patched_rrsync() sees it.
47 shim = base / 'rsync-shim'
48 shim.write_text('#!/bin/sh\nexec ' + RSYNC + ' "$@"\n')
49 shim.chmod(0o755)
50 wrapper = patched_rrsync(base, rsync_path=str(shim))
51 rsh = f'{sys.executable} {os.path.abspath(__file__)} --shell'
52 env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)}
53
54 for spelling in ('-M--debug=FILTER2', '--remote-option=--debug=ALL3'):
55 got = subprocess.run(
56 rsync_argv('-r', spelling, '-e', rsh, f'{src}/', 'ignored:'),
57 env=env, capture_output=True, text=True, timeout=20)
58 out = got.stdout + got.stderr
59 assert got.returncode != 0 and 'option --debug has been disabled' in out, (
60 f'rrsync accepted a peer-selected {spelling}: rc={got.returncode}, '
61 f'out={out!r}')
62
63 # The option table is generated by the cull-options script, so the entry this
64 # relies on can come back on a regeneration. Check the value, not just the
65 # behaviour, so that is unambiguous when it happens.
66 assert "'debug': -1," in wrapper.read_text(), (
67 "support/rrsync no longer disables 'debug' -- a cull-options regeneration "
68 'probably restored it')
69
70 # Ordinary transfers must be unaffected -- including -vvv, which reaches
71 # FILTER2 by verbosity alone and so must NOT be caught by the refusal above.
72 for extra in ([], ['-vv'], ['-vvv']):
73 ok = subprocess.run(
74 rsync_argv('-r', *extra, '-e', rsh, f'{src}/', 'ignored:'),
75 env=env, capture_output=True, text=True, timeout=20)
76 assert ok.returncode == 0 and (root / 'f').read_bytes() == b'hi\n', (
77 f'an ordinary transfer broke with {extra}: rc={ok.returncode}, '
78 f'stderr={ok.stderr!r}')