| 1 |
|
| 2 |
"""A forced rrsync -wo endpoint must not return a local --files-from file.""" |
| 3 |
|
| 4 |
import os |
| 5 |
import shlex |
| 6 |
import signal |
| 7 |
import subprocess |
| 8 |
import sys |
| 9 |
|
| 10 |
signal.signal(signal.SIGUSR1, signal.SIG_IGN) |
| 11 |
signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 12 |
if '--shell' in sys.argv: |
| 13 |
i = sys.argv.index('--shell') + 2 |
| 14 |
env = {**os.environ, 'SSH_ORIGINAL_COMMAND': ' '.join(sys.argv[i:])} |
| 15 |
signal.signal(signal.SIGUSR1, signal.SIG_DFL) |
| 16 |
signal.signal(signal.SIGUSR2, signal.SIG_DFL) |
| 17 |
wrapper, root = env['RRSYNC_WRAPPER'], env['RRSYNC_ROOT'] |
| 18 |
os.execve(wrapper, [wrapper, '-wo', '-no-lock', root], env) |
| 19 |
|
| 20 |
from rsyncfns import RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, rsync_path_arg |
| 21 |
|
| 22 |
base = SCRATCHDIR / 'rrsync-write-only-files-from' |
| 23 |
rmtree(base) |
| 24 |
src = base / 'src' |
| 25 |
root = base / 'root' |
| 26 |
makepath(src, root) |
| 27 |
secret_name = 'SERVER-ONLY-SECRET-NAME' |
| 28 |
(root / 'protected-list').write_text(secret_name + '\n') |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
shim = base / 'rsync-shim' |
| 34 |
shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n') |
| 35 |
shim.chmod(0o755) |
| 36 |
wrapper = patched_rrsync(base, rsync_path=str(shim)) |
| 37 |
rsh = f'{shlex.quote(sys.executable)} {shlex.quote(os.path.abspath(__file__))} --shell' |
| 38 |
env = {**os.environ, 'RRSYNC_WRAPPER': str(wrapper), 'RRSYNC_ROOT': str(root)} |
| 39 |
got = subprocess.run( |
| 40 |
rsync_argv('-r', '--files-from=:protected-list', |
| 41 |
'-e', rsh, f'{src}/', 'ignored:'), |
| 42 |
env=env, capture_output=True, text=True, timeout=20) |
| 43 |
|
| 44 |
assert got.returncode != 0 and 'write-only server cannot read' in got.stderr, ( |
| 45 |
f'write-only rrsync accepted remote --files-from: rc={got.returncode}, ' |
| 46 |
f'stdout={got.stdout!r}, stderr={got.stderr!r}') |
| 47 |
assert secret_name not in got.stdout and secret_name not in got.stderr, ( |
| 48 |
f'write-only rrsync returned the protected record: rc={got.returncode}, ' |
| 49 |
f'stdout={got.stdout!r}, stderr={got.stderr!r}') |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
(src / 'allowed').write_bytes(b'NEW') |
| 56 |
(src / 'unlisted').write_bytes(b'NOPE') |
| 57 |
local_list = base / 'local-list' |
| 58 |
local_list.write_text('allowed\n') |
| 59 |
safe = subprocess.run( |
| 60 |
rsync_argv('-r', f'--files-from={local_list}', |
| 61 |
'-e', rsh, f'{src}/', 'ignored:'), |
| 62 |
env=env, capture_output=True, text=True, timeout=20) |
| 63 |
assert safe.returncode == 0 and (root / 'allowed').read_bytes() == b'NEW', ( |
| 64 |
f'local --files-from upload failed: rc={safe.returncode}, ' |
| 65 |
f'stdout={safe.stdout!r}, stderr={safe.stderr!r}') |
| 66 |
assert not (root / 'unlisted').exists(), ( |
| 67 |
'the client-local list was not honoured: an unlisted file was uploaded, ' |
| 68 |
'so this control does not show that --files-from selected anything') |