| 1 |
|
| 2 |
"""Pin what `--safe-links` / `--copy-unsafe-links` call an "unsafe" symlink, and |
| 3 |
the "top of the transfer" cut-off. |
| 4 |
|
| 5 |
The rsync(1) docs define an unsafe symlink only loosely: --safe-links "ignore[s] |
| 6 |
symlinks that point outside the destination tree", and --copy-unsafe-links acts |
| 7 |
on links "that point outside the copied tree", with the cut-off described as |
| 8 |
"the top of the transfer ... the part of the path that rsync isn't mentioning in |
| 9 |
the verbose output". That last phrase is not something a user can test against. |
| 10 |
|
| 11 |
The actual rule (unsafe_symlink() in util1.c) is purely LEXICAL on the link |
| 12 |
value relative to the link's depth within the transfer: |
| 13 |
- any absolute target ('/...') is unsafe; |
| 14 |
- a relative target is unsafe iff its ".." count rises above the transfer top. |
| 15 |
It does NOT resolve the target on disk. This test pins both the per-shape |
| 16 |
verdict and the trailing-slash cut-off, measured (not assumed), so the doc can |
| 17 |
state the rule concretely. Pure local client behaviour: no daemon/root/tcp. |
| 18 |
|
| 19 |
Cross-version: the lexical rule is long-standing; expected identical against |
| 20 |
--rsync-bin=old_versions/rsync_3.2.7. |
| 21 |
""" |
| 22 |
|
| 23 |
import os |
| 24 |
import subprocess |
| 25 |
|
| 26 |
from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail |
| 27 |
|
| 28 |
base = SCRATCHDIR / 'safe-links' |
| 29 |
rmtree(base) |
| 30 |
base.mkdir(parents=True) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
(base / 'sibling').write_text('SIBLING\n') |
| 35 |
|
| 36 |
|
| 37 |
tree = base / 'tree' |
| 38 |
tree.mkdir() |
| 39 |
(tree / 'file').write_text('FILE\n') |
| 40 |
(tree / 'sub').mkdir() |
| 41 |
(tree / 'sub' / 'subfile').write_text('SUBFILE\n') |
| 42 |
|
| 43 |
SHAPES = { |
| 44 |
'rel-same-dir': ('link_a', 'file'), |
| 45 |
'rel-into-sub': ('link_b', 'sub/subfile'), |
| 46 |
'rel-up-inside': ('sub/link_c', '../file'), |
| 47 |
'rel-escape': ('link_d', '../sibling'), |
| 48 |
'rel-deep-escape':('sub/link_e', '../../sibling'), |
| 49 |
'abs-target': ('link_f', str(base / 'sibling')), |
| 50 |
} |
| 51 |
for _name, (where, val) in SHAPES.items(): |
| 52 |
lp = tree / where |
| 53 |
lp.parent.mkdir(parents=True, exist_ok=True) |
| 54 |
os.symlink(val, lp) |
| 55 |
|
| 56 |
|
| 57 |
UNSAFE = {'rel-escape', 'rel-deep-escape', 'abs-target'} |
| 58 |
|
| 59 |
|
| 60 |
def run(*args): |
| 61 |
subprocess.run(rsync_argv('-a', *args), stdout=subprocess.DEVNULL, |
| 62 |
stderr=subprocess.DEVNULL) |
| 63 |
|
| 64 |
|
| 65 |
def fresh(dst): |
| 66 |
rmtree(dst) |
| 67 |
dst.mkdir(parents=True) |
| 68 |
|
| 69 |
|
| 70 |
grid, mism = [], [] |
| 71 |
|
| 72 |
|
| 73 |
dst = base / 'safe_dst' |
| 74 |
fresh(dst) |
| 75 |
run('--safe-links', f'{tree}/', f'{dst}/') |
| 76 |
for name, (where, _val) in SHAPES.items(): |
| 77 |
present = (dst / where).is_symlink() |
| 78 |
want_present = name not in UNSAFE |
| 79 |
grid.append(f"safe-links {name:15} present={int(present)} want={int(want_present)}") |
| 80 |
if present != want_present: |
| 81 |
mism.append(f"--safe-links {name}: present={present}, expected={want_present}") |
| 82 |
|
| 83 |
|
| 84 |
dst = base / 'copy_dst' |
| 85 |
fresh(dst) |
| 86 |
run('--copy-unsafe-links', f'{tree}/', f'{dst}/') |
| 87 |
for name, (where, _val) in SHAPES.items(): |
| 88 |
p = dst / where |
| 89 |
is_link = p.is_symlink() |
| 90 |
is_file = p.is_file() and not is_link |
| 91 |
if name in UNSAFE: |
| 92 |
|
| 93 |
ok = is_file |
| 94 |
grid.append(f"copy-unsafe {name:15} deref={int(is_file)} want=1") |
| 95 |
else: |
| 96 |
ok = is_link |
| 97 |
grid.append(f"copy-unsafe {name:15} keptlink={int(is_link)} want=1") |
| 98 |
if not ok: |
| 99 |
mism.append(f"--copy-unsafe-links {name}: is_link={is_link} is_file={is_file}") |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
dst = base / 'cut_inside' |
| 107 |
fresh(dst) |
| 108 |
run('--safe-links', f'{tree}/', f'{dst}/') |
| 109 |
kept_inside = (dst / 'sub' / 'link_c').is_symlink() |
| 110 |
grid.append(f"cutoff top=tree sub/link_c kept={int(kept_inside)} want=1") |
| 111 |
if not kept_inside: |
| 112 |
mism.append("cut-off: sub/link_c (../file) should be SAFE when top=tree") |
| 113 |
|
| 114 |
dst = base / 'cut_sub' |
| 115 |
fresh(dst) |
| 116 |
run('--safe-links', f'{tree}/sub/', f'{dst}/') |
| 117 |
kept_sub = (dst / 'link_c').is_symlink() |
| 118 |
grid.append(f"cutoff top=sub link_c kept={int(kept_sub)} want=0") |
| 119 |
if kept_sub: |
| 120 |
mism.append("cut-off: link_c (../file) should be UNSAFE when top=sub") |
| 121 |
|
| 122 |
if os.environ.get('REPORT_SAFE'): |
| 123 |
test_fail("REPORT-SAFE grid:\n " + "\n ".join(grid)) |
| 124 |
if mism: |
| 125 |
test_fail("safe-links/copy-unsafe-links contract deviated:\n " + "\n ".join(mism)) |
| 126 |
|
| 127 |
print("safe-links-unsafe-def: unsafe == absolute target OR a relative target " |
| 128 |
"whose '..' depth rises above the transfer top (lexical, not resolved); " |
| 129 |
"--safe-links drops unsafe links, --copy-unsafe-links dereferences them; " |
| 130 |
"the same '../file' link flips safe->unsafe as the transfer top moves down") |