Debian Codegraph @

testsuite/log-control-chars_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Regression for KI-51/52: the log file must escape control characters in a
3 # (possibly attacker-controlled) filename, so an admin who cat's the log can't
4 # have terminal escapes injected (CWE-117). Covers both C0 (< 0x20) and the C1
5 # range (0x80-0x9f, incl CSI 0x9b) on the log path.
6
7 import os
8
9 from rsyncfns import SCRATCHDIR, run_rsync, test_fail, test_skipped
10
11 base = SCRATCHDIR / 'logctl'
12 src = base / 'src'
13 dst = base / 'dst'
14 src.mkdir(parents=True, exist_ok=True)
15 dst.mkdir(parents=True, exist_ok=True)
16 log = base / 'rsync.log'
17
18 srcb = os.fsencode(str(src))
19 made = 0
20 for raw in (b'c0_\x1b_esc', b'c1_\x9b_csi'):
21 try:
22 with open(srcb + b'/' + raw, 'wb') as fh:
23 fh.write(b'x')
24 made += 1
25 except OSError:
26 pass # a filesystem that rejects control-char names (e.g. Cygwin)
27 if made == 0:
28 test_skipped("filesystem rejects control-char filenames")
29
30 run_rsync('-rv', f'--log-file={log}', f'{src}/', f'{dst}/')
31
32 data = log.read_bytes()
33 if b'\x1b' in data:
34 test_fail("raw C0 ESC (0x1b) byte left un-escaped in the log file")
35 if b'\x9b' in data:
36 test_fail("raw C1 CSI (0x9b) byte left un-escaped in the log file")
37 if b'\\#' not in data:
38 test_fail("expected escaped \\#NNN sequences in the log file, found none")
39
40 print(f'log-control-chars: {made} control-char name(s) escaped in the log file')