Debian Codegraph @

testsuite/daemon-path-rsync-var_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression: a %RSYNC_*% reference in an ordinary daemon string param
3 # (path/uid/gid/...) must expand to the RAW value, not be shell-quoted.
4 #
5 # The %RSYNC_*% shell-escaping (an injection guard for the shell-executed hooks:
6 # pre/post-xfer exec, early exec, name converter) leaked into the shared
7 # expand_vars() used by every string accessor. So `path = /srv/%RSYNC_MODULE_NAME%`
8 # expanded to /srv/'served' and the documented `path = /home/%RSYNC_USER_NAME%`
9 # (rsyncd.conf.5) broke with "@ERROR: chdir failed". Now only the hook
10 # accessors escape; ordinary params expand verbatim.
11
12 import subprocess
13
14 from rsyncfns import (
15 SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail,
16 write_daemon_conf,
17 )
18
19 DAEMON_PORT = 12906
20
21 base = SCRATCHDIR / 'daemon-path-var'
22 rmtree(base)
23 # The module's real path is <root>/<module-name>; the conf references it via
24 # %RSYNC_MODULE_NAME% (set for every connection, no auth needed). If that
25 # expands quoted, the daemon chdir's to <root>/'served' (literal quotes), which
26 # does not exist -> the pull fails.
27 root = base / 'root'
28 served = root / 'served' # module is named "served"
29 served.mkdir(parents=True)
30 (served / 'hello.txt').write_text('EXPANDED\n')
31
32 conf = write_daemon_conf([
33 ('served', {'path': f'{root}/%RSYNC_MODULE_NAME%',
34 'use chroot': 'no', 'read only': 'yes'}),
35 ])
36 url = start_test_daemon(conf, DAEMON_PORT).rstrip('/')
37
38 dest = base / 'dest'
39 dest.mkdir(parents=True)
40 proc = subprocess.run(
41 rsync_argv('-a', f'{url}/served/hello.txt', str(dest) + '/'),
42 capture_output=True, text=True)
43
44 got = dest / 'hello.txt'
45 if proc.returncode != 0 or not got.is_file():
46 test_fail("%RSYNC_MODULE_NAME% in `path` did not expand to the raw value "
47 "(the daemon could not chdir -- the path was shell-quoted); "
48 f"rc={proc.returncode}\nstderr: {proc.stderr.strip()}")
49 if got.read_text() != 'EXPANDED\n':
50 test_fail(f"unexpected content from the expanded-path module: {got.read_text()!r}")
51
52 print("daemon-path-rsync-var: %RSYNC_*% in a string param expands unquoted")