Debian Codegraph @

testsuite/rsync-ssl-stunnel-ca-required_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Finding [12] (CWE-295 improper certificate validation): in stunnel mode
3 # rsync-ssl builds the stunnel client config with NO CA / verify directives when
4 # RSYNC_SSL_CA_CERT is unset (rsync-ssl:88-99 leaves verify=""), so the TLS
5 # session validates no server certificate. An active network attacker can then
6 # MITM the rsync-over-SSL connection -- reading and modifying transferred file
7 # data and the daemon auth challenge/response. Fix: rsync-ssl refuses stunnel
8 # mode without a CA unless RSYNC_SSL_ALLOW_INSECURE_STUNNEL=1 (rsync-ssl:140).
9 #
10 # Part 1 demonstrates the danger: with the explicit insecure opt-out and no CA, a
11 # fake stunnel captures the generated config and it carries no certificate
12 # verification at all -- the MITM-able config the old default produced. Part 2 is
13 # the fix: without the opt-out and without a CA, rsync-ssl refuses to run.
14
15 import os
16 import subprocess
17
18 from rsyncfns import SCRATCHDIR, SRCDIR, rmtree, test_fail
19
20 base = SCRATCHDIR / 'rsync-ssl-stunnel'
21 rmtree(base)
22 base.mkdir(parents=True)
23
24 config_capture = base / 'stunnel_config'
25 fake_stunnel = base / 'fake_stunnel'
26 # rsync-ssl feeds the generated stunnel config on fd 10 (exec ... -fd 10 ...);
27 # bash, not /bin/sh, since dash cannot redirect from fd >9 -- and via env, since
28 # the BSDs keep bash in /usr/local/bin, not /bin.
29 fake_stunnel.write_text(f"#!/usr/bin/env bash\ncat <&10 > {config_capture} 2>/dev/null\nexit 0\n")
30 fake_stunnel.chmod(0o755)
31
32 helper = ['--HELPER', 'localhost', 'rsync', '--server', '--daemon', '.']
33 env = {**os.environ, 'RSYNC_SSL_TYPE': 'stunnel', 'RSYNC_SSL_STUNNEL': str(fake_stunnel)}
34 env.pop('RSYNC_SSL_CA_CERT', None)
35
36 # --- Part 1: with the explicit insecure opt-out and no CA, the generated config
37 # has no certificate verification -- the MITM-able connection.
38 env1 = {**env, 'RSYNC_SSL_ALLOW_INSECURE_STUNNEL': '1'}
39 subprocess.run(['bash', str(SRCDIR / 'rsync-ssl')] + helper, env=env1,
40 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
41 cfg = config_capture.read_text() if config_capture.exists() else ''
42 if 'connect = localhost' not in cfg:
43 test_fail(f"premise: fake stunnel did not capture a config:\n{cfg!r}")
44 if 'verifyChain' in cfg or 'CAfile' in cfg:
45 test_fail("premise: expected NO certificate-verification directives in the "
46 f"no-CA stunnel config:\n{cfg}")
47
48 # --- Part 2: without the opt-out and without a CA, rsync-ssl refuses to run, so
49 # the unverified connection shown in part 1 cannot happen by accident.
50 proc = subprocess.run(['bash', str(SRCDIR / 'rsync-ssl')] + helper, env=env,
51 stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
52 if proc.returncode == 0 or 'stunnel requires RSYNC_SSL_CA_CERT' not in proc.stdout:
53 test_fail("rsync-ssl did NOT refuse insecure stunnel mode without a CA:\n" + proc.stdout)
54
55 print("rsync-ssl-stunnel-ca-required: stunnel without a CA yields an unverified "
56 "(MITM-able) config; rsync-ssl refuses it unless explicitly opted out")