| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- from __future__ import annotations
- import re
- import subprocess
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[1]
- INSTALL = ROOT / "deploy" / "amd64-release" / "install.sh"
- UNINSTALL = ROOT / "deploy" / "amd64-release" / "uninstall.sh"
- MATRIX = ROOT / "tools" / "run_amd64_release_matrix.sh"
- def _text(path: Path) -> str:
- return path.read_text(encoding="utf-8")
- def _assert_removals_are_inside_helper(
- script: Path, helper: str, next_helper: str, allowed_outside: tuple[str, ...] = ()
- ) -> None:
- source = _text(script)
- helper_start = source.index(f"{helper}() {{")
- helper_end = source.index(f"\n{next_helper}() {{", helper_start)
- offset = 0
- for line_number, line in enumerate(source.splitlines(keepends=True), start=1):
- stripped = line.lstrip()
- if not stripped.startswith("#") and re.search(r"(?<![A-Za-z0-9_])(rm|rmdir)\s", line):
- inside = helper_start <= offset < helper_end
- explicitly_allowed = any(fragment in line for fragment in allowed_outside)
- assert inside or explicitly_allowed, (
- f"unguarded removal in {script.name}:{line_number}: {line.rstrip()}"
- )
- offset += len(line)
- def test_release_shell_entrypoints_parse() -> None:
- for script in (INSTALL, UNINSTALL, MATRIX):
- subprocess.run(["bash", "-n", str(script)], check=True)
- def test_all_tracked_shell_entrypoints_are_executable() -> None:
- scripts = sorted(ROOT.rglob("*.sh"))
- assert scripts
- for script in scripts:
- assert script.stat().st_mode & 0o111 == 0o111, f"not executable: {script}"
- def test_uninstall_allowlist_covers_every_owned_target_type() -> None:
- source = _text(UNINSTALL)
- expected = {
- "file:/usr/local/bin/hthd",
- "file:/usr/local/bin/hthctl",
- "file:/usr/local/bin/hth-capture",
- "file:/etc/systemd/system/hthd.service",
- "file:/etc/systemd/system/hthd@.service",
- "file:/usr/lib/sysusers.d/hth.conf",
- "file:/usr/lib/tmpfiles.d/hth.conf",
- "file:/usr/local/share/hth/sdk/hth_sdk-*.whl",
- "config:/etc/hth/hthd.yaml",
- "config:/etc/hth/release-metadata.json",
- "config:/etc/hth/install-manifest.txt",
- "config:/etc/hth/profiles/6400-control/profile.json",
- "config:/etc/hth/profiles/prod2u-door-temp/profile.json",
- "symlink:/etc/systemd/system/multi-user.target.wants/hthd.service",
- "managed_tree:/run/hth",
- "managed_tree:/var/lib/hth/captures",
- "managed_tree:/var/lib/hth/replay",
- "managed_tree:/var/lib/hth/receipts",
- "owned_dir:/usr/local/share/hth/sdk",
- "owned_dir:/usr/local/share/hth",
- "owned_dir:/etc/hth/profiles/6400-control",
- "owned_dir:/etc/hth/profiles/prod2u-door-temp",
- "owned_dir:/etc/hth/profiles",
- "owned_dir:/etc/hth",
- "owned_dir:/var/lib/hth",
- }
- missing = sorted(entry for entry in expected if entry not in source)
- assert not missing, f"uninstall allowlist is missing: {missing}"
- assert 'resolved="$(realpath -m -s -- "$path")"' in source
- assert '[[ "$resolved" == "$path" ]]' in source
- assert '${resolved#/usr/local/share/hth/sdk/}' in source
- assert '!= */*' in source
- def test_symlink_parent_guard_rejects_adversarial_parent(tmp_path: Path) -> None:
- source = _text(UNINSTALL)
- start = source.index("no_symlink_parents() {")
- end = source.index("\nsafe_path() {", start)
- helper = source[start:end]
- real_parent = tmp_path / "real-parent"
- linked_parent = tmp_path / "linked-parent"
- real_parent.mkdir()
- linked_parent.symlink_to(real_parent, target_is_directory=True)
- real_result = subprocess.run(
- ["bash", "-c", f'{helper}\nno_symlink_parents "$1"', "guard", str(real_parent / "file")],
- check=False,
- )
- linked_result = subprocess.run(
- ["bash", "-c", f'{helper}\nno_symlink_parents "$1"', "guard", str(linked_parent / "file")],
- check=False,
- )
- assert real_result.returncode == 0
- assert linked_result.returncode != 0
- assert 'no_symlink_parents "$resolved"' in source
- def test_product_removals_use_the_guarded_dispatchers() -> None:
- _assert_removals_are_inside_helper(
- UNINSTALL, "remove_owned_path", "manifest_identity_rejected"
- )
- _assert_removals_are_inside_helper(
- INSTALL,
- "remove_product_path",
- "identity_allowed",
- allowed_outside=('temp_config:-',),
- )
- assert 'rm -f -- "$SHAREDIR/sdk"/*.whl' not in _text(INSTALL)
- def test_current_manifest_never_claims_service_identity_ownership() -> None:
- install = _text(INSTALL)
- marker = 'cat >"$ETCDIR/install-manifest.txt" <<EOF'
- manifest = install.split(marker, 1)[1].split("\nEOF", 1)[0]
- assert "\naccount " not in manifest
- assert "\ngroup " not in manifest
- assert "\ncreated_account " not in manifest
- assert "\ncreated_group " not in manifest
- assert 'echo "created_account $identity_name"' not in install
- assert 'echo "created_group $identity_name"' not in install
- assert "/etc/hth/profiles/prod2u-door-temp" not in manifest
- assert "multi-user.target.wants/hthd.service" not in manifest
- assert 'if [[ -L "$UNITDIR/multi-user.target.wants/hthd.service" ]]' in install
- def test_purge_never_deletes_service_identities() -> None:
- uninstall = _text(UNINSTALL)
- assert "userdel " not in uninstall
- assert "groupdel " not in uninstall
- assert "current manifest cannot claim service identity ownership" in uninstall
- assert "created_account|created_group) manifest_identity_rejected" in uninstall
- def test_manifestless_paths_fail_closed_and_legacy_upgrade_is_fingerprinted() -> None:
- install = _text(INSTALL)
- uninstall = _text(UNINSTALL)
- expected_hashes = {
- "498ee4146b2d44490d58d0e13404978331772fd85bf546f5eea7cdded8c55ad8",
- "b57403ff3d3dfc0163aba4b35af33d26cb9aa64e54e4017e52f124aae0cf2421",
- "382e3e72f61732d26ce77b50323432c8ad534dffba2bea6b9ec029f89fcd1f43",
- "a6a83979d4d2195adf4704719dc750c0abdd48e803f6f8cc178234695d4afc7e",
- "4894e25647825ea614f8ffaffd72c66b0dc31c6efda1c8481dddd2316a07ae96",
- "3c783b5dfb7a17ed1f44eccb1956c5a8ec25e5daebab5d5287111c1387166364",
- "348a07b6b23a9fd3e860b830c15e0bdea17d1da5f28a380bea4065f6fbe800ac",
- }
- assert expected_hashes.issubset(set(re.findall(r"[0-9a-f]{64}", install)))
- assert 'if [[ "$ACTION" == upgrade && ! -f "$OLD_MANIFEST" ]]' in install
- assert "verify_legacy_v011" in install
- assert "unexpected hthd@.service beside legacy v0.1.1" in install
- assert "unknown old manifest kind" in install
- assert "old manifest is incomplete" in install
- assert "trusted install manifest requires release metadata" in install
- assert "old manifest claims an unowned legacy door-temperature path" in install
- for child_dir in (
- '"$SHAREDIR/sdk"',
- '"$ETCDIR/profiles"',
- '"$ETCDIR/profiles/6400-control"',
- '"$STATEDIR/captures"',
- '"$STATEDIR/replay"',
- '"$STATEDIR/receipts"',
- ):
- assert child_dir in install
- assert '"$ETCDIR/profiles/6400-control/profile.json"' in install
- assert '"$LIBDIR/sysusers.d/hth.conf"' in install
- assert "find \"$SHAREDIR/sdk\" -maxdepth 1" in install
- assert "HTH-like files exist without a trusted install manifest" in uninstall
- assert "declare -a FILES=()" in uninstall
- assert "declare -a CONFIGS=()" in uninstall
- footprint = install.split("FOOTPRINT=0", 1)[1].split(
- "declare -A OLD_MANIFEST_ENTRIES", 1
- )[0]
- for managed_tree in (
- '"$RUNDIR"',
- '"$STATEDIR/captures"',
- '"$STATEDIR/replay"',
- '"$STATEDIR/receipts"',
- ):
- assert managed_tree in footprint
- def test_uninstall_requires_strict_current_manifest_before_mutation() -> None:
- uninstall = _text(UNINSTALL)
- validation_end = uninstall.index('systemctl disable --now "$SERVICE"')
- validator = uninstall[:validation_end]
- assert '[[ -f "$ROOT/RELEASE-METADATA.json" ]]' in validator
- assert 'trusted install manifest requires installed release metadata' in validator
- assert 'installed release metadata does not match this uninstall package' in validator
- assert 'install manifest header does not match installed release metadata' in validator
- assert 'immutable installed file does not match uninstall package' in validator
- assert 'required immutable installed file is missing or not regular' in validator
- for artifact in (
- '"$BINDIR/hthd" "$ROOT/bin/hthd"',
- '"$BINDIR/hthctl" "$ROOT/bin/hthctl"',
- '"$BINDIR/hth-capture" "$ROOT/bin/hth-capture"',
- '"$UNITDIR/hthd.service" "$ROOT/service/hthd.service"',
- '"$LIBDIR/sysusers.d/hth.conf" "$ROOT/service/hth.conf"',
- '"$LIBDIR/tmpfiles.d/hth.conf"',
- '"$ETCDIR/profiles/6400-control/profile.json"',
- '"$EXPECTED_WHEEL" "${PACKAGE_WHEELS[0]}"',
- '"$ETCDIR/release-metadata.json"',
- ):
- assert artifact in validator
- assert 'duplicate current manifest entry' in validator
- assert 'current manifest is incomplete' in validator
- assert 'current manifest must contain exactly one owned SDK wheel' in validator
- assert 'entry is not valid for a current HTH manifest' in validator
- assert "userdel " not in validator
- assert "groupdel " not in validator
- assert not any(
- line.lstrip().startswith("remove_owned_path ") for line in validator.splitlines()
- )
- purge_residual = uninstall.split('if [[ "$PURGE" -eq 1 ]]; then', 2)[2]
- assert '"$ETCDIR/profiles/prod2u-door-temp/profile.json"' not in purge_residual
- manifestless_footprint = uninstall.split('if [[ ! -f "$MANIFEST" ]]', 1)[1].split(
- '[[ -f "$ROOT/RELEASE-METADATA.json" ]]', 1
- )[0]
- assert '"$ETCDIR/profiles/prod2u-door-temp/profile.json"' not in manifestless_footprint
- def test_matrix_preserves_preexisting_identities() -> None:
- matrix = _text(MATRIX)
- assert "SCENARIO_PREEXISTING_IDENTITIES" in matrix
- assert 'identities_before="$(identity_snapshot)"' in matrix
- assert 'identities_after="$(identity_snapshot)"' in matrix
- assert '[ "$identities_before" = "$identities_after" ]' in matrix
- assert "service identity was incorrectly claimed by HTH" in matrix
- assert "preexisting_identity_preservation=PASS" in matrix
- def test_matrix_contains_destructive_path_adversarial_scenarios() -> None:
- matrix = _text(MATRIX)
- assert "SCENARIO_SYMLINK_PATH_REJECTION" in matrix
- assert "SCENARIO_FAKE_MANIFEST_REJECTION" in matrix
- assert "SCENARIO_UNKNOWN_LEGACY_PROFILE_PRESERVED" in matrix
- assert "SCENARIO_CURRENT_PROFILE_SENTINEL_REJECTION" in matrix
- assert "SCENARIO_MANAGED_TREE_SENTINEL_REJECTION" in matrix
- assert "SCENARIO_FAKE_MANIFEST_UNINSTALL_REJECTION" in matrix
- assert "SCENARIO_FULL_MANIFEST_PARTIAL_CLOSURE_REJECTION" in matrix
- assert "install unexpectedly accepted symlink path" in matrix
- assert "upgrade unexpectedly accepted a fake manifest" in matrix
- assert "symlink_path_rejection=PASS" in matrix
- assert "fake_manifest_rejection=PASS" in matrix
- assert "unknown_legacy_profile_preserved=PASS" in matrix
- assert "current_profile_sentinel_rejection=PASS" in matrix
- assert "managed_tree_sentinel_rejection=PASS" in matrix
- assert "fake_manifest_uninstall_rejection=PASS" in matrix
- assert "full_manifest_partial_closure_rejection=PASS" in matrix
- assert "/etc/hth/profiles/prod2u-door-temp/profile.json" in matrix
|