| 1 | #!/usr/bin/env python3 |
| 2 | # Regression: a non-daemon receiver must FOLLOW an absolute symlinked |
| 3 | # destination directory -- the common `/backup -> /mnt/disk`, `/var/www -> |
| 4 | # /srv/www` admin pattern that scripts/cron use with absolute paths. |
| 5 | # |
| 6 | # The dest-symlink hardening opened the operator-named absolute dest with |
| 7 | # O_NOFOLLOW and *refused* a symlink ("refusing to chdir into symlinked |
| 8 | # destination ... use --insecure-links"), exiting 3. A relative symlinked dest |
| 9 | # always followed (plain chdir), so the behaviour was inconsistent too. Now a |
| 10 | # real directory is still opened O_NOFOLLOW (never reached via a raced leaf |
| 11 | # symlink), but an operator-named symlinked dest is followed; the per-entry |
| 12 | # transfer stays confined beneath the resolved root. |
| 13 | |
| 14 | import os |
| 15 | |
| 16 | from rsyncfns import SCRATCHDIR, assert_same, rmtree, run_rsync, test_fail |
| 17 | |
| 18 | base = SCRATCHDIR / 'dest-symlinked' |
| 19 | src = base / 'src' |
| 20 | real = base / 'realdest' |
| 21 | link = base / 'backup' # absolute symlink -> realdest |
| 22 | rmtree(base) |
| 23 | src.mkdir(parents=True) |
| 24 | real.mkdir(parents=True) |
| 25 | (src / 'f.txt').write_text('content\n') |
| 26 | (src / 'sub').mkdir() |
| 27 | (src / 'sub' / 'g.txt').write_text('deep\n') |
| 28 | os.symlink(str(real), str(link)) # ABSOLUTE symlinked destination |
| 29 | |
| 30 | # Absolute dest path through the symlink -- this exited 3 on the unfixed branch. |
| 31 | run_rsync('-a', f'{src}/', f'{link}/') # check=True: must succeed (exit 0) |
| 32 | |
| 33 | for rel in ('f.txt', 'sub/g.txt'): |
| 34 | got = real / rel |
| 35 | if not got.is_file(): |
| 36 | test_fail("transfer through an absolute symlinked dest did not land in " |
| 37 | f"the real directory: {got} missing") |
| 38 | assert_same(src / rel, got, label=rel) |
| 39 | |
| 40 | print("dest-symlinked-dir: a non-daemon receiver follows an absolute symlinked dest") |