Debian Codegraph @

testsuite/rrsync-copy-unsafe-links-denied_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Finding [8] (CWE-59): support/rrsync leaves --copy-unsafe-links enabled in a
3 # restricted (non-"/") directory. A restricted SSH user who can request
4 # --copy-unsafe-links can have the spawned rsync sender dereference a symlink
5 # that points OUTSIDE the restricted tree and copy the *referent's content* out,
6 # exfiltrating files readable by the rrsync account but outside the allowed
7 # policy directory.
8 #
9 # Part 1 demonstrates the danger of the option itself: a symlink inside the
10 # served tree pointing at an outside secret is dereferenced by --copy-unsafe-
11 # links, so the outside content lands in the puller's copy. Part 2 is the fix:
12 # a restricted rrsync now refuses --copy-unsafe-links, so the part-1 exfiltration
13 # cannot be requested through the wrapper. Runs at any uid.
14
15 import os
16 import subprocess
17
18 from rsyncfns import (
19 SCRATCHDIR, rmtree, rsync_argv, run_rrsync_denied, test_fail,
20 )
21
22 SECRET = 'OUTSIDE-SECRET-readable-by-the-rrsync-account\n'
23
24 base = SCRATCHDIR / 'rrsync-copy-unsafe'
25 rmtree(base)
26 served = base / 'restricted'
27 outside = base / 'outside'
28 served.mkdir(parents=True)
29 outside.mkdir()
30 (outside / 'secret').write_text(SECRET)
31 # An "unsafe" symlink: it lives inside the served tree but its target escapes it.
32 os.symlink('../outside/secret', served / 'leak')
33
34 # --- Part 1: --copy-unsafe-links dereferences the escaping symlink (the danger).
35 dest = base / 'dest'
36 dest.mkdir()
37 subprocess.run(rsync_argv('-a', '--copy-unsafe-links', f'{served}/', f'{dest}/'),
38 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
39 leaked = dest / 'leak'
40 if not (leaked.is_file() and not leaked.is_symlink() and leaked.read_text() == SECRET):
41 test_fail("premise check failed: --copy-unsafe-links should turn the escaping "
42 "symlink into a regular file holding the outside secret")
43
44 # --- Part 2: a restricted rrsync must refuse --copy-unsafe-links, so the
45 # exfiltration shown in part 1 cannot be requested through the wrapper.
46 run_rrsync_denied('rsync --server --sender --copy-unsafe-links . .',
47 'option --copy-unsafe-links has been disabled')
48
49 print("rrsync-copy-unsafe-links: --copy-unsafe-links exfiltrates outside symlink "
50 "targets; restricted rrsync refuses it")