feat(security): harden yaml safe_load and connection command injection
This commit is contained in:
1 parent
2da9b45548
commit
a90e8bf597
5 files changed
+353
-10
No files matched your search
@@ -162,7 +162,10 @@ class RunHandler:
|
|||||||
path = args.data[0]
|
path = args.data[0]
|
||||||
try:
|
try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
playbook = yaml.load(f, Loader=yaml.FullLoader)
|
playbook = yaml.safe_load(f)
|
||||||
|
|
||||||
|
if not isinstance(playbook, dict):
|
||||||
|
raise TypeError("Invalid playbook format: expected a dictionary")
|
||||||
|
|
||||||
# Check preflight first before any task runs
|
# Check preflight first before any task runs
|
||||||
if getattr(args, "preflight_ai", False):
|
if getattr(args, "preflight_ai", False):
|
||||||
|
|||||||
+125
-7
@@ -103,7 +103,7 @@ class node:
|
|||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost=''):
|
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost='', allow_unsafe_options=None):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
### Parameters:
|
### Parameters:
|
||||||
@@ -138,14 +138,22 @@ class node:
|
|||||||
like "os", "prompt" and "screenleght_command"
|
like "os", "prompt" and "screenleght_command"
|
||||||
|
|
||||||
- jumphost (str): Reference another node to be used as a jumphost
|
- jumphost (str): Reference another node to be used as a jumphost
|
||||||
|
|
||||||
|
- allow_unsafe_options (bool): Explicitly override safety restrictions.
|
||||||
'''
|
'''
|
||||||
self.config = config
|
self.config = config
|
||||||
if config == '':
|
if config == '':
|
||||||
self.idletime = 0
|
self.idletime = 0
|
||||||
self.key = None
|
self.key = None
|
||||||
else:
|
else:
|
||||||
self.idletime = config.config["idletime"]
|
self.idletime = config.config["idletime"] if (hasattr(config, "config") and isinstance(config.config, dict) and "idletime" in config.config) else 0
|
||||||
self.key = config.key
|
self.key = getattr(config, "key", None)
|
||||||
|
if allow_unsafe_options is not None:
|
||||||
|
self.allow_unsafe_options = bool(allow_unsafe_options)
|
||||||
|
elif config != '' and config is not None and hasattr(config, "config") and isinstance(config.config, dict):
|
||||||
|
self.allow_unsafe_options = bool(config.config.get("allow_unsafe_options", False))
|
||||||
|
else:
|
||||||
|
self.allow_unsafe_options = False
|
||||||
self.unique = unique
|
self.unique = unique
|
||||||
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
|
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
|
||||||
for key in attr:
|
for key in attr:
|
||||||
@@ -216,6 +224,79 @@ class node:
|
|||||||
jh_dict["password"] = [jh_dict["password"]]
|
jh_dict["password"] = [jh_dict["password"]]
|
||||||
return jh_dict
|
return jh_dict
|
||||||
|
|
||||||
|
def _is_unsafe_allowed(self) -> bool:
|
||||||
|
if getattr(self, "allow_unsafe_options", False):
|
||||||
|
return True
|
||||||
|
if hasattr(self, "config") and self.config and hasattr(self.config, "config") and isinstance(self.config.config, dict):
|
||||||
|
return bool(self.config.config.get("allow_unsafe_options", False))
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _validate_base_fields(self, check_port=True):
|
||||||
|
"""Sanitize host, user, and port against argument injection and shell metacharacters."""
|
||||||
|
host_s = str(self.host) if self.host is not None else ""
|
||||||
|
if not host_s:
|
||||||
|
raise ValueError("Host cannot be empty")
|
||||||
|
if host_s.startswith("-"):
|
||||||
|
raise ValueError(f"Host cannot start with '-': '{self.host}'")
|
||||||
|
host_bare = (
|
||||||
|
host_s[1:-1]
|
||||||
|
if (host_s.startswith("[") and host_s.endswith("]"))
|
||||||
|
else host_s
|
||||||
|
)
|
||||||
|
if not host_bare or host_bare.startswith("-"):
|
||||||
|
raise ValueError(f"Invalid characters in host: '{self.host}'")
|
||||||
|
if _LEGACY_SSH_UNSAFE_RE.search(host_bare):
|
||||||
|
raise ValueError(f"Invalid characters in host: '{self.host}'")
|
||||||
|
|
||||||
|
user_s = str(self.user) if self.user is not None else ""
|
||||||
|
if user_s:
|
||||||
|
if user_s.startswith("-"):
|
||||||
|
raise ValueError(f"User cannot start with '-': '{self.user}'")
|
||||||
|
if _LEGACY_SSH_UNSAFE_RE.search(user_s):
|
||||||
|
raise ValueError(f"Invalid characters in user: '{self.user}'")
|
||||||
|
|
||||||
|
if check_port and self.port != "" and self.port is not None:
|
||||||
|
if isinstance(self.port, (bool, float)):
|
||||||
|
raise ValueError(f"Invalid TCP SSH port '{self.port}'")
|
||||||
|
port_s = str(self.port).strip()
|
||||||
|
if port_s:
|
||||||
|
if (
|
||||||
|
not port_s.isascii()
|
||||||
|
or not re.fullmatch(r"[0-9]+", port_s, re.ASCII)
|
||||||
|
or not (1 <= int(port_s) <= 65535)
|
||||||
|
):
|
||||||
|
raise ValueError(f"Invalid TCP SSH port '{self.port}'")
|
||||||
|
|
||||||
|
def _validate_options_and_safety(self):
|
||||||
|
"""Validate options string for unbalanced quotes and dangerous control characters."""
|
||||||
|
if not self.options:
|
||||||
|
return
|
||||||
|
opts_str = str(self.options).strip()
|
||||||
|
if not opts_str:
|
||||||
|
return
|
||||||
|
if not self._is_unsafe_allowed():
|
||||||
|
if any(c in opts_str for c in ("\n", "\r", "\x00")):
|
||||||
|
raise ValueError("Options cannot contain newlines or null bytes")
|
||||||
|
try:
|
||||||
|
shlex.split(opts_str)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid options: unbalanced quotes ({e})") from e
|
||||||
|
|
||||||
|
def _validate_ssh_options(self):
|
||||||
|
"""Sanitize SSH options against local command execution directives."""
|
||||||
|
self._validate_options_and_safety()
|
||||||
|
if not self.options:
|
||||||
|
return
|
||||||
|
opts_str = str(self.options).strip()
|
||||||
|
if not opts_str:
|
||||||
|
return
|
||||||
|
if not self._is_unsafe_allowed():
|
||||||
|
dangerous_directives = ("ProxyCommand", "LocalCommand", "PermitLocalCommand", "KnownHostsCommand")
|
||||||
|
pattern = r'(?:' + '|'.join(dangerous_directives) + r')'
|
||||||
|
match = re.search(pattern, opts_str, re.IGNORECASE)
|
||||||
|
if match:
|
||||||
|
raise ValueError(f"Disallowed SSH directive in options: '{match.group(0)}'")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _validate_legacy_ssh_hop_fields(host, user, port):
|
def _validate_legacy_ssh_hop_fields(host, user, port):
|
||||||
"""Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.)."""
|
"""Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.)."""
|
||||||
@@ -1442,10 +1523,13 @@ class node:
|
|||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None):
|
def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None):
|
||||||
|
self._validate_base_fields(check_port=True)
|
||||||
is_from_mosh = (
|
is_from_mosh = (
|
||||||
self.protocol == "mosh"
|
self.protocol == "mosh"
|
||||||
or getattr(self, "_orig_protocol", "") == "mosh"
|
or getattr(self, "_orig_protocol", "") == "mosh"
|
||||||
)
|
)
|
||||||
|
if not is_from_mosh:
|
||||||
|
self._validate_ssh_options()
|
||||||
proto = "ssh" if force_ssh else self.protocol
|
proto = "ssh" if force_ssh else self.protocol
|
||||||
cmd = proto
|
cmd = proto
|
||||||
if is_from_mosh:
|
if is_from_mosh:
|
||||||
@@ -1536,7 +1620,8 @@ class node:
|
|||||||
if isinstance(self.tags, dict) and self.tags.get("mosh_ssh_options"):
|
if isinstance(self.tags, dict) and self.tags.get("mosh_ssh_options"):
|
||||||
ssh_opts = str(self.tags["mosh_ssh_options"]).strip()
|
ssh_opts = str(self.tags["mosh_ssh_options"]).strip()
|
||||||
if ssh_opts:
|
if ssh_opts:
|
||||||
if re.search(r'(?:LocalCommand|ProxyCommand)', ssh_opts, re.IGNORECASE):
|
if not self._is_unsafe_allowed():
|
||||||
|
if re.search(r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', ssh_opts, re.IGNORECASE):
|
||||||
raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand")
|
raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand")
|
||||||
try:
|
try:
|
||||||
shlex.split(ssh_opts)
|
shlex.split(ssh_opts)
|
||||||
@@ -1555,7 +1640,8 @@ class node:
|
|||||||
if opts:
|
if opts:
|
||||||
if re.search(r'--(?:ssh|server|client)(?:=|\s|$)', opts):
|
if re.search(r'--(?:ssh|server|client)(?:=|\s|$)', opts):
|
||||||
raise ValueError("Options cannot override --ssh, --server or --client for mosh")
|
raise ValueError("Options cannot override --ssh, --server or --client for mosh")
|
||||||
if re.search(r'(?:LocalCommand|ProxyCommand)', opts, re.IGNORECASE):
|
if not self._is_unsafe_allowed():
|
||||||
|
if re.search(r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', opts, re.IGNORECASE):
|
||||||
raise ValueError("Options cannot contain LocalCommand or ProxyCommand")
|
raise ValueError("Options cannot contain LocalCommand or ProxyCommand")
|
||||||
try:
|
try:
|
||||||
shlex.split(opts)
|
shlex.split(opts)
|
||||||
@@ -1569,6 +1655,8 @@ class node:
|
|||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_telnet_cmd(self):
|
def _generate_telnet_cmd(self):
|
||||||
|
self._validate_base_fields(check_port=True)
|
||||||
|
self._validate_options_and_safety()
|
||||||
cmd = f"telnet {self.host}"
|
cmd = f"telnet {self.host}"
|
||||||
if self.port:
|
if self.port:
|
||||||
cmd += f" {self.port}"
|
cmd += f" {self.port}"
|
||||||
@@ -1578,22 +1666,52 @@ class node:
|
|||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_kube_cmd(self):
|
def _generate_kube_cmd(self):
|
||||||
cmd = f"kubectl exec {self.options} {self.host} -it --"
|
self._validate_base_fields(check_port=False)
|
||||||
|
self._validate_options_and_safety()
|
||||||
kube_command = self.tags.get("kube_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
kube_command = self.tags.get("kube_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
||||||
|
if not self._is_unsafe_allowed() and kube_command:
|
||||||
|
k_str = str(kube_command)
|
||||||
|
if any(c in k_str for c in ("\n", "\r", "\x00")):
|
||||||
|
raise ValueError("kube_command cannot contain newlines or null bytes")
|
||||||
|
try:
|
||||||
|
shlex.split(k_str)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid kube_command: unbalanced quotes ({e})") from e
|
||||||
|
cmd = f"kubectl exec {self.options} {self.host} -it --"
|
||||||
cmd += f" {kube_command}"
|
cmd += f" {kube_command}"
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_docker_cmd(self):
|
def _generate_docker_cmd(self):
|
||||||
cmd = f"docker {self.options} exec -it {self.host}"
|
self._validate_base_fields(check_port=False)
|
||||||
|
self._validate_options_and_safety()
|
||||||
docker_command = self.tags.get("docker_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
docker_command = self.tags.get("docker_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
||||||
|
if not self._is_unsafe_allowed() and docker_command:
|
||||||
|
d_str = str(docker_command)
|
||||||
|
if any(c in d_str for c in ("\n", "\r", "\x00")):
|
||||||
|
raise ValueError("docker_command cannot contain newlines or null bytes")
|
||||||
|
try:
|
||||||
|
shlex.split(d_str)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid docker_command: unbalanced quotes ({e})") from e
|
||||||
|
cmd = f"docker {self.options} exec -it {self.host}"
|
||||||
cmd += f" {docker_command}"
|
cmd += f" {docker_command}"
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_ssm_cmd(self):
|
def _generate_ssm_cmd(self):
|
||||||
|
self._validate_base_fields(check_port=False)
|
||||||
|
self._validate_options_and_safety()
|
||||||
region = self.tags.get("region", "") if isinstance(self.tags, dict) else ""
|
region = self.tags.get("region", "") if isinstance(self.tags, dict) else ""
|
||||||
profile = self.tags.get("profile", "") if isinstance(self.tags, dict) else ""
|
profile = self.tags.get("profile", "") if isinstance(self.tags, dict) else ""
|
||||||
|
if not self._is_unsafe_allowed():
|
||||||
|
for field_name, field_val in [("region", region), ("profile", profile)]:
|
||||||
|
if field_val:
|
||||||
|
f_str = str(field_val)
|
||||||
|
if f_str.startswith("-"):
|
||||||
|
raise ValueError(f"SSM tag '{field_name}' cannot start with '-': '{field_val}'")
|
||||||
|
if re.search(r'[\s\x00-\x1f\x7f;|&$`><"\']', f_str):
|
||||||
|
raise ValueError(f"Invalid characters in SSM tag '{field_name}': '{field_val}'")
|
||||||
cmd = f"aws ssm start-session --target {self.host}"
|
cmd = f"aws ssm start-session --target {self.host}"
|
||||||
if region:
|
if region:
|
||||||
cmd += f" --region {region}"
|
cmd += f" --region {region}"
|
||||||
|
|||||||
@@ -960,7 +960,7 @@ class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
|||||||
if request.value.startswith("---YAML---\n"):
|
if request.value.startswith("---YAML---\n"):
|
||||||
import yaml
|
import yaml
|
||||||
content = request.value[len("---YAML---\n"):]
|
content = request.value[len("---YAML---\n"):]
|
||||||
data = yaml.load(content, Loader=yaml.FullLoader)
|
data = yaml.safe_load(content)
|
||||||
self.service.import_from_dict(data)
|
self.service.import_from_dict(data)
|
||||||
else:
|
else:
|
||||||
self.service.import_from_file(request.value)
|
self.service.import_from_file(request.value)
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class ImportExportService(BaseService):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
data = yaml.safe_load(f)
|
||||||
self.import_from_dict(data)
|
self.import_from_dict(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||||
|
|||||||
@@ -0,0 +1,222 @@
|
|||||||
|
"""Tests for YAML safe deserialization and connection options sanitization with allow_unsafe_options."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from connpy.cli.run_handler import RunHandler
|
||||||
|
from connpy.core import node
|
||||||
|
from connpy.grpc_layer.server import ImportExportServicer
|
||||||
|
from connpy.services.exceptions import InvalidConfigurationError
|
||||||
|
from connpy.services.import_export_service import ImportExportService
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 1. Tests de Deserialización YAML Segura
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TestYamlSafeDeserialization:
|
||||||
|
def test_import_export_service_rejects_unsafe_yaml(self, config, tmp_path):
|
||||||
|
"""YAML with !!python/object/apply must fail with InvalidConfigurationError and never execute."""
|
||||||
|
malicious_yaml = tmp_path / "malicious.yaml"
|
||||||
|
malicious_yaml.write_text('!!python/object/apply:os.system ["echo pwned"]')
|
||||||
|
|
||||||
|
service = ImportExportService(config)
|
||||||
|
|
||||||
|
with patch("os.system") as mock_system:
|
||||||
|
with pytest.raises(InvalidConfigurationError):
|
||||||
|
service.import_from_file(str(malicious_yaml))
|
||||||
|
mock_system.assert_not_called()
|
||||||
|
|
||||||
|
def test_grpc_import_from_file_rejects_unsafe_yaml(self):
|
||||||
|
"""gRPC import with !!python/object/apply must abort via context and never execute."""
|
||||||
|
payload = '---YAML---\n!!python/object/apply:os.system ["echo pwned"]'
|
||||||
|
request = MagicMock()
|
||||||
|
request.value = payload
|
||||||
|
context = MagicMock()
|
||||||
|
|
||||||
|
provider = MagicMock()
|
||||||
|
servicer = ImportExportServicer(provider)
|
||||||
|
|
||||||
|
with patch("os.system") as mock_system:
|
||||||
|
servicer.import_from_file(request, context)
|
||||||
|
context.abort.assert_called_once()
|
||||||
|
mock_system.assert_not_called()
|
||||||
|
|
||||||
|
def test_run_handler_yaml_run_rejects_unsafe_yaml(self, tmp_path):
|
||||||
|
"""RunHandler.yaml_run with !!python/object/apply exits cleanly with code 10 and never executes."""
|
||||||
|
malicious_playbook = tmp_path / "bad_playbook.yaml"
|
||||||
|
malicious_playbook.write_text(
|
||||||
|
'tasks:\n - name: exploit\n action: run\n nodes: all\n commands:\n - !!python/object/apply:os.system ["echo pwned"]\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
app = MagicMock()
|
||||||
|
handler = RunHandler(app)
|
||||||
|
args = MagicMock()
|
||||||
|
args.data = [str(malicious_playbook)]
|
||||||
|
args.preflight_ai = False
|
||||||
|
args.analyze = None
|
||||||
|
|
||||||
|
with patch("os.system") as mock_system:
|
||||||
|
with pytest.raises(SystemExit) as exc_info:
|
||||||
|
handler.yaml_run(args)
|
||||||
|
assert exc_info.value.code == 10
|
||||||
|
mock_system.assert_not_called()
|
||||||
|
|
||||||
|
def test_run_handler_yaml_run_rejects_non_dict_yaml(self, tmp_path):
|
||||||
|
"""RunHandler.yaml_run with empty or non-dict YAML exits cleanly with code 10."""
|
||||||
|
non_dict_playbook = tmp_path / "list_playbook.yaml"
|
||||||
|
non_dict_playbook.write_text("- item1\n- item2\n")
|
||||||
|
|
||||||
|
app = MagicMock()
|
||||||
|
handler = RunHandler(app)
|
||||||
|
args = MagicMock()
|
||||||
|
args.data = [str(non_dict_playbook)]
|
||||||
|
args.preflight_ai = False
|
||||||
|
args.analyze = None
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit) as exc_info:
|
||||||
|
handler.yaml_run(args)
|
||||||
|
assert exc_info.value.code == 10
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 2. Tests Negativos de Inyección en Protocolos (allow_unsafe_options=False)
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TestProtocolInjectionNegative:
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_host_starting_with_dash_raises_value_error(self, proto):
|
||||||
|
"""Host starting with '-' raises ValueError across all protocols."""
|
||||||
|
n = node("bad_node", "-oProxyCommand=id", protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Host cannot start with '-'"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_host_with_unsafe_characters_raises_value_error(self, proto):
|
||||||
|
"""Host containing whitespace, control characters or shell metacharacters raises ValueError."""
|
||||||
|
for bad_host in ["host; id", "host | id", "host`id`", "host$id", "host&id"]:
|
||||||
|
n = node("bad_node", bad_host, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Invalid characters in host"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_user_starting_with_dash_raises_value_error(self, proto):
|
||||||
|
"""User starting with '-' raises ValueError across all protocols."""
|
||||||
|
n = node("n1", "10.0.0.1", user="-oProxyCommand=id", protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="User cannot start with '-'"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_user_with_unsafe_characters_raises_value_error(self, proto):
|
||||||
|
"""User containing shell metacharacters raises ValueError across all protocols."""
|
||||||
|
for bad_user in ["; id", "user | id", "user`id`", "user$id", "user&id"]:
|
||||||
|
n = node("n1", "10.0.0.1", user=bad_user, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Invalid characters in user"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet"])
|
||||||
|
def test_invalid_port_raises_value_error(self, proto):
|
||||||
|
"""Invalid port in SSH/SFTP/Telnet raises ValueError."""
|
||||||
|
for bad_port in ["22 -oProxyCommand=id", "99999", "abc", "0", "-1"]:
|
||||||
|
n = node("n1", "10.0.0.1", port=bad_port, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Invalid TCP SSH port"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp"])
|
||||||
|
def test_dangerous_ssh_directives_in_options_raise_value_error(self, proto):
|
||||||
|
"""Directives like ProxyCommand, LocalCommand, etc. in options raise ValueError."""
|
||||||
|
dangerous_opts = [
|
||||||
|
'-o ProxyCommand="sh -c id"',
|
||||||
|
"-oProxyCommand=id",
|
||||||
|
'-o LocalCommand="id"',
|
||||||
|
"-o PermitLocalCommand=yes",
|
||||||
|
'-o KnownHostsCommand="id"',
|
||||||
|
'-o "ProxyCommand=id"',
|
||||||
|
"-o proxycommand=id",
|
||||||
|
]
|
||||||
|
for opt in dangerous_opts:
|
||||||
|
n = node("n1", "10.0.0.1", options=opt, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Disallowed SSH directive in options"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_unbalanced_quotes_in_options_raise_value_error(self, proto):
|
||||||
|
"""Unbalanced quotes in options raise ValueError."""
|
||||||
|
n = node("n1", "10.0.0.1", options='-o "StrictHostKeyChecking=no', protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="unbalanced quotes"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
def test_ssm_invalid_region_or_profile_tags_raise_value_error(self):
|
||||||
|
"""SSM tags 'region' and 'profile' reject leading dash and shell metacharacters."""
|
||||||
|
n_reg = node("ssm1", "i-1234567890abcdef0", protocol="ssm", tags={"region": "us-east-1; id"})
|
||||||
|
with pytest.raises(ValueError, match="Invalid characters in SSM tag"):
|
||||||
|
n_reg._get_cmd()
|
||||||
|
|
||||||
|
n_prof = node("ssm2", "i-1234567890abcdef0", protocol="ssm", tags={"profile": "-bad"})
|
||||||
|
with pytest.raises(ValueError, match="cannot start with '-'"):
|
||||||
|
n_prof._get_cmd()
|
||||||
|
|
||||||
|
def test_container_command_validation(self):
|
||||||
|
"""Kube and Docker commands reject newlines and unbalanced quotes."""
|
||||||
|
n_kube = node("k1", "pod-1", protocol="kubectl", tags={"kube_command": 'sh -c "echo\nid"'})
|
||||||
|
with pytest.raises(ValueError, match="cannot contain newlines or null bytes"):
|
||||||
|
n_kube._get_cmd()
|
||||||
|
|
||||||
|
n_kube_q = node("k2", "pod-1", protocol="kubectl", tags={"kube_command": 'sh -c "unclosed'})
|
||||||
|
with pytest.raises(ValueError, match="unbalanced quotes"):
|
||||||
|
n_kube_q._get_cmd()
|
||||||
|
|
||||||
|
n_doc = node("d1", "cont-1", protocol="docker", tags={"docker_command": 'sh -c "echo\nid"'})
|
||||||
|
with pytest.raises(ValueError, match="cannot contain newlines or null bytes"):
|
||||||
|
n_doc._get_cmd()
|
||||||
|
|
||||||
|
n_doc_q = node("d2", "cont-1", protocol="docker", tags={"docker_command": 'sh -c "unclosed'})
|
||||||
|
with pytest.raises(ValueError, match="unbalanced quotes"):
|
||||||
|
n_doc_q._get_cmd()
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 3. Tests Positivos de Regresión y allow_unsafe_options=True
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TestPositiveAndAllowUnsafeOptions:
|
||||||
|
def test_ssh_jumphost_proxycommand_allowed_when_safe_mode(self, populated_config):
|
||||||
|
"""Valid jumphost chain still generates -o ProxyCommand correctly when allow_unsafe_options=False."""
|
||||||
|
# populated_config has 'router1'
|
||||||
|
n = node("target", "10.0.0.2", protocol="ssh", jumphost="router1", config=populated_config)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert '-o ProxyCommand="' in cmd
|
||||||
|
assert "ssh -W %h:%p" in cmd
|
||||||
|
assert "admin@10.0.0.1" in cmd
|
||||||
|
|
||||||
|
def test_ssh_safe_options_permitted(self):
|
||||||
|
"""Benign SSH options work without issue."""
|
||||||
|
safe_opts = "-o StrictHostKeyChecking=no -i ~/.ssh/id_rsa -L 8080:localhost:80"
|
||||||
|
n = node("s1", "10.0.0.1", protocol="ssh", options=safe_opts)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert safe_opts in cmd
|
||||||
|
|
||||||
|
def test_user_domain_and_ipv6_bracketed_permitted(self):
|
||||||
|
"""Formats like DOMAIN\\user and bracketed IPv6 [2001:db8::1] remain supported."""
|
||||||
|
n = node("s1", "[2001:db8::1]", user=r"DOMAIN\user", protocol="ssh", port="22")
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert r"DOMAIN\user@[2001:db8::1]" in cmd
|
||||||
|
assert "-p 22" in cmd
|
||||||
|
|
||||||
|
def test_allow_unsafe_options_via_config(self, populated_config):
|
||||||
|
"""When config.config['allow_unsafe_options'] is True, ProxyCommand in options is allowed."""
|
||||||
|
populated_config.config["allow_unsafe_options"] = True
|
||||||
|
custom_proxy = '-o ProxyCommand="ssh -W %h:%p bastion"'
|
||||||
|
n = node("s1", "10.0.0.1", protocol="ssh", options=custom_proxy, config=populated_config)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert custom_proxy in cmd
|
||||||
|
|
||||||
|
def test_allow_unsafe_options_via_kwarg(self):
|
||||||
|
"""When allow_unsafe_options=True passed to node, dangerous options are allowed."""
|
||||||
|
custom_proxy = '-o ProxyCommand="ssh -W %h:%p bastion"'
|
||||||
|
n = node("s1", "10.0.0.1", protocol="ssh", options=custom_proxy, allow_unsafe_options=True)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert custom_proxy in cmd
|
||||||
Reference in new issue
Block a user