Debian Codegraph @

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

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