|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Receiver file arguments must remain beneath a pinned parent.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import shlex |
| 6 | +import subprocess |
| 7 | +import time |
| 8 | + |
| 9 | +from rsyncfns import ( |
| 10 | + SCRATCHDIR, makepath, patched_rrsync, proc_self_fd_pins, rmtree, |
| 11 | + rsync_argv, test_fail, test_skipped, |
| 12 | +) |
| 13 | + |
| 14 | +if not proc_self_fd_pins(): |
| 15 | + test_skipped('requires /proc/self/fd directory pins') |
| 16 | + |
| 17 | +base = SCRATCHDIR / 'rrsync-receiver-file' |
| 18 | +rmtree(base) |
| 19 | +restricted = base / 'restricted' |
| 20 | +outside = base / 'outside' |
| 21 | +source = base / 'source' |
| 22 | +downloads = base / 'downloads' |
| 23 | +makepath(restricted, outside, downloads) |
| 24 | +source.write_text('replacement contents\n') |
| 25 | + |
| 26 | +# The optional gate pauses the real server after rrsync validates its argument. |
| 27 | +shim = base / 'rsync-shim' |
| 28 | +shim.write_text('''#!/usr/bin/env python3 |
| 29 | +import os, pathlib, sys, time |
| 30 | +ready = os.environ.get('RRSYNC_TEST_READY') |
| 31 | +if ready: |
| 32 | + pathlib.Path(ready).touch() |
| 33 | + go = pathlib.Path(os.environ['RRSYNC_TEST_GO']) |
| 34 | + end = time.monotonic() + 10 |
| 35 | + while not go.exists(): |
| 36 | + if time.monotonic() >= end: |
| 37 | + sys.exit('timed out waiting for test gate') |
| 38 | + time.sleep(0.01) |
| 39 | +cmd = %r |
| 40 | +os.execvp(cmd[0], cmd + sys.argv[1:]) |
| 41 | +''' % rsync_argv()) |
| 42 | +shim.chmod(0o755) |
| 43 | +rrsync = patched_rrsync(base, rsync_path=str(shim)) |
| 44 | +rsh = base / 'fake-rsh' |
| 45 | +rsh.write_text('#!/bin/sh\nshift\nSSH_ORIGINAL_COMMAND="$*"\n' |
| 46 | + 'export SSH_ORIGINAL_COMMAND\nexec %s -no-lock %s\n' |
| 47 | + % (shlex.quote(str(rrsync)), shlex.quote(str(restricted)))) |
| 48 | +rsh.chmod(0o755) |
| 49 | + |
| 50 | + |
| 51 | +def command(src, dest, *options): |
| 52 | + return rsync_argv('-ac', '-e', str(rsh), *options, str(src), str(dest)) |
| 53 | + |
| 54 | + |
| 55 | +def transfer(src, dest, *options, succeeds=True): |
| 56 | + proc = subprocess.run(command(src, dest, *options), capture_output=True, |
| 57 | + text=True, timeout=20) |
| 58 | + if (proc.returncode == 0) != succeeds: |
| 59 | + test_fail(f'transfer {src} -> {dest}: rc={proc.returncode}, ' |
| 60 | + f'{proc.stdout!r} {proc.stderr!r}') |
| 61 | + return proc |
| 62 | + |
| 63 | + |
| 64 | +def check_contents(path): |
| 65 | + if path.is_symlink() or not path.is_file() or path.read_text() != source.read_text(): |
| 66 | + test_fail(f'wrong file contents or type: {path}') |
| 67 | + |
| 68 | + |
| 69 | +for option in ((), ('--inplace',)): |
| 70 | + destination = restricted / 'existing' |
| 71 | + destination.write_text('old\n') |
| 72 | + transfer(source, 'dummy:existing', *option) |
| 73 | + check_contents(destination) |
| 74 | + new = restricted / 'new' |
| 75 | + new.unlink(missing_ok=True) |
| 76 | + transfer(source, 'dummy:new', *option) |
| 77 | + check_contents(new) |
| 78 | + |
| 79 | +makepath(restricted / 'directory') |
| 80 | +transfer(source, 'dummy:directory/') |
| 81 | +check_contents(restricted / 'directory' / source.name) |
| 82 | +transfer('dummy:existing', str(downloads) + '/') |
| 83 | +check_contents(downloads / 'existing') |
| 84 | + |
| 85 | +marker = outside / 'untouched' |
| 86 | +marker.write_text('outside fixture\n') |
| 87 | +transfer(source, 'dummy:../outside/untouched', succeeds=False) |
| 88 | +(restricted / 'outside-link').symlink_to(outside, target_is_directory=True) |
| 89 | +transfer(source, 'dummy:outside-link/untouched', succeeds=False) |
| 90 | +if marker.read_text() != 'outside fixture\n': |
| 91 | + test_fail('a rejected destination changed the outside fixture') |
| 92 | + |
| 93 | + |
| 94 | +def gated_transfer(name, change): |
| 95 | + ready = base / 'ready' |
| 96 | + go = base / 'go' |
| 97 | + ready.unlink(missing_ok=True) |
| 98 | + go.unlink(missing_ok=True) |
| 99 | + env = {**os.environ, 'RRSYNC_TEST_READY': str(ready), 'RRSYNC_TEST_GO': str(go)} |
| 100 | + proc = subprocess.Popen(command(source, f'dummy:{name}', '--inplace'), |
| 101 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 102 | + text=True, env=env) |
| 103 | + try: |
| 104 | + end = time.monotonic() + 10 |
| 105 | + while not ready.exists(): |
| 106 | + if proc.poll() is not None or time.monotonic() >= end: |
| 107 | + test_fail('receiver did not reach the post-validation gate') |
| 108 | + time.sleep(0.01) |
| 109 | + change() |
| 110 | + go.touch() |
| 111 | + stdout, stderr = proc.communicate(timeout=20) |
| 112 | + if marker.read_text() != 'outside fixture\n': |
| 113 | + test_fail(f'in-place transfer changed the outside fixture: {stdout} {stderr}') |
| 114 | + return proc.returncode |
| 115 | + finally: |
| 116 | + if proc.poll() is None: |
| 117 | + proc.kill() |
| 118 | + proc.communicate() |
| 119 | + |
| 120 | + |
| 121 | +leaf = restricted / 'raced-leaf' |
| 122 | +leaf.write_text('old\n') |
| 123 | + |
| 124 | + |
| 125 | +def replace_leaf(): |
| 126 | + leaf.unlink() |
| 127 | + leaf.symlink_to(marker) |
| 128 | + |
| 129 | + |
| 130 | +rc = gated_transfer('raced-leaf', replace_leaf) |
| 131 | +if rc == 0: |
| 132 | + check_contents(leaf) |
| 133 | + |
| 134 | +parent = restricted / 'raced-parent' |
| 135 | +makepath(parent) |
| 136 | +(parent / 'untouched').write_text('old\n') |
| 137 | +saved_parent = restricted / 'saved-parent' |
| 138 | + |
| 139 | + |
| 140 | +def replace_parent(): |
| 141 | + parent.rename(saved_parent) |
| 142 | + parent.symlink_to(outside, target_is_directory=True) |
| 143 | + |
| 144 | + |
| 145 | +if gated_transfer('raced-parent/untouched', replace_parent) == 0: |
| 146 | + check_contents(saved_parent / 'untouched') |
| 147 | + |
| 148 | +print('rrsync receiver file overwrite, creation, directory, pull and path checks passed') |
0 commit comments