Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Daemon destination ".." traversal into the module's excluded subtree, on a
3 # `path = /` module. (Reported by Omar Elsayed.) The peer pushes to a dest that
4 # climbs out of an in-module dir back into the excluded subtree via "..".
5 # Sandbox-anchored under SCRATCHDIR (Omar's original wrote to the real /secret),
6 # so it runs unprivileged; the module still serves "/" so the traversal is real.
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 = 12963
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-dest-dir-daemon'
28 rmtree(base)
29 base.mkdir()
30
31 dest = base / 'dest-dir-daemon'
32 dest.mkdir()
33
34 secret = base / 'secret' # excluded subtree
35 secret.mkdir()
36 victim = secret / 'f0'
37 victim.write_text(SECRET)
38
39 src = dest / 'src'
40 src.mkdir()
41 sub = src / 'sub'
42 sub.mkdir()
43 (src / 'f0').write_text("NEW-PUSHED-CONTENT\n")
44
45 base_rel = str(base).lstrip('/')
46 conf = write_daemon_conf(
47 [('everything', {'path': '/', 'read only': 'no', 'exclude': str(secret) + '/'})])
48 url = start_test_daemon(conf, DAEMON_PORT)
49
50 subprocess.run(
51 rsync_argv(
52 '-a',
53 f'{src}/',
54 f'{url}everything/{base_rel}/dest-dir-daemon/src/sub/../../../secret/',
55 ),
56 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
57
58 after = victim.read_text() if victim.exists() else None
59 if after != SECRET:
60 test_fail("escaped: a dest '..' traversal reached the excluded subtree "
61 f"({victim} is now {after!r})")
62 print("did not escape")