Debian Codegraph @

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

raw symbols SCIP
1 #!/usr/bin/env python3
2 # Companion to rsync-ssl-stunnel-hostname-check for the openssl backend. The
3 # openssl arm has always passed -verify_hostname $hostname (3.4.x onward), so the
4 # hostname identity is bound by default. This test locks that in AND checks the
5 # new RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 opt-out: chain verification ($caopt) and SNI
6 # (-servername) stay, only -verify_hostname is dropped.
7 #
8 # A fake openssl echoes its argv to a capture file via RSYNC_SSL_OPENSSL.
9
10 import os
11 import shlex
12 import subprocess
13
14 from rsyncfns import SCRATCHDIR, SRCDIR, rmtree, test_fail
15
16 base = SCRATCHDIR / 'rsync-ssl-openssl-host'
17 rmtree(base)
18 base.mkdir(parents=True)
19
20 args_capture = base / 'openssl_args'
21 fake_openssl = base / 'fake_openssl'
22 fake_openssl.write_text(f'#!/bin/sh\necho "$@" > {shlex.quote(str(args_capture))}\nexit 0\n')
23 fake_openssl.chmod(0o755)
24 ca = base / 'ca.pem'
25 ca.write_text("dummy-ca\n")
26
27 helper = ['--HELPER', 'localhost', 'rsync', '--server', '--daemon', '.']
28 base_env = {**os.environ, 'RSYNC_SSL_TYPE': 'openssl',
29 'RSYNC_SSL_OPENSSL': str(fake_openssl), 'RSYNC_SSL_CA_CERT': str(ca)}
30 base_env.pop('RSYNC_SSL_SKIP_HOSTNAME_CHECK', None)
31
32
33 def run(env):
34 if args_capture.exists():
35 args_capture.unlink()
36 subprocess.run(['bash', str(SRCDIR / 'rsync-ssl')] + helper, env=env,
37 stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
38 return args_capture.read_text() if args_capture.exists() else ''
39
40
41 # --- Default: the openssl client verifies the hostname.
42 args = run(base_env)
43 if '-connect localhost:' not in args:
44 test_fail(f"premise: fake openssl did not capture argv:\n{args!r}")
45 if '-verify_hostname localhost' not in args:
46 test_fail(f"openssl backend is missing -verify_hostname localhost:\n{args}")
47
48 # --- Chain-only opt-out: drop -verify_hostname, keep SNI + chain verification.
49 args = run({**base_env, 'RSYNC_SSL_SKIP_HOSTNAME_CHECK': '1'})
50 if '-verify_hostname' in args:
51 test_fail("RSYNC_SSL_SKIP_HOSTNAME_CHECK=1 must drop -verify_hostname from "
52 f"the openssl backend:\n{args}")
53 if '-servername localhost' not in args or '-CAfile' not in args:
54 test_fail("SKIP_HOSTNAME_CHECK must keep SNI (-servername) and chain "
55 f"verification (-CAfile):\n{args}")
56
57 print("rsync-ssl-openssl-hostname-check: openssl binds the cert to the host by "
58 "default, and SKIP_HOSTNAME_CHECK opts back to chain-only")