| 1 |
|
| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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' |
| 37 |
makepath(module) |
| 38 |
(module / 'f.txt').write_text('DATA\n') |
| 39 |
|
| 40 |
|
| 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 |
|
| 65 |
|
| 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 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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') |