Debian Codegraph @

testsuite/rsync-ssl-stunnel-hostname-check_test.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Finding (codex scan, CWE-295 improper certificate validation): in stunnel mode
3 # rsync-ssl emitted "verifyChain = yes" + "CAfile" but NO "checkHost" directive,
4 # so the TLS session validated the certificate CHAIN but not its HOSTNAME. An
5 # active network attacker holding a cert that chains to the configured CA but is
6 # issued for a DIFFERENT name could MITM the rsync-over-SSL connection. Fix:
7 # emit "checkHost = $hostname" whenever the chain is verified, mirroring the
8 # openssl backend's -verify_hostname. RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 keeps chain
9 # verification but skips the identity check (restores 3.4.x stunnel behaviour).
10 #
11 # rsync-ssl feeds the generated stunnel config on fd 10; a fake stunnel captures
12 # it and we inspect the directives.
13
14 import os
15 import subprocess
16
17 from rsyncfns import SCRATCHDIR, SRCDIR, rmtree, test_fail
18
19 base = SCRATCHDIR / 'rsync-ssl-stunnel-host'
20 rmtree(base)
21 base.mkdir(parents=True)
22
23 config_capture = base / 'stunnel_config'
24 fake_stunnel = base / 'fake_stunnel'
25 # bash, not /bin/sh: dash cannot redirect from fd >9; via env for the BSDs' bash.
26 fake_stunnel.write_text(f"#!/usr/bin/env bash\ncat <&10 > {config_capture} 2>/dev/null\nexit 0\n")
27 fake_stunnel.chmod(0o755)
28 ca = base / 'ca.pem'
29 ca.write_text("dummy-ca\n")
30
31 helper = ['--HELPER', 'localhost', 'rsync', '--server', '--daemon', '.']
32 base_env = {**os.environ, 'RSYNC_SSL_TYPE': 'stunnel',
33 'RSYNC_SSL_STUNNEL': str(fake_stunnel)}
34 for v in ('RSYNC_SSL_CA_CERT', 'RSYNC_SSL_ALLOW_INSECURE_STUNNEL',
35 'RSYNC_SSL_SKIP_HOSTNAME_CHECK'):
36 base_env.pop(v, None)
37
38
39 def run(env):
40 if config_capture.exists():
41 config_capture.unlink()
42 subprocess.run(['bash', str(SRCDIR / 'rsync-ssl')] + helper, env=env,
43 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
44 return config_capture.read_text() if config_capture.exists() else ''
45
46
47 # --- Part 1: CA configured, default -> chain AND hostname are verified.
48 cfg = run({**base_env, 'RSYNC_SSL_CA_CERT': str(ca)})
49 if 'connect = localhost' not in cfg:
50 test_fail(f"premise: fake stunnel did not capture a config:\n{cfg!r}")
51 if 'verifyChain = yes' not in cfg:
52 test_fail(f"a CA-configured stunnel config must verify the chain:\n{cfg}")
53 if 'checkHost = localhost' not in cfg:
54 test_fail("CA-configured stunnel config is missing the hostname binding "
55 f"(checkHost = localhost):\n{cfg}")
56
57 # --- Part 2: explicit insecure opt-out, no CA -> no verification at all.
58 cfg = run({**base_env, 'RSYNC_SSL_CA_CERT': '',
59 'RSYNC_SSL_ALLOW_INSECURE_STUNNEL': '1'})
60 if 'checkHost' in cfg:
61 test_fail(f"insecure (no-CA) stunnel config must not emit checkHost:\n{cfg}")
62
63 # --- Part 3: chain-only opt-out -> chain verified, hostname check skipped.
64 cfg = run({**base_env, 'RSYNC_SSL_CA_CERT': str(ca),
65 'RSYNC_SSL_SKIP_HOSTNAME_CHECK': '1'})
66 if 'verifyChain = yes' not in cfg:
67 test_fail(f"SKIP_HOSTNAME_CHECK must keep chain verification:\n{cfg}")
68 if 'checkHost' in cfg:
69 test_fail("RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 must drop the hostname binding "
70 f"(no checkHost):\n{cfg}")
71
72 print("rsync-ssl-stunnel-hostname-check: stunnel binds the cert to the host when "
73 "verifying a chain, and SKIP_HOSTNAME_CHECK opts back to chain-only")