"""Allowlist and input-bound tests (DESIGN.md ยง8 item 1). The allowlist is the main control: without it, `start /bin/sh` makes every other control decorative. """ from __future__ import annotations import tomllib from pathlib import Path import pytest from hhstreamd.policy import ( MAX_ARG_BYTES, MAX_ARGV_BYTES, MAX_ARGV_ITEMS, Policy, PolicyError, check_app_name, check_argv, load_policy, policy_from_dict, ) CONFIG = """ [defaults] max_sessions = 2 idle_timeout_s = 10800 max_idle_timeout_s = 86400 [apps.chromium] exec = "/run/current-system/sw/bin/chromium" args_allow = ["--new-window", "--app=*", "--user-data-dir=*"] [apps.foot] exec = "/usr/bin/foot" [apps.blender] exec = "/usr/bin/blender" args = ["--background"] args_allow = ["*.blend"] idle_timeout_s = 60 """ @pytest.fixture def policy() -> Policy: return policy_from_dict(tomllib.loads(CONFIG), source="test-config") # --------------------------------------------------------------------------- # the allowlist itself # --------------------------------------------------------------------------- def test_an_allowlisted_app_resolves_to_its_absolute_path(policy: Policy): rule, argv = policy.resolve("chromium", ["--new-window"]) assert rule.name == "chromium" assert argv == ["/run/current-system/sw/bin/chromium", "--new-window"] def test_an_app_that_is_not_in_the_allowlist_is_refused_with_a_policy_error(policy: Policy): with pytest.raises(PolicyError) as exc: policy.resolve("sh", []) assert exc.value.error == "not_allowlisted" assert "sh" in exc.value.reason assert "blender, chromium, foot" in exc.value.hint @pytest.mark.parametrize("name", ["/bin/sh", "./sh", "../../bin/sh", "sh;rm -rf /", "a b"]) def test_a_path_or_a_shell_fragment_is_not_even_a_valid_app_name(policy: Policy, name: str): """The caller names an app; it never names a binary.""" with pytest.raises(PolicyError) as exc: policy.resolve(name, []) assert exc.value.error == "invalid_argument" def test_an_empty_allowlist_refuses_everything_and_says_so(): empty = Policy(source="/etc/hhstream/config.toml") with pytest.raises(PolicyError) as exc: empty.resolve("foot", []) assert exc.value.error == "not_allowlisted" assert "the allowlist is empty" in exc.value.hint def test_fixed_args_from_the_config_precede_the_callers(policy: Policy): _, argv = policy.resolve("blender", ["scene.blend"]) assert argv == ["/usr/bin/blender", "--background", "scene.blend"] def test_an_app_rule_may_set_its_own_default_idle_timeout(policy: Policy): assert policy.apps["blender"].idle_timeout_s == 60 assert policy.apps["foot"].idle_timeout_s is None # --------------------------------------------------------------------------- # argument allowlisting # --------------------------------------------------------------------------- def test_arguments_are_matched_against_patterns(policy: Policy): policy.resolve("chromium", ["--app=https://example.com", "--user-data-dir=/tmp/x"]) def test_an_argument_that_matches_no_pattern_is_refused(policy: Policy): with pytest.raises(PolicyError) as exc: policy.resolve("chromium", ["--no-sandbox"]) assert exc.value.error == "argument_refused" assert "--no-sandbox" in exc.value.reason def test_an_app_with_no_args_allow_accepts_no_caller_arguments(policy: Policy): policy.resolve("foot", []) # fine with pytest.raises(PolicyError) as exc: policy.resolve("foot", ["-e", "sh"]) assert exc.value.error == "argument_refused" # --------------------------------------------------------------------------- # config validation # --------------------------------------------------------------------------- def test_a_relative_exec_path_is_rejected_at_load_time(): with pytest.raises(PolicyError) as exc: policy_from_dict({"apps": {"foot": {"exec": "foot"}}}) assert "absolute path" in exc.value.reason def test_an_app_with_no_exec_is_rejected(): with pytest.raises(PolicyError): policy_from_dict({"apps": {"foot": {}}}) @pytest.mark.parametrize("field", ["args", "args_allow"]) def test_args_fields_must_be_lists_of_strings(field): with pytest.raises(PolicyError): policy_from_dict({"apps": {"foot": {"exec": "/usr/bin/foot", field: "oops"}}}) def test_a_missing_config_file_yields_an_empty_allowlist_rather_than_a_crash(tmp_path: Path): """A misconfigured deployment must not look like a daemon crash.""" policy = load_policy(tmp_path / "absent.toml") assert policy.apps == {} assert policy.source.endswith("absent.toml") def test_a_config_file_is_read_from_disk(tmp_path: Path): path = tmp_path / "config.toml" path.write_text(CONFIG) policy = load_policy(path) assert set(policy.apps) == {"chromium", "foot", "blender"} assert policy.max_sessions == 2 # --------------------------------------------------------------------------- # input bounds on `start` # --------------------------------------------------------------------------- def test_argv_must_be_a_list_not_a_string(): """A string here would be the beginning of a shell command line.""" with pytest.raises(PolicyError) as exc: check_argv("--new-window https://example.com") assert exc.value.error == "invalid_argument" def test_argv_items_must_be_strings(): with pytest.raises(PolicyError): check_argv(["--a", 7]) def test_argv_length_is_bounded(): check_argv(["x"] * MAX_ARGV_ITEMS) with pytest.raises(PolicyError) as exc: check_argv(["x"] * (MAX_ARGV_ITEMS + 1)) assert "more than" in exc.value.reason def test_a_single_argument_is_bounded(): with pytest.raises(PolicyError): check_argv(["x" * (MAX_ARG_BYTES + 1)]) def test_the_total_argv_size_is_bounded(): item = "x" * (MAX_ARG_BYTES - 1) count = MAX_ARGV_BYTES // len(item) + 2 with pytest.raises(PolicyError) as exc: check_argv([item] * count) assert "in total" in exc.value.reason def test_a_nul_byte_in_an_argument_is_refused(): """execve would truncate at the NUL; refusing is the honest answer.""" with pytest.raises(PolicyError) as exc: check_argv(["--url=http://x\0--no-sandbox"]) assert "NUL" in exc.value.reason def test_app_names_are_bounded_and_charset_checked(): check_app_name("chromium") check_app_name("my-app_2.0+x") with pytest.raises(PolicyError): check_app_name("") with pytest.raises(PolicyError): check_app_name("a" * 65) with pytest.raises(PolicyError): check_app_name(None) def test_an_idle_timeout_above_the_configured_maximum_is_refused(policy: Policy): assert policy.check_idle_timeout(None) == 10800.0 assert policy.check_idle_timeout(60) == 60.0 with pytest.raises(PolicyError) as exc: policy.check_idle_timeout(86401) assert exc.value.error == "refused" def test_a_negative_or_non_numeric_idle_timeout_is_refused(policy: Policy): with pytest.raises(PolicyError): policy.check_idle_timeout(-1) with pytest.raises(PolicyError): policy.check_idle_timeout("soon") def test_a_zero_idle_timeout_is_accepted_and_means_never_expire(policy: Policy): assert policy.check_idle_timeout(0) == 0.0 def test_the_session_cap_is_enforced(policy: Policy): policy.check_capacity(1) with pytest.raises(PolicyError) as exc: policy.check_capacity(2) assert exc.value.error == "session_cap_reached" assert "max_sessions is 2" in exc.value.reason