Debian Codegraph @

testsuite/rrsync-files-from-stdin_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A pull with a local --files-from must work through rrsync.
3
4 `rsync -a --files-from=LIST host:. dest/` does not send the list file to the
5 server. It sends the literal `--files-from=-` and streams the names down the
6 protocol connection, so `-` reaches rrsync as an option value meaning "read the
7 list from stdin" -- it is a sentinel, not a pathname.
8
9 88cee089 started inode-pinning every checked option value, and `--files-from`
10 is checked (long_opts['files-from'] == 3). Pinning `-` means realpath()ing and
11 opening a file named "-" in the restricted dir, which does not exist, so rrsync
12 kills the connection before rsync ever runs:
13
14 rrsync error: post-realpath open failed (race detected): - No such file...
15
16 That breaks every --files-from pull through a restricted account. v3.4.4
17 delivers the file.
18
19 A list file that really is a pathname must still be validated and pinned, so
20 the second half checks that an out-of-tree --files-from is still refused.
21 """
22
23 import os
24 import shlex
25 import subprocess
26
27 from rsyncfns import (
28 RSYNC, SCRATCHDIR, makepath, patched_rrsync, rmtree, rsync_argv, test_fail, rsync_path_arg,
29 )
30
31 DATA = 'FILES-FROM-CONTENT\n'
32
33 base = SCRATCHDIR / 'rrsync-files-from'
34 rmtree(base)
35 restricted = base / 'restricted'
36 dest = base / 'dest'
37 outside = base / 'outside'
38 makepath(restricted, dest, outside)
39
40 (restricted / 'f1').write_text(DATA)
41 (restricted / 'f2').write_text(DATA)
42 (outside / 'list').write_text('f1\n')
43
44 listfile = base / 'list'
45 listfile.write_text('f1\nf2\n')
46
47 # $RSYNC may carry a wrapper (valgrind, --protocol=N); rrsync wants a single
48 # executable, so hand it a shim that re-expands the whole command line.
49 shim = base / 'rsync-shim'
50 shim.write_text('#!/bin/sh\nexec ' + rsync_path_arg(RSYNC) + ' "$@"\n')
51 shim.chmod(0o755)
52
53 rrsync = patched_rrsync(base, rsync_path=str(shim))
54
55 rsh = base / 'fake-rsh'
56 rsh.write_text(
57 '#!/bin/sh\n'
58 'shift\n'
59 'SSH_ORIGINAL_COMMAND="$*"\n'
60 'export SSH_ORIGINAL_COMMAND\n'
61 'exec %s %s\n' % (shlex.quote(str(rrsync)), shlex.quote(str(restricted))))
62 rsh.chmod(0o755)
63
64
65 def transfer(*args):
66 return subprocess.run(rsync_argv('-a', '-e', str(rsh), *args),
67 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
68 text=True)
69
70
71 # --- the regression: a local --files-from sends "--files-from=-" -------------
72 proc = transfer('--files-from=' + str(listfile), 'dummy:.', str(dest) + '/')
73 ctx = f'rc={proc.returncode}, output={proc.stdout.strip()[:300]!r}'
74
75 for name in ('f1', 'f2'):
76 got = dest / name
77 if not got.is_file():
78 test_fail(f'--files-from pull did not deliver {name} ({ctx})')
79 if got.read_text() != DATA:
80 test_fail(f'--files-from pull delivered wrong content for {name} ({ctx})')
81 if proc.returncode != 0:
82 test_fail(f'--files-from pull delivered the files but failed ({ctx})')
83
84 # --- control: a --files-from that IS a pathname stays confined ---------------
85 # The sentinel exemption must be for the exact string "-" only, not a general
86 # hole in the checking of this option.
87 def rrsync_direct(files_from):
88 # rrsync insists on the "--sender ..." shape sshd hands it; get that right
89 # or it dies at the syntax check without ever looking at the option value,
90 # which would make this control pass no matter what.
91 return subprocess.run(
92 [str(rrsync), '-ro', '-no-lock', str(restricted)],
93 env={**os.environ,
94 'SSH_ORIGINAL_COMMAND':
95 'rsync --server --sender -logDtpre.iLsfxC '
96 '--files-from=%s . .' % files_from},
97 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
98
99
100 # Prove the command shape itself is accepted, so a rejection below is really
101 # about the pathname.
102 proc = rrsync_direct(restricted / 'f1')
103 for wrong in ('invalid rsync-command syntax', 'does not run rsync'):
104 if wrong in proc.stdout:
105 test_fail('the control command shape is wrong, so it never reaches the '
106 f'--files-from check ({wrong!r}): '
107 f'output={proc.stdout.strip()[:300]!r}')
108
109 proc = rrsync_direct(outside / 'list')
110 if proc.returncode == 0:
111 test_fail('an out-of-tree --files-from pathname was accepted: '
112 f'output={proc.stdout.strip()[:300]!r}')
113 for wrong in ('invalid rsync-command syntax', 'does not run rsync'):
114 if wrong in proc.stdout:
115 test_fail('the out-of-tree --files-from was rejected for the wrong '
116 f'reason ({wrong!r}, not the pathname): '
117 f'{proc.stdout.strip()[:300]!r}')
118
119 print('rrsync passes the --files-from stdin sentinel through; '
120 'a real out-of-tree list file is still refused')