| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 32 |
|
| 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 |
|
| 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 |
|
| 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") |