"""Unit tests for the parts of sessions-ctl that guard the SSH boundary. Run: python3 -m unittest discover -s dev/tests """ import importlib.util import json import tempfile import unittest from pathlib import Path _spec = importlib.util.spec_from_file_location("sessions_ctl", Path(__file__).parents[1] / "sessions_ctl.py") sc = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(sc) KEY = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPAP6gemgKZVguFi6Diif+dBbMAZQupimeV9OQuUotlw vps-manual" class RemoteParsing(unittest.TestCase): def test_allowed_verbs_pass(self): self.assertEqual(sc.parse_remote("create fix-login papure main"), ["create", "fix-login", "papure", "main"]) self.assertEqual(sc.parse_remote("list"), ["list"]) def test_local_only_verbs_rejected(self): for cmd in ("delete x --yes --force", "refresh-access", "_launch x"): with self.assertRaises(sc.UserError, msg=cmd): sc.parse_remote(cmd) def test_no_command_means_no_shell(self): for cmd in (None, "", " "): with self.assertRaises(sc.UserError): sc.parse_remote(cmd) def test_shell_metacharacters_are_just_text(self): # "list;" is not a verb; nothing is ever handed to a shell. for cmd in ("list; rm -rf ~", "bash -i", "$(id)", "list 'unterminated"): with self.assertRaises(sc.UserError, msg=cmd): sc.parse_remote(cmd) def test_injection_in_arguments_fails_validation(self): argv = sc.parse_remote("status '$(id)'") with self.assertRaises(sc.UserError): sc.validate_name(argv[1]) class Validation(unittest.TestCase): def test_names(self): for ok in ("a", "fix-login", "x" * 32, "9lives"): sc.validate_name(ok) for bad in ("", "-x", "X", "a_b", "a/b", "..", "x" * 33, "a b", "--force"): with self.assertRaises(sc.UserError, msg=bad): sc.validate_name(bad) def test_branches(self): for ok in ("main", "feature/x-1", "v1.2"): sc.validate_branch(ok) for bad in ("-b", "--upload-pack=x", "a..b", "x.lock", "a/", "a b", "~x"): with self.assertRaises(sc.UserError, msg=bad): sc.validate_branch(bad) def test_arity(self): with self.assertRaises(sc.UserError): sc.cmd_repos({"repos": {}}, ["extra"]) class AuthorizedKeys(unittest.TestCase): def test_line_shape(self): line = sc.authorized_line(KEY, ["37.205.10.63"], "/home/agent/.local/bin/sessions-ctl") self.assertEqual( line, 'from="37.205.10.63",restrict,command="/home/agent/.local/bin/sessions-ctl --remote" ' "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPAP6gemgKZVguFi6Diif+dBbMAZQupimeV9OQuUotlw sessions-manager", ) def test_rejects_bad_key_and_ip(self): with self.assertRaises(sc.UserError): sc.authorized_line('ssh-ed25519 AAAA" ,command="sh', ["1.2.3.4"], "/x") with self.assertRaises(sc.UserError): sc.authorized_line(KEY, ['1.2.3.4",command="sh'], "/x") def test_replace_keeps_other_lines(self): other = "ssh-ed25519 AAAAother admin@laptop" first = sc.replace_marked_line(other + "\n", "L1 sessions-manager") self.assertEqual(first, other + "\nL1 sessions-manager\n") second = sc.replace_marked_line(first, "L2 sessions-manager") self.assertEqual(second, other + "\nL2 sessions-manager\n") self.assertEqual(sc.replace_marked_line(second, "L2 sessions-manager"), second) # idempotent class Trust(unittest.TestCase): def test_trust_and_untrust_preserve_other_keys(self): with tempfile.TemporaryDirectory() as d: cj = Path(d) / ".claude.json" cj.write_text(json.dumps({"oauthAccount": {"x": 1}, "projects": {"/a": {"hasTrustDialogAccepted": True}}})) cj.chmod(0o600) sc.trust_workspace(Path("/w/s1"), cj) data = json.loads(cj.read_text()) self.assertTrue(data["projects"]["/w/s1"]["hasTrustDialogAccepted"]) self.assertEqual(data["oauthAccount"], {"x": 1}) self.assertEqual(cj.stat().st_mode & 0o777, 0o600) sc.untrust_workspace(Path("/w/s1"), cj) self.assertEqual(json.loads(cj.read_text())["projects"], {"/a": {"hasTrustDialogAccepted": True}}) class ClaudeArgs(unittest.TestCase): def test_new_then_resume(self): state = {"name": "s1", "uuid": "11111111-2222-3333-4444-555555555555"} with tempfile.TemporaryDirectory() as d: projects = Path(d) first = sc.claude_args(state, "dev-box", projects) self.assertEqual(first[-2:], ["--session-id", state["uuid"]]) self.assertIn("dev-box-s1", first) (projects / "-home-agent-sessions-s1").mkdir() (projects / "-home-agent-sessions-s1" / f"{state['uuid']}.jsonl").write_text("") self.assertEqual(sc.claude_args(state, "dev-box", projects)[-2:], ["--resume", state["uuid"]]) class MainContract(unittest.TestCase): def test_remote_without_command_prints_json_error(self): import contextlib, io, os buf = io.StringIO() os.environ.pop("SSH_ORIGINAL_COMMAND", None) with contextlib.redirect_stdout(buf): rc = sc.main(["--remote"]) self.assertEqual(rc, 1) self.assertFalse(json.loads(buf.getvalue())["ok"]) if __name__ == "__main__": unittest.main()