|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Tests for hostname validation — no FUSE mount required.""" |
| 3 | + |
| 4 | +if __name__ == "__main__": |
| 5 | + import pytest |
| 6 | + import sys |
| 7 | + |
| 8 | + sys.exit(pytest.main([__file__] + sys.argv[1:])) |
| 9 | + |
| 10 | +import subprocess |
| 11 | +from util import base_cmdline, basename |
| 12 | +from os.path import join as pjoin |
| 13 | + |
| 14 | + |
| 15 | +def test_reject_option_injection_in_hostname(tmpdir): |
| 16 | + """Bracketed source that resolves to a dash-prefixed host must be rejected.""" |
| 17 | + |
| 18 | + mnt_dir = str(tmpdir.mkdir("mnt")) |
| 19 | + malicious = "[-oProxyCommand=echo pwned]:/path" |
| 20 | + |
| 21 | + cmdline = base_cmdline + [ |
| 22 | + pjoin(basename, "sshfs"), |
| 23 | + "-f", |
| 24 | + malicious, |
| 25 | + mnt_dir, |
| 26 | + ] |
| 27 | + res = subprocess.run( |
| 28 | + cmdline, |
| 29 | + stdin=subprocess.DEVNULL, |
| 30 | + stdout=subprocess.PIPE, |
| 31 | + stderr=subprocess.PIPE, |
| 32 | + timeout=10, |
| 33 | + text=True, |
| 34 | + ) |
| 35 | + assert res.returncode != 0 |
| 36 | + assert "invalid hostname" in res.stderr |
| 37 | + |
| 38 | + |
| 39 | +def test_reject_dash_host_after_doubledash(tmpdir): |
| 40 | + """Non-bracketed dash-prefixed source after -- must also be rejected.""" |
| 41 | + |
| 42 | + mnt_dir = str(tmpdir.mkdir("mnt")) |
| 43 | + |
| 44 | + cmdline = base_cmdline + [ |
| 45 | + pjoin(basename, "sshfs"), |
| 46 | + "-f", |
| 47 | + "--", |
| 48 | + "-oProxyCommand=echo pwned:/path", |
| 49 | + mnt_dir, |
| 50 | + ] |
| 51 | + res = subprocess.run( |
| 52 | + cmdline, |
| 53 | + stdin=subprocess.DEVNULL, |
| 54 | + stdout=subprocess.PIPE, |
| 55 | + stderr=subprocess.PIPE, |
| 56 | + timeout=10, |
| 57 | + text=True, |
| 58 | + ) |
| 59 | + assert res.returncode != 0 |
| 60 | + assert "invalid hostname" in res.stderr |
0 commit comments