Debian Codegraph @

testsuite/rsync-ssl-hostname-validation_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Finding [20] (CWE-88 argument injection): rsync-ssl expands an untrusted SSL
3 # hostspec into the openssl/gnutls command line unquoted (rsync-ssl:146) and into
4 # the stunnel heredoc config (rsync-ssl:154) with no character policy. A hostname
5 # containing whitespace / newline / control characters therefore injects extra
6 # TLS options or stunnel directives -- a forced connection target, a verification
7 # bypass, or stunnel config injection. Fix: validate_ssl_hostname rejects such
8 # hostnames before any helper is invoked (rsync-ssl:138).
9 #
10 # Part 1 demonstrates the injection against an UNFIXED copy of rsync-ssl plus a
11 # fake openssl that records its argv: the crafted host's payload lands as a
12 # separate openssl argument. Part 2 is the fix: the shipped rsync-ssl rejects the
13 # host string before exec'ing the helper.
14
15 import os
16 import shlex
17 import re
18 import subprocess
19
20 from rsyncfns import SCRATCHDIR, SRCDIR, rmtree, test_fail
21
22 base = SCRATCHDIR / 'rsync-ssl-hostname'
23 rmtree(base)
24 base.mkdir(parents=True)
25
26 argv_capture = base / 'openssl_argv'
27 fake_openssl = base / 'fake_openssl'
28 fake_openssl.write_text(f"#!/bin/sh\nprintf '%s\\n' \"$@\" > {shlex.quote(str(argv_capture))}\nexit 0\n")
29 fake_openssl.chmod(0o755)
30
31 # An unfixed rsync-ssl: drop the validate_ssl_hostname guard so the host string
32 # reaches the helper command line as it did before the fix.
33 unfixed = base / 'rsync-ssl-unfixed'
34 unfixed.write_text(re.sub(r'(?m)^(\s*)validate_ssl_hostname .*$',
35 r'\1: # hostname guard removed for this regression test',
36 (SRCDIR / 'rsync-ssl').read_text()))
37 unfixed.chmod(0o755)
38
39 EVIL_HOST = 'localhost INJECTED_OPENSSL_ARG'
40 env = {**os.environ, 'RSYNC_SSL_TYPE': 'openssl',
41 'RSYNC_SSL_OPENSSL': str(fake_openssl), 'RSYNC_SSL_CA_CERT': '/dev/null'}
42 helper = ['--HELPER', EVIL_HOST, 'rsync', '--server', '--daemon', '.']
43
44 # --- Part 1: the unfixed helper injects the payload into the openssl argv.
45 subprocess.run(['bash', str(unfixed)] + helper, env=env,
46 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
47 captured = argv_capture.read_text().splitlines() if argv_capture.exists() else []
48 if 'INJECTED_OPENSSL_ARG' not in captured:
49 test_fail("premise: the crafted hostname did not inject a separate openssl "
50 f"argument (captured argv: {captured})")
51
52 # --- Part 2: the shipped rsync-ssl rejects the injecting host string.
53 proc = subprocess.run(['bash', str(SRCDIR / 'rsync-ssl')] + helper, env=env,
54 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
55 if proc.returncode == 0 or 'invalid rsync-ssl hostname' not in proc.stdout:
56 test_fail("rsync-ssl did NOT reject the argument-injecting hostname:\n" + proc.stdout)
57
58 print("rsync-ssl-hostname-validation: a control-char hostspec injects helper "
59 "arguments; rsync-ssl rejects it")