Debian Codegraph @

testsuite/daemon-exec-metachar-documented-limit_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """The hook metacharacter refusal applies to operator values too, as documented.
3
4 rsyncd.conf(5) states this under `pre-xfer exec`: a %VAR% expanded into a hook
5 command is refused if the value carries a shell-active character, and that
6 applies to EVERY %RSYNC_*% value including ones the operator supplied. A module
7 whose path contains a space therefore cannot be interpolated into a hook, and
8 the documented alternative is to read the value from the environment instead.
9
10 The refusal is deliberate rather than an oversight -- rsync cannot tell an
11 operator's space from an attacker's once both are inside the same string, and
12 `path` may itself be built from a peer value (`path = /home/%RSYNC_USER_NAME%`).
13 It is pinned here because it is a real usability cost, and if it ever changes
14 the manual has to change with it.
15 """
16
17 import os
18 import shlex
19 import subprocess
20
21 from rsyncfns import (
22 SCRATCHDIR, makepath, rmtree, rsync_argv, start_test_daemon, test_fail,
23 write_daemon_conf,
24 )
25
26 REFUSAL = 'holds a shell metacharacter'
27
28 # The daemon inherits this process's environment. If RSYNC_MODULE_PATH happens
29 # to be set already, %RSYNC_MODULE_PATH% expands from the inherited value and
30 # the workaround half passes even when rsync never exported it -- the check
31 # would prove nothing. Poison it, so a pass means rsync overwrote it.
32 os.environ['RSYNC_MODULE_PATH'] = '/inherited-value-that-rsync-must-overwrite'
33
34 base = SCRATCHDIR / 'exec-metachar-documented-limit'
35 rmtree(base)
36 module = base / 'My Backups' # an ordinary directory name with a space
37 makepath(module)
38 (module / 'f.txt').write_text('DATA\n')
39
40 # --- interpolated: refused ------------------------------------------------
41 dest = base / 'dest'
42 makepath(dest)
43 conf = write_daemon_conf([
44 ('m', {
45 'path': str(module), 'read only': 'yes',
46 'pre-xfer exec': "sh -c 'printf %s \"%RSYNC_MODULE_PATH%\" >/dev/null'",
47 }),
48 ], name='exec-metachar-interpolated.conf')
49 url = start_test_daemon(conf, 12991)
50
51 proc = subprocess.run(rsync_argv('-r', f'{url}m/', f'{dest}/'),
52 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
53 if proc.returncode == 0:
54 test_fail('a module path containing a space was interpolated into a hook '
55 'and served; rsyncd.conf(5) says it is refused, so either the '
56 f'manual or the code is now wrong: {proc.stdout.strip()[:300]!r}')
57
58 log = SCRATCHDIR / 'rsyncd.log'
59 log_text = log.read_text(errors='replace') if log.exists() else ''
60 if REFUSAL not in log_text:
61 test_fail('the transfer failed, but not with the documented metacharacter '
62 f'refusal -- something else stopped it: {proc.stdout.strip()[:300]!r}')
63
64 # --- read from the environment: works -------------------------------------
65 # The manual offers this as the way to use such a value, so it has to work.
66 hook = base / 'hook.sh'
67 witness = base / 'saw.txt'
68 hook.write_text(f'#!/bin/sh\nprintf %s "$RSYNC_MODULE_PATH" > {shlex.quote(str(witness))}\n')
69 hook.chmod(0o755)
70
71 dest2 = base / 'dest2'
72 makepath(dest2)
73 conf2 = write_daemon_conf([
74 ('m', {'path': str(module), 'read only': 'yes',
75 'pre-xfer exec': shlex.quote(str(hook))}),
76 ], globals={'pid file': str(base / 'rsyncd2.pid')},
77 name='exec-metachar-environment.conf')
78 url2 = start_test_daemon(conf2, 12992)
79
80 proc2 = subprocess.run(rsync_argv('-r', f'{url2}m/', f'{dest2}/'),
81 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
82 if proc2.returncode != 0:
83 test_fail('the documented workaround failed: reading $RSYNC_MODULE_PATH '
84 'from the environment must not be subject to the refusal '
85 f'({proc2.stdout.strip()[:300]!r})')
86 if not (dest2 / 'f.txt').is_file():
87 test_fail(f'workaround reported success but transferred nothing: {proc2.stdout!r}')
88 if not witness.is_file():
89 test_fail('the hook did not run at all, so the workaround is unproven')
90 got = witness.read_text()
91 if got != str(module):
92 test_fail(f'the hook received {got!r} from the environment, not the module '
93 f'path {str(module)!r} -- the space did not survive')
94
95 # The manual lists the refused characters; only the space is exercised above,
96 # so the rest would silently become wrong if the set were narrowed. Each module
97 # path must EXIST and each daemon needs its own log: a path that is merely
98 # missing makes the transfer fail on its own, and checking only the exit status
99 # then passes whether or not the character was refused.
100 port = 12995
101 for ch in ('*', '?', '[', ']', '#', '!', '~', '{', '}'):
102 mod = base / f'ch{ord(ch)}' / f'x{ch}y'
103 makepath(mod)
104 (mod / 'f.txt').write_text('DATA\n')
105 chlog = base / f'log{ord(ch)}'
106 c = write_daemon_conf([
107 ('m', {'path': str(mod), 'read only': 'yes',
108 'pre-xfer exec': "sh -c 'printf %s \"%RSYNC_MODULE_PATH%\" >/dev/null'"}),
109 ], globals={'pid file': str(base / f'pid{ord(ch)}'), 'log file': str(chlog)},
110 name=f'exec-metachar-{ord(ch)}.conf')
111 u = start_test_daemon(c, port)
112 port += 1
113 out = base / f'out{ord(ch)}'
114 makepath(out)
115 r = subprocess.run(rsync_argv('-r', f'{u}m/', f'{out}/'),
116 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
117 if r.returncode == 0:
118 test_fail(f'a module path containing {ch!r} was interpolated into a hook '
119 'and served; rsyncd.conf(5) lists it as refused')
120 chlog_text = chlog.read_text(errors='replace') if chlog.is_file() else ''
121 if REFUSAL not in chlog_text:
122 test_fail(f'the transfer for {ch!r} failed, but not with the documented '
123 f'refusal -- the module path exists, so something else went '
124 f'wrong and this check proves nothing: {r.stdout.strip()[:200]!r}')
125
126 print('an operator value with a space is refused when interpolated, and works '
127 'when read from the environment, exactly as rsyncd.conf(5) says; every '
128 'documented character is refused too')