Debian Codegraph @

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

raw symbols SCIP
1 import os
2 import subprocess
3
4 from rsyncfns import (
5 SCRATCHDIR, forced_protocol, rmtree, rsync_argv, start_test_daemon,
6 test_fail, write_daemon_conf,
7 )
8
9 DAEMON_PORT = 12961
10 SECRET = "PROTECTED-OUTSIDE-MODULE\n"
11 PUSHED = "NEW\n"
12 OLD_DEST = "OLD-DESTINATION-FILE-CONTENT\n"
13
14 base = SCRATCHDIR / 'partialdirdaemon'
15 rmtree(base)
16 base.mkdir()
17
18 mod = base / 'mod'
19 secret = base / 'secret'
20 if secret.exists():
21 rmtree(secret)
22 secret.mkdir(parents=True)
23
24 victim = secret / 'f0'
25 victim.write_text(SECRET) # a file the partial must not clobber
26 mod.mkdir()
27 dest = mod / 'f0'
28 dest.write_text(OLD_DEST)
29
30 blink = mod / 'blink' # euid-owned symlink -> outside module
31 os.symlink(f'{secret}', blink)
32
33 src = base / 'src'
34 src.mkdir()
35 (src / 'f0').write_text(PUSHED)
36
37 conf = write_daemon_conf(
38 [('mod', {'path': str(mod), 'read only': 'no'})])
39 url = start_test_daemon(conf, DAEMON_PORT)
40
41 proc = subprocess.run(
42 rsync_argv('-a', '--delay-updates', '--partial-dir=/blink',
43 f'{src}/', f'{url}mod/'),
44 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
45
46 # The security oracle -- the outside-module victim must never be clobbered --
47 # holds at every protocol and is checked unconditionally below. The stronger
48 # "daemon actively rejects the forced --partial-dir operand" behavior only
49 # applies at protocol 30+; at protocol 29 the operand is handled differently and
50 # the transfer completes normally (dest replaced, victim still untouched), so
51 # gate just that secondary check.
52 proto = forced_protocol()
53 dest_after = dest.read_text() if dest.exists() else None
54 if proto is None or proto >= 30:
55 if proc.returncode == 0 or dest_after != OLD_DEST:
56 test_fail(
57 "the daemon did not reject the forced --partial-dir staging path: "
58 f"rsync exited {proc.returncode} and {dest} is now {dest_after!r}. "
59 "If the operand were ignored, the upload would replace the destination "
60 f"with {PUSHED!r}.")
61 else:
62 # At protocol 29 the operand is handled differently: the transfer must still
63 # complete normally (no escape) rather than fail for an unrelated reason, so
64 # require the known-good outcome instead of skipping the check entirely.
65 if proc.returncode != 0 or dest_after != PUSHED:
66 test_fail(
67 "protocol-29 --partial-dir run did not complete normally: rsync exited "
68 f"{proc.returncode} and {dest} is {dest_after!r} (expected a clean "
69 f"transfer replacing it with {PUSHED!r})")
70
71 after = victim.read_text() if victim.exists() else None
72 if after != SECRET:
73 test_fail(
74 "the daemon followed a --partial-dir symlink outside the module: "
75 f"{victim} is now {after!r} (a peer backed up over a file the module "
76 "does not serve).")
77 print("daemon confines a peer-supplied --partial-dir symlink to the served set")