Debian Codegraph @

testsuite/daemon-exec-rsync-env-shell-escape_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 import os
3 import subprocess
4
5 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail, write_daemon_conf
6
7 base = SCRATCHDIR / 'exec-env-escape'
8 rmtree(base)
9 src = base / 'src'
10 src.mkdir(parents=True)
11 (src / 'f').write_text('payload\n')
12 sentinel = base / 'pwned'
13
14 os.environ['RSYNC_REQUEST'] = f"ok;touch {sentinel};#"
15 conf = write_daemon_conf([
16 ('execmod', {'path': str(src), 'read only': 'yes',
17 'pre-xfer exec': 'sh -c "printf %RSYNC_REQUEST% >/dev/null"'}),
18 ])
19 url = start_test_daemon(conf, 12936)
20 proc = subprocess.run(
21 rsync_argv('-r', f'{url}execmod/'),
22 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
23 os.environ.pop('RSYNC_REQUEST', None)
24
25 if sentinel.exists():
26 test_fail("daemon exec-hook %RSYNC_REQUEST% expansion executed injected shell syntax")
27 # A value carrying shell syntax is refused outright rather than quoted, so the
28 # hook must not run and the transfer must fail closed.
29 if proc.returncode == 0:
30 test_fail("daemon exec-hook accepted a %RSYNC_REQUEST% holding shell syntax")
31
32 print("daemon-exec-rsync-env-shell-escape: %RSYNC_*% holding shell syntax is refused")