| 1 |
|
| 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 |
|
| 29 |
|
| 30 |
secret = base / 'secret' |
| 31 |
secret.write_text(SECRET + '\n') |
| 32 |
|
| 33 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 52 |
|
| 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 |
|
| 62 |
|
| 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 |
|
| 68 |
|
| 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 |
|
| 82 |
|
| 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 |
|
| 92 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 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 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
rmtree(src) |
| 182 |
makepath(src / 'sub') |
| 183 |
(src / 'sub' / 'f').write_bytes(b'x\n') |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 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' |
| 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 |
|
| 218 |
|
| 219 |
|
| 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 |
|
| 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 |
|
| 243 |
|
| 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 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 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 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 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}') |