Debian Codegraph @

testsuite/remote-shell-newline-escaping_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Finding [27]: rsync's remote-shell argument quoting (SHELL_CHARS in options.c)
3 # omitted the newline/CR command separators, so a newline in a remote path could
4 # inject a separate command into the remote shell. The test rsh support/lsh.sh
5 # runs the remote command via `eval "${@}"`, so an UNescaped newline in the
6 # destination splits off a second line that runs `touch <sentinel>`. The fix
7 # adds \n\r to SHELL_CHARS so such args are quoted and the newline stays literal.
8
9 import os
10 import subprocess
11
12 from rsyncfns import SCRATCHDIR, SRCDIR, rmtree, rsync_argv, test_fail, rsh_cmd
13
14 base = SCRATCHDIR / 'remote-shell-newline'
15 rmtree(base)
16 src = base / 'src'
17 src.mkdir(parents=True)
18 (src / 'f').write_text('payload\n')
19 sentinel = base / 'pwned'
20
21 env = os.environ.copy()
22 env['RSYNC_RSH'] = rsh_cmd(None, '--no-cd')
23 dest = f"lh:{base}/dest\ntouch {sentinel}\n#"
24 subprocess.run(rsync_argv('-a', f'{src}/', dest),
25 stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env)
26
27 # Without the fix, lsh.sh's eval splits on the newline: the first line runs the
28 # server into base/dest and the second runs `touch <sentinel>`. With the newline
29 # quoted the whole string is one literal (invalid) destination, so neither effect
30 # happens. Both checks are filesystem-independent -- they don't depend on whether
31 # a newline-bearing path is creatable, which differs across Linux/macOS/BSD.
32 if sentinel.exists():
33 test_fail("remote-shell newline injection executed the sentinel command")
34 if (base / 'dest').exists():
35 test_fail("remote-shell newline split the destination "
36 "(eval ran the server into base/dest as a separate command)")
37
38 print("remote-shell-newline-escaping: newline in a remote arg is quoted, not executed")