|
| 1 | +"""SSH command allowlist validation.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import shlex |
| 6 | + |
| 7 | +_SAFE_SSH_OPTION_KEYS = frozenset( |
| 8 | + {"StrictHostKeyChecking", "BatchMode", "UserKnownHostsFile"} |
| 9 | +) |
| 10 | + |
| 11 | +_PORT_RE = re.compile(r"^[0-9]{1,5}$") |
| 12 | + |
| 13 | + |
| 14 | +class InvalidSshCommandError(Exception): |
| 15 | + """Raised when an SSH command string does not pass the allowlist.""" |
| 16 | + |
| 17 | + |
| 18 | +def _validate_value_flag(tokens: list[str], i: int, flag: str) -> None: |
| 19 | + """Raise InvalidSshCommandError if a -i or -p flag or its value is invalid.""" |
| 20 | + if i + 1 >= len(tokens): |
| 21 | + raise InvalidSshCommandError(f"flag {flag!r} requires a value") |
| 22 | + if flag == "-p" and not _PORT_RE.match(tokens[i + 1]): |
| 23 | + raise InvalidSshCommandError("-p requires a numeric port") |
| 24 | + |
| 25 | + |
| 26 | +def _validate_option_flag(tokens: list[str], i: int) -> None: |
| 27 | + """Raise InvalidSshCommandError if a -o flag or its value is invalid.""" |
| 28 | + if i + 1 >= len(tokens): |
| 29 | + raise InvalidSshCommandError("-o requires a value") |
| 30 | + key = tokens[i + 1].split("=", 1)[0] |
| 31 | + if key not in _SAFE_SSH_OPTION_KEYS: |
| 32 | + raise InvalidSshCommandError( |
| 33 | + f"SSH option {tokens[i + 1]!r} is not in the allowed list" |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def _validate_ssh_flags(tokens: list[str]) -> None: |
| 38 | + """Raise InvalidSshCommandError if any flag token after the binary is invalid.""" |
| 39 | + i = 1 |
| 40 | + while i < len(tokens): |
| 41 | + flag = tokens[i] |
| 42 | + if flag in ("-i", "-p"): |
| 43 | + _validate_value_flag(tokens, i, flag) |
| 44 | + i += 2 |
| 45 | + elif flag == "-o": |
| 46 | + _validate_option_flag(tokens, i) |
| 47 | + i += 2 |
| 48 | + else: |
| 49 | + raise InvalidSshCommandError(f"flag {flag!r} is not allowed") |
| 50 | + |
| 51 | + |
| 52 | +def sanitize_ssh_cmd(ssh_cmd: str) -> str: |
| 53 | + """Return *ssh_cmd* if it passes the allowlist; raise :exc:`InvalidSshCommandError` otherwise. |
| 54 | +
|
| 55 | + Accepts: |
| 56 | + * A bare absolute path to an ssh binary (no arguments). |
| 57 | + * ``ssh`` with a restricted set of flags: ``-i <file>``, ``-p <port>``, |
| 58 | + ``-o`` with option key in :data:`_SAFE_SSH_OPTION_KEYS`. |
| 59 | + """ |
| 60 | + try: |
| 61 | + tokens = shlex.split(ssh_cmd) |
| 62 | + except ValueError as exc: |
| 63 | + raise InvalidSshCommandError("cannot parse ssh command") from exc |
| 64 | + |
| 65 | + if not tokens: |
| 66 | + raise InvalidSshCommandError("empty ssh command") |
| 67 | + |
| 68 | + binary = tokens[0] |
| 69 | + |
| 70 | + if os.path.isabs(binary): |
| 71 | + if len(tokens) == 1: |
| 72 | + return ssh_cmd |
| 73 | + raise InvalidSshCommandError("absolute-path ssh binary may not carry arguments") |
| 74 | + |
| 75 | + if binary != "ssh": |
| 76 | + raise InvalidSshCommandError("ssh command must be 'ssh' or an absolute path") |
| 77 | + |
| 78 | + _validate_ssh_flags(tokens) |
| 79 | + return ssh_cmd |
0 commit comments