Debian Codegraph @

testsuite/operator-path-traversal-partial-dir-daemon_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon --partial-dir ".." traversal into the module's excluded subtree, on a
3 # `path = /` module. (Reported by Omar Elsayed.) The peer-supplied --partial-dir
4 # climbs via ".." back into the excluded subtree; a staged partial must not land
5 # there. Sandbox-anchored under SCRATCHDIR (Omar's original wrote to the real
6 # /secret), so it runs unprivileged; the module still serves "/".
7 import os
8 import subprocess
9
10 from rsyncfns import (
11 SCRATCHDIR, rmtree, rsync_argv, start_test_daemon, test_fail, write_daemon_conf, test_skipped,
12 )
13
14 DAEMON_PORT = 12965
15
16 # rsyncd.conf's "exclude" is a SPACE-SEPARATED pattern list, so a path with a
17 # space in it cannot be expressed there at all -- the pattern silently matches
18 # nothing and the traversal below then "escapes" for a reason that has nothing
19 # to do with the protection under test. ("filter" is space-safe and does hold;
20 # verified by running this same case with a filter rule.) Skip rather than
21 # report a bogus escape.
22 if ' ' in str(SCRATCHDIR):
23 test_skipped("scratch path contains a space; rsyncd.conf 'exclude' is a "
24 "space-separated list and cannot express such a pattern")
25 SECRET = "PROTECTED-IN-EXCLUDED-SUBTREE\n"
26
27 base = SCRATCHDIR / 'traversal-partial-dir-daemon'
28 rmtree(base)
29 base.mkdir()
30
31 partial = base / 'partial-dir-daemon'
32 partial.mkdir()
33
34 secret = base / 'secret' # excluded subtree
35 secret.mkdir()
36 victim = secret / 'f0'
37 victim.write_text(SECRET)
38
39 dest = partial / 'dest'
40 dest.mkdir()
41 src = partial / 'src'
42 src.mkdir()
43 sub = src / 'sub'
44 sub.mkdir()
45 (src / 'f0').write_text("NEW-PUSHED-CONTENT\n")
46
47 base_rel = str(base).lstrip('/')
48 conf = write_daemon_conf(
49 [('everything', {'path': '/', 'read only': 'no', 'exclude': str(secret) + '/'})])
50 url = start_test_daemon(conf, DAEMON_PORT)
51
52 subprocess.run(
53 rsync_argv(
54 '-a',
55 f'--partial-dir=/{base_rel}/partial-dir-daemon/src/sub/../../../secret/',
56 f'{src}/',
57 f'{url}everything/{base_rel}/partial-dir-daemon/dest',
58 ),
59 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
60
61 after = victim.read_text() if victim.exists() else None
62 if after is None:
63 test_fail("escaped: a --partial-dir '..' traversal DELETED the excluded victim")
64 if after != SECRET:
65 test_fail("escaped: a --partial-dir '..' traversal OVERWROTE the excluded victim "
66 f"({victim} is now {after!r})")
67 print("did not escape")