From 8e388cc6b6361bb38c976066d703cb031d95da9c Mon Sep 17 00:00:00 2001 From: Fede Luzzi Date: Mon, 16 Nov 2026 21:14:58 -0300 Subject: [PATCH] feat(spec-04-adaptive-timeouts-ssm): worker (eng_flash_high) implementation --- connpy/core.py | 23 ++++-- connpy/tests/test_adaptive_timeouts_ssm.py | 88 ++++++++++++++++++++++ 2 files changed, 103 insertions(+), 8 deletions(-) diff --git a/connpy/core.py b/connpy/core.py index db16384..3b716fe 100755 --- a/connpy/core.py +++ b/connpy/core.py @@ -144,7 +144,7 @@ def resolve_timeouts( tag_max_ext, "tags.max_progress_extensions", min_val=0, - max_val=MAX_STAGE_TIMEOUT_CAP, + max_val=10, ) else: max_ext = DEFAULT_MAX_PROGRESS_EXTENSIONS @@ -174,7 +174,10 @@ def compile_expects(expects_map): elif isinstance(pat, re.Pattern): if isinstance(pat.pattern, str): compiled.append( - re.compile(pat.pattern.encode("ascii"), pat.flags | re.DOTALL) + re.compile( + pat.pattern.encode("ascii"), + (pat.flags & ~re.UNICODE) | re.DOTALL, + ) ) else: compiled.append(pat) @@ -1089,23 +1092,27 @@ class node: not self._is_unsafe_allowed() and _SSM_UNSAFE_OPTIONS_RE.search(nc_cmd) ): raise ValueError(f"Invalid nc_command in {jh_proto} jumphost") + jh_opt_joined = "" jh_opt_str = str(jh.get("options", "") or "").strip() if jh_opt_str: if _CONTROL_CHAR_RE.search(jh_opt_str): raise ValueError(f"Control characters disallowed in {jh_proto} options") + if not self._is_unsafe_allowed() and _SSM_UNSAFE_OPTIONS_RE.search(jh_opt_str): + raise ValueError(f"Invalid characters in {jh_proto} options") try: - shlex.split(jh_opt_str) + jh_opt_tokens = shlex.split(jh_opt_str) except ValueError as exc: raise ValueError(f"Invalid {jh_proto} options: unbalanced quotes") from exc + jh_opt_joined = shlex.join(jh_opt_tokens) if jh_proto == "kubectl": cmd = "kubectl exec " - if jh_opt_str: - cmd += f"{jh_opt_str} " + if jh_opt_joined: + cmd += f"{jh_opt_joined} " cmd += f"{jh_host} -i -- {nc_cmd} {w_target} {w_port}" else: cmd = "docker " - if jh_opt_str: - cmd += f"{jh_opt_str} " + if jh_opt_joined: + cmd += f"{jh_opt_joined} " cmd += f"exec -i {jh_host} {nc_cmd} {w_target} {w_port}" else: if include_meta: @@ -1513,7 +1520,7 @@ class node: if tasks[0] in done and tasks[1] not in done: try: await asyncio.wait_for(tasks[1], timeout=0.2) - except (TimeoutError, asyncio.CancelledError): + except (asyncio.TimeoutError, TimeoutError, asyncio.CancelledError): # noqa: UP041 pass for t in tasks: diff --git a/connpy/tests/test_adaptive_timeouts_ssm.py b/connpy/tests/test_adaptive_timeouts_ssm.py index acb8287..3e42da0 100644 --- a/connpy/tests/test_adaptive_timeouts_ssm.py +++ b/connpy/tests/test_adaptive_timeouts_ssm.py @@ -187,6 +187,12 @@ class TestResolveTimeouts: with pytest.raises(ValueError, match="Invalid"): resolve_timeouts("ssh", {}, [], auth_timeout=bad_val) + def test_max_progress_extensions_capped_at_10(self): + assert resolve_timeouts("ssh", {"max_progress_extensions": 0}, []) == (10, 25, 10, 0, 180) + assert resolve_timeouts("ssh", {"max_progress_extensions": 10}, []) == (10, 25, 10, 10, 180) + with pytest.raises(ValueError, match="Invalid"): + resolve_timeouts("ssh", {"max_progress_extensions": 11}, []) + def test_connect_and_run_return_code_12_on_invalid_timeout(self): n = node("r1", "10.0.0.1", user="admin", protocol="ssh") res_conn = n._connect(timeout=True) @@ -554,6 +560,74 @@ class TestJumphostChainAndSecurity: with pytest.raises(ValueError, match="cannot contain|Disallowed"): node("router1", "10.20.0.5", user="netadmin", jumphost="jh_ssh", config=cfg) + def test_kubectl_and_docker_jumphost_options_reject_injection_and_serialize_shlex(self, tmp_path): + for jh_proto in ("kubectl", "docker"): + for bad_opts in ["-n ops;id", "--context=$(whoami)", "-n ops|cat", "--flag `id`"]: + cfg_bad = _make_config( + tmp_path, + { + "jh": { + "host": "jump-box", + "protocol": jh_proto, + "port": "", + "user": "", + "password": "", + "options": bad_opts, + "logs": "", + "tags": "", + "jumphost": "", + "type": "connection", + }, + "dest": { + "host": "10.20.0.5", + "protocol": "ssh", + "port": "", + "user": "admin", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "jh", + "type": "connection", + }, + }, + ) + with pytest.raises(ValueError, match=f"Invalid characters in {jh_proto} options"): + node("dest", "10.20.0.5", user="admin", jumphost="jh", config=cfg_bad) + + cfg_ok = _make_config( + tmp_path, + { + "jh": { + "host": "jump-box", + "protocol": jh_proto, + "port": "", + "user": "", + "password": "", + "options": "-n ops --context prod", + "logs": "", + "tags": "", + "jumphost": "", + "type": "connection", + }, + "dest": { + "host": "10.20.0.5", + "protocol": "ssh", + "port": "", + "user": "admin", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "jh", + "type": "connection", + }, + }, + ) + dest = node("dest", "10.20.0.5", user="admin", jumphost="jh", config=cfg_ok) + raw_cmd, _ = dest._build_jumphost_chain("jh", cfg_ok) + assert "-n ops --context prod" in raw_cmd + # ============================================================================= # 4. _connect progress extension, global deadline, finally, concurrency, redaction @@ -792,6 +866,20 @@ class TestHardenedExpectsAndMotd: assert compiled["ssh"][1] is pexpect.EOF assert compiled["ssh"][2] is pexpect.TIMEOUT + def test_compile_expects_masks_re_unicode_when_recompiling_str_pattern(self): + str_pat_default = re.compile(r"Connection refused") + str_pat_explicit = re.compile(r"Permission denied.*", re.IGNORECASE | re.UNICODE) + compiled = compile_expects({"ssh": [str_pat_default, str_pat_explicit]}) + + for cpat in compiled["ssh"]: + assert isinstance(cpat, re.Pattern) + assert isinstance(cpat.pattern, bytes) + assert cpat.flags & re.DOTALL + assert not (cpat.flags & re.UNICODE) + + assert compiled["ssh"][1].flags & re.IGNORECASE + assert compiled["ssh"][1].search(b"PERMISSION DENIED\nhost") is not None + def test_adversarial_motd_banners_do_not_false_positive_and_connect(self): # 1) SSH adversarial MOTD: TACACS+ unavailable + Permission denied retry + chunked Connection refused + closed + not supported n_ssh = node("r_ssh", "10.0.0.1", user="admin", password="pw1234", protocol="ssh")