Debian Codegraph @

testsuite/filter-merge-content-echo_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """A merge file's contents must not come back in a filter syntax error.
3
4 A per-directory merge rule names a file the peer chooses and that travels over
5 the protocol, so no argument of ours ever mentions it. Echoing an unparsable
6 line back made the filter parser a read-any-line oracle: every line that is not
7 valid filter syntax was returned verbatim to the client. The diagnostic must
8 say where the bad rule is, not what it says.
9
10 Rules that came from an ARGUMENT are still echoed -- that text is the user's
11 own and hiding it would only make ordinary typos harder to fix.
12 """
13
14 import os
15 import subprocess
16
17 from rsyncfns import SCRATCHDIR, makepath, rmtree, rsync_argv
18
19 SECRET = 'TOP-SECRET-PASSWORD-abc123'
20
21 base = SCRATCHDIR / 'filter-merge-content-echo'
22 rmtree(base)
23 src = base / 'src'
24 dest = base / 'dest'
25 makepath(src, dest)
26 (src / 'f').write_bytes(b'hi\n')
27
28 # Not valid filter syntax, so the parser reports it -- this stands in for any
29 # non-filter file on the server: a key, a password store, a config.
30 secret = base / 'secret'
31 secret.write_text(SECRET + '\n')
32
33 # The reachable shape: a per-dir merge file whose own contents name the target.
34 (src / '.rsync-filter').write_text(': ../secret\n')
35 got = subprocess.run(rsync_argv('-r', '-F', f'{src}/', f'{dest}/'),
36 capture_output=True, text=True, timeout=30)
37 out = got.stdout + got.stderr
38 assert got.returncode != 0, (
39 f'the unparsable merge rule was accepted, so this test no longer '
40 f'exercises the error path: rc={got.returncode}, out={out!r}')
41 assert SECRET not in out, (
42 'the contents of a merged file were echoed back in the filter error, so a '
43 f'peer that can choose the merge path can read any line: out={out!r}')
44 # The merged file's own PATH came from a rule too (the .rsync-filter names it),
45 # so it cannot be printed either -- but the diagnostic still has to say enough
46 # to be actionable.
47 assert 'a file read earlier' in out, (
48 'the error says nothing at all about where the bad rule was, leaving no '
49 f'way to act on it: out={out!r}')
50
51 # Same for a merge file reached by an explicit argument: the text is still file
52 # content, and --filter='. FILE' is how an operator-supplied list is read.
53 (base / 'rules').write_text(SECRET + '\n')
54 got = subprocess.run(
55 rsync_argv('-r', f'--filter=. {base}/rules', f'{src}/', f'{dest}/'),
56 capture_output=True, text=True, timeout=30)
57 out = got.stdout + got.stderr
58 assert got.returncode != 0 and SECRET not in out, (
59 f'a --filter=. merge file echoed its contents: rc={got.returncode}, '
60 f'out={out!r}')
61 # This one WAS named by an argument, so its own path is the user's own text and
62 # is still shown -- the location must not be thrown away wholesale.
63 assert 'rules line 1' in out, (
64 'a merge file named by an argument should still be located precisely: '
65 f'out={out!r}')
66
67 # A file merged FROM a file cannot show its own path, but the place that named
68 # it can be shown, and is what the user needs to fix.
69 (base / 'outer-known').write_text(f'. {base}/inner-bad\n')
70 (base / 'inner-bad').write_text(SECRET + '\n')
71 got = subprocess.run(
72 rsync_argv('-r', f'--filter=. {base}/outer-known', f'{src}/', f'{dest}/'),
73 capture_output=True, text=True, timeout=30)
74 out = got.stdout + got.stderr
75 assert SECRET not in out and 'inner-bad' not in out, (
76 f'the indirectly named merge file leaked its own path: out={out!r}')
77 assert 'a file named at' in out and 'outer-known line 1' in out, (
78 'the error does not point at the rule that named the bad file, which is '
79 f'the only thing the user can act on: out={out!r}')
80
81 # ...but a rule given AS an argument must still be shown, or an ordinary typo
82 # becomes undiagnosable.
83 got = subprocess.run(
84 rsync_argv('-r', '--filter=Zbogus-rule-text', f'{src}/', f'{dest}/'),
85 capture_output=True, text=True, timeout=30)
86 out = got.stdout + got.stderr
87 assert got.returncode != 0 and 'Zbogus-rule-text' in out, (
88 'a bad rule given on the command line is no longer echoed, which is a '
89 f'usability regression rather than a leak: rc={got.returncode}, out={out!r}')
90
91 # An ordinary, VALID merge file must still work -- the diagnostic change must
92 # not have broken merge parsing itself.
93 (src / '.rsync-filter').write_text('- *.tmp\n')
94 (src / 'keep.txt').write_bytes(b'keep\n')
95 (src / 'drop.tmp').write_bytes(b'drop\n')
96 ok = subprocess.run(rsync_argv('-r', '-F', f'{src}/', f'{dest}/'),
97 capture_output=True, text=True, timeout=30)
98 assert ok.returncode == 0, f'a valid merge file failed: out={ok.stderr!r}'
99 assert (dest / 'keep.txt').exists() and not (dest / 'drop.tmp').exists(), (
100 'the valid merge rule was not applied, so the parser is broken')
101
102 # A merge rule INSIDE a merge file names its own file, so the failed-open
103 # message echoed text that came out of the file being read. A line of the
104 # target that happens to parse as a merge rule leaks through it.
105 (base / 'outer').write_text(f'. {base}/nested\n')
106 (base / 'nested').write_text(f'. {SECRET}\n')
107 got = subprocess.run(
108 rsync_argv('-r', f'--filter=. {base}/outer', f'{src}/', f'{dest}/'),
109 capture_output=True, text=True, timeout=30)
110 out = got.stdout + got.stderr
111 assert got.returncode != 0 and SECRET not in out, (
112 'a nested merge file named by file content was echoed in the failed-open '
113 f'message: rc={got.returncode}, out={out!r}')
114
115 # The reported location must be the file the bad rule is actually IN. fname
116 # can point into parse_merge_name()'s static buffer, which a merge rule inside
117 # the same file overwrites, so the error blamed the nested file instead.
118 (base / 'child').write_text('- harmless\n')
119 (base / 'parent').write_text(f'. {base}/child\nZbad-in-parent\n')
120 got = subprocess.run(
121 rsync_argv('-r', f'--filter=. {base}/parent', f'{src}/', f'{dest}/'),
122 capture_output=True, text=True, timeout=30)
123 out = got.stdout + got.stderr
124 assert 'parent line 2' in out, (
125 'the error blames the wrong file or line for a rule that follows a nested '
126 f'merge, so the location it prints cannot be trusted: out={out!r}')
127
128 # A CRLF pair is one line ending, not two.
129 (base / 'crlf').write_bytes(b'- harmless\r\nZbad-on-line-2\r\n')
130 got = subprocess.run(
131 rsync_argv('-r', f'--filter=. {base}/crlf', f'{src}/', f'{dest}/'),
132 capture_output=True, text=True, timeout=30)
133 out = got.stdout + got.stderr
134 assert 'crlf line 2' in out, f'CRLF miscounted the line: out={out!r}'
135
136 # The FILTER2 traces print rule text as well, so a word-split merge -- whose
137 # every word becomes its own no-prefix rule -- had its contents echoed by
138 # add_rule() with nothing failing to parse at all.
139 (src / '.rsync-filter').write_text(':w- ../secret\n')
140 got = subprocess.run(
141 rsync_argv('-r', '-F', '--debug=FILTER2', f'{src}/', f'{dest}/'),
142 capture_output=True, text=True, timeout=30)
143 out = got.stdout + got.stderr
144 assert SECRET not in out, (
145 'the filter debug trace echoed the contents of a merged file, so the '
146 f'oracle survives with --debug=FILTER2: out={out!r}')
147
148 # The MATCH traces name the pattern that acted, and a pattern read from a merge
149 # file is that file's text. This one is not really debug-gated: plain -vv
150 # raises the filter level far enough for a sender or generator to print it, so
151 # a stock client reaches it with no --debug at all.
152 #
153 # The pattern is written as a glob whose LITERAL text cannot appear anywhere
154 # else -- the file it matches is named without the brackets -- so finding it in
155 # the output can only mean the rule text was echoed.
156 PATTERN_TEXT = 'sec[r]et-marker-9x'
157 MATCHED_NAME = 'secret-marker-9x'
158 rmtree(src)
159 makepath(src)
160 (src / 'f').write_bytes(b'keep\n')
161 (src / MATCHED_NAME).write_bytes(b'hidden\n')
162 (src / '.rsync-filter').write_text('- ' + PATTERN_TEXT + '\n')
163 got = subprocess.run(rsync_argv('-r', '-F', '-vv', f'{src}/', f'{dest}/'),
164 capture_output=True, text=True, timeout=30)
165 out = got.stdout + got.stderr
166 assert got.returncode == 0, f'the -vv transfer failed: out={out!r}'
167 assert 'because of' in out, (
168 f'no match was reported, so this case exercises nothing: out={out!r}')
169 assert PATTERN_TEXT not in out, (
170 'the match trace echoed a pattern that came from a merge file, which plain '
171 f'-vv is enough to reach: out={out!r}')
172 assert not (dest / MATCHED_NAME).exists(), (
173 'the rule did not actually hide the file, so the trace above was not the '
174 'one this checks')
175
176 # A deferred per-dir merge (":" rule) is parsed long after the file that named
177 # it was read, so the parse-time context is gone by then and the redactions
178 # have to come off the RULE instead. Two leaks lived here: an over-long
179 # deferred name needed no verbosity at all, and -vvv printed the name plus
180 # whether it opened.
181 rmtree(src)
182 makepath(src / 'sub')
183 (src / 'sub' / 'f').write_bytes(b'x\n')
184 # The name has to survive parse_filter_str's own MAXPATHLEN check and only
185 # overflow once the scanned directory is prepended, which is a narrow window
186 # whose position depends on the scratch path and the platform's MAXPATHLEN --
187 # so search for it rather than hard-coding a length.
188 maxpath = os.pathconf(str(base), 'PC_PATH_MAX')
189 overflowed = False
190 for slack in range(4, 80, 4):
191 pad = maxpath - len(str(src)) - slack
192 if pad < 32:
193 continue
194 (src / '.rsync-filter').write_text(': sub/DEFERRED-SECRET-' + 'Q' * pad + '\n')
195 got = subprocess.run(rsync_argv('-r', '-F', f'{src}/', f'{dest}/'),
196 capture_output=True, text=True, timeout=30)
197 out = got.stdout + got.stderr
198 assert 'DEFERRED-SECRET' not in out, (
199 'an over-long deferred merge name was echoed, with no verbosity '
200 f'needed: out={out!r}')
201 if 'merge-file name overflows' in out:
202 overflowed = True
203 break
204 assert overflowed, (
205 'no length reached the deferred merge-name overflow, so that path was '
206 'never exercised -- widen the search')
207
208 DEFERRED_NAME = 'named-from-f[i]le-x9' # the brackets exist only in the file
209 (src / '.rsync-filter').write_text(': ' + DEFERRED_NAME + '\n')
210 got = subprocess.run(rsync_argv('-r', '-F', '-vvv', f'{src}/', f'{dest}/'),
211 capture_output=True, text=True, timeout=30)
212 out = got.stdout + got.stderr
213 assert DEFERRED_NAME not in out, (
214 'the deferred per-dir merge name -- which came from a file -- was printed '
215 f'by the -vvv trace, along with whether it opened: out={out!r}')
216
217 # Details ABOUT a file-derived rule leak too, so they go through the same
218 # helper: the trailing-whitespace warning is computed from the rule's last
219 # byte, and would otherwise tell a peer whether a hidden rule ended in a space.
220 rmtree(src)
221 makepath(src)
222 (src / 'f').write_bytes(b'x\n')
223 (src / '.rsync-filter').write_text('- trailing-ws-probe \n')
224 got = subprocess.run(
225 rsync_argv('-r', '-F', '--debug=FILTER1', f'{src}/', f'{dest}/'),
226 capture_output=True, text=True, timeout=30)
227 out = got.stdout + got.stderr
228 assert 'trailing-ws-probe' not in out and 'CAUTION' not in out, (
229 'a property of a file-derived rule (its trailing whitespace) was reported, '
230 f'which tells the peer about text it cannot otherwise see: out={out!r}')
231
232 # ...but for the user's OWN rule the warning is exactly what they need.
233 got = subprocess.run(
234 rsync_argv('-r', '--debug=FILTER1', '--filter=- my-own-rule ',
235 f'{src}/', f'{dest}/'),
236 capture_output=True, text=True, timeout=30)
237 out = got.stdout + got.stderr
238 assert 'CAUTION' in out and 'my-own-rule' in out, (
239 'the trailing-whitespace warning was lost for an argument-supplied rule, '
240 f'which is a usability regression: out={out!r}')
241
242 # An over-long ARGUMENT rule must still be reported at full length -- the
243 # redaction helper must not quietly truncate the user's own text.
244 long_arg = 'ARGLONG-' + 'Q' * (maxpath + 200)
245 got = subprocess.run(rsync_argv('-r', f'--filter=- {long_arg}', f'{src}/', f'{dest}/'),
246 capture_output=True, text=True, timeout=30)
247 out = got.stdout + got.stderr
248 assert 'discarding over-long filter' in out, f'expected the over-long path: out={out!r}'
249 assert out.count('Q') > maxpath, (
250 'the over-long argument rule was truncated more aggressively than before, '
251 f'losing the user\'s own text: {out.count("Q")} Qs of {maxpath + 200}')
252
253 # The ":e" modifier synthesizes a SECOND rule -- an exclude for the merge
254 # file's own basename -- by hand, with no template to inherit from. Built
255 # that way it carried no provenance, so once parsing finished it looked
256 # argument-origin and the match trace printed a merge file's text verbatim.
257 # Plain -vv reaches this; no --debug is involved.
258 #
259 # The pattern is a glob whose literal text cannot appear anywhere else: the
260 # file it matches is named without the brackets, so finding the bracketed
261 # form in the output can only mean the rule text was echoed.
262 SELF_PATTERN = 'excl-self-[x]9'
263 SELF_MATCHED = 'excl-self-x9'
264 rmtree(src)
265 makepath(src)
266 (src / 'f').write_bytes(b'keep\n')
267 (src / SELF_MATCHED).write_bytes(b'hidden\n')
268 (src / '.rsync-filter').write_text(':e ' + SELF_PATTERN + '\n')
269 got = subprocess.run(rsync_argv('-r', '-F', '-vv', f'{src}/', f'{dest}/'),
270 capture_output=True, text=True, timeout=30)
271 out = got.stdout + got.stderr
272 assert got.returncode == 0, f'the :e transfer failed: out={out!r}'
273 assert 'because of' in out, (
274 f'no match was reported, so this case exercises nothing: out={out!r}')
275 assert SELF_PATTERN not in out, (
276 'the exclude-self rule that ":e" builds by hand echoed a merge file\'s '
277 f'text at plain -vv, so it carries no provenance: out={out!r}')
278
279 # A per-directory merge file's fname points INTO dirbuf, which is cut back to
280 # the directory before the location is recorded. Copying it after the cut
281 # left the location naming the DIRECTORY, losing the filename -- no leak, but
282 # it breaks the "redact what, keep where" bargain the rest of this relies on.
283 rmtree(src)
284 makepath(src)
285 (src / 'f').write_bytes(b'x\n')
286 (src / '.rsync-filter').write_text('Zbad-perdir\n')
287 got = subprocess.run(rsync_argv('-r', '-F', f'{src}/', f'{dest}/'),
288 capture_output=True, text=True, timeout=30)
289 out = got.stdout + got.stderr
290 assert got.returncode != 0 and 'Zbad-perdir' not in out, (
291 f'the per-dir rule text was echoed: rc={got.returncode}, out={out!r}')
292 assert '.rsync-filter line 1' in out, (
293 'the location names the directory instead of the merge file, so it no '
294 f'longer says which file to fix: out={out!r}')