Debian Codegraph @

testsuite/files-from-path-clamp_test.py

raw symbols SCIP
1 """Pin how `--files-from` treats a leading slash and ".." components.
2
3 The rsync(1) `--files-from` entry states the goal but not the mechanism: "any
4 leading slashes are removed and no '..' references are allowed to go higher than
5 the source dir." "are removed" / "are not allowed" don't say what actually
6 happens to such an entry. Measured behaviour:
7
8 - a leading "/" is stripped and the entry is taken relative to the source dir;
9 - a ".." component is CLAMPED (trimmed) so the path cannot rise above the
10 source dir -- "../keep" resolves to "<source>/keep", NOT the parent's "keep".
11
12 So an entry that tries to escape silently resolves back inside the source; it
13 does not error out by virtue of the "..", and it never reaches a sibling of the
14 source. Pure local client behaviour: no daemon/root/tcp. Cross-version:
15 expected identical against --rsync-bin=old_versions/rsync_3.2.7.
16 """
17
18 import subprocess
19
20 from rsyncfns import SCRATCHDIR, rmtree, rsync_argv, test_fail
21
22 base = SCRATCHDIR / 'files-from-clamp'
23 rmtree(base)
24 base.mkdir(parents=True)
25
26 src = base / 'src'
27 src.mkdir()
28 (src / 'file').write_text('INSRC-FILE\n')
29 (src / 'keep').write_text('INSRC-KEEP\n') # what "../keep" must clamp to
30 # A sibling of the source with the SAME basename: a real escape would grab this.
31 (base / 'keep').write_text('OUTSIDE-KEEP-MUST-NOT-APPEAR\n')
32
33 listf = base / 'list'
34 # leading-slash entry, a normal entry, and an escaping "../keep".
35 listf.write_text('/file\nkeep\n../keep\n')
36
37 dst = base / 'dst'
38 rmtree(dst)
39 dst.mkdir()
40 proc = subprocess.run(
41 rsync_argv('-a', f'--files-from={listf}', f'{src}/', f'{dst}/'),
42 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
43
44 # leading "/" stripped -> taken relative to the source.
45 f = dst / 'file'
46 if not f.is_file() or f.read_text() != 'INSRC-FILE\n':
47 test_fail("--files-from: leading-slash '/file' was not transferred relative "
48 "to the source dir")
49
50 # "keep" and the clamped "../keep" both resolve to <source>/keep, never the
51 # parent's keep -> the escape was prevented.
52 k = dst / 'keep'
53 if not k.is_file():
54 test_fail("--files-from: 'keep' did not transfer")
55 if k.read_text() != 'INSRC-KEEP\n':
56 test_fail(f"--files-from: '../keep' escaped the source dir -- dst/keep is "
57 f"{k.read_text()!r}, expected the in-source 'INSRC-KEEP'")
58
59 # No file should have landed outside dst (no sibling-of-dst named 'keep' created).
60 if (base / 'keep').read_text() != 'OUTSIDE-KEEP-MUST-NOT-APPEAR\n':
61 test_fail("--files-from: the outside 'keep' was modified")
62
63 print("files-from-path-clamp: leading '/' stripped (entry taken relative to the "
64 "source); '..' is clamped within the source dir so '../keep' resolves to "
65 "<source>/keep and cannot reach a sibling of the source")