diff --git a/scripts/e2e/catalog.json b/scripts/e2e/catalog.json index 00bf957ce1..7dfdd54199 100644 --- a/scripts/e2e/catalog.json +++ b/scripts/e2e/catalog.json @@ -42,12 +42,6 @@ "check": "plugin_cache_integrity", "message": "Plugin cache directory is absent, so hook paths could not be verified: /home/autoskillit/.claude/plugins/cache/autoskillit-local/autoskillit. Run `autoskillit install` to rebuild it.", "issue": "https://github.com/TalonT-Org/AutoSkillit/issues/5243" - }, - { - "severity": "error", - "check": "installation_record_invalid", - "message": "Invalid RECORD path '../../../bin/autoskillit' in /home/autoskillit/.local/share/uv/tools/autoskillit/lib/python3.11/site-packages/autoskillit (active distribution: /home/autoskillit/.local/share/uv/tools/autoskillit/lib/python3.11/site-packages/autoskillit; uv tool: /home/autoskillit/.local/share/uv/tools/autoskillit/lib/python3.11/site-packages/autoskillit)", - "issue": "https://github.com/TalonT-Org/AutoSkillit/issues/5244" } ] }, diff --git a/src/autoskillit/cli/doctor/_doctor_install.py b/src/autoskillit/cli/doctor/_doctor_install.py index c5c8fb9560..c3592c1988 100644 --- a/src/autoskillit/cli/doctor/_doctor_install.py +++ b/src/autoskillit/cli/doctor/_doctor_install.py @@ -15,6 +15,8 @@ from pathlib import Path from typing import TYPE_CHECKING +import regex as re + from autoskillit.core import Severity, file_url_path, get_logger, is_python_bytecode_path from ._doctor_types import DoctorResult @@ -450,53 +452,78 @@ def _regular_files(root: Path) -> Iterator[Path]: return +def _scheme_base(site_packages: Path) -> Path: + """Return the install-scheme base that bounds RECORD rows. + + Installers record console scripts and data relative to purelib, e.g. + ``../../../bin/autoskillit`` from ``/lib/pythonX.Y/site-packages`` + (also ``lib64`` and ``dist-packages``; ``/Lib/site-packages`` + on Windows). Standard + script and data targets lie under ````. Other layouts, such as + an unpacked uv cache archive, are bounded to themselves. + """ + lib = site_packages.parent + if site_packages not in (lib / "site-packages", lib / "dist-packages"): + return site_packages + if re.fullmatch(r"python[0-9]+\.[0-9]+t?", lib.name) and lib.parent.name in {"lib", "lib64"}: + base = lib.parent.parent + elif lib == lib.parent / "Lib": + base = lib.parent + else: + return site_packages + return base if base.parent != base else site_packages + + def _verify_record_entry( installation: _Installation, site_packages: Path, + bound: Path, row: list[str], tracked: set[str], ) -> DoctorResult | None: if len(row) < 2: return None - relative = Path(row[0]) - if relative.is_absolute() or ".." in relative.parts: - return DoctorResult( - Severity.ERROR, - "installation_record_invalid", - f"Invalid RECORD path {row[0]!r} in {_display(installation)}", - ) - candidate = site_packages / relative + candidate = site_packages / row[0] try: - package_relative = candidate.relative_to(installation.package_root) - except ValueError: - pass - else: - tracked.add(package_relative.as_posix()) - if not row[1]: - return None - parsed_hash = _record_hash(row[1]) - if parsed_hash is None: + candidate = candidate.resolve() + if not candidate.is_relative_to(bound): + return DoctorResult( + Severity.ERROR, + "installation_record_invalid", + f"Invalid RECORD path {row[0]!r} outside {bound} in {_display(installation)}", + ) + try: + package_relative = candidate.relative_to(installation.package_root) + except ValueError: + pass + else: + tracked.add(package_relative.as_posix()) + if not row[1]: + return None + parsed_hash = _record_hash(row[1]) + if parsed_hash is None: + return DoctorResult( + Severity.ERROR, + "installation_record_invalid", + f"Invalid RECORD hash for {row[0]} in {_display(installation)}", + ) + algorithm, expected = parsed_hash + content = candidate.read_bytes() + except (OSError, RuntimeError, ValueError): return DoctorResult( Severity.ERROR, - "installation_record_invalid", - f"Invalid RECORD hash for {row[0]} in {_display(installation)}", + "installation_record_unreadable_entry", + f"Unreadable RECORD entry {candidate} in {_display(installation)}", ) - algorithm, expected = parsed_hash try: - observed = hashlib.new(algorithm, candidate.read_bytes()).digest() - except ValueError: + observed = hashlib.new(algorithm, content).digest() + except (TypeError, ValueError): return DoctorResult( Severity.ERROR, - "installation_record_unknown_algorithm", - f"Unknown RECORD hash algorithm {algorithm!r} " + "installation_record_unsupported_algorithm", + f"Unsupported RECORD hash algorithm {algorithm!r} " f"for {candidate} in {_display(installation)}", ) - except OSError: - return DoctorResult( - Severity.ERROR, - "installation_record_unreadable_entry", - f"Unreadable RECORD entry {candidate} in {_display(installation)}", - ) if observed != expected: return DoctorResult( Severity.ERROR, @@ -511,10 +538,11 @@ def _verify_record(installation: _Installation) -> tuple[list[DoctorResult], set results: list[DoctorResult] = [] tracked: set[str] = set() site_packages = installation.record_path.parent.parent + bound = _scheme_base(site_packages).resolve() try: rows = csv.reader(installation.record_path.read_text(encoding="utf-8").splitlines()) for row in rows: - result = _verify_record_entry(installation, site_packages, row, tracked) + result = _verify_record_entry(installation, site_packages, bound, row, tracked) if result is not None: results.append(result) except (OSError, UnicodeDecodeError, csv.Error) as exc: diff --git a/tests/arch/test_autoskillit_root_children_registered.py b/tests/arch/test_autoskillit_root_children_registered.py index c865e74668..3a6ee8b9c8 100644 --- a/tests/arch/test_autoskillit_root_children_registered.py +++ b/tests/arch/test_autoskillit_root_children_registered.py @@ -16,17 +16,19 @@ # These names are accounted for elsewhere: home-level artifacts, a read-only # legacy path, and a runtime directory with a real-git proof. _CLASSIFIED_ELSEWHERE: dict[str, int] = { - # Home-level hook state: hooks/guards/mcp_health_advisor.py:36. + # Home-level hook state: hooks/guards/mcp_health_advisor.py:_active_kitchens_path. "active_kitchens.json": 1, - # Home-level marketplace shape: core/types/constants/_type_constants_retirements.py:309. + # Home-level marketplace shape: + # core/types/constants/_type_constants_retirements.py:RETIRED_INSTALL_ARTIFACT_SHAPES. "marketplace": 1, - # Home-level plugin storage: cli/doctor/_doctor_install.py:355. + # Home-level plugin storage: cli/doctor/_doctor_install.py:_owned_installation_roots. "plugin-generations": 1, - # Home-level hook projection repair: workspace/_projected_artifact/_hook_repair.py:538. + # Home-level hook projection repair: + # workspace/_projected_artifact/_hook_repair.py:repair_broken_projection_hooks. "plugin-projections": 1, - # Home-level dismissal state: cli/update/_update_checks.py:79. + # Home-level dismissal state: cli/update/_update_checks.py:_read_dismiss_state. "update_check.json": 1, - # Legacy path is read only for migration: cli/install/_marketplace.py:162. + # Legacy path is read only for migration: cli/install/_marketplace.py:_ensure_workspace_ready. "scripts": 1, # Runtime self-ignore is proven by tests/workspace/test_clone_gate_runtime_state.py. "managed-headless-session-lineage": 1, diff --git a/tests/cli/_installation_integrity_helpers.py b/tests/cli/_installation_integrity_helpers.py new file mode 100644 index 0000000000..d5548c96ec --- /dev/null +++ b/tests/cli/_installation_integrity_helpers.py @@ -0,0 +1,24 @@ +"""Shared RECORD fixtures for installation-integrity tests.""" + +from __future__ import annotations + +import base64 +import hashlib + +from autoskillit.cli.doctor._doctor_types import DoctorResult +from autoskillit.core import Severity + +CLEAN = DoctorResult( + Severity.OK, + "installation_integrity", + "All discoverable AutoSkillit installations match their RECORD files", +) + + +def record_hash(content: bytes) -> str: + encoded = base64.urlsafe_b64encode(hashlib.sha256(content).digest()).rstrip(b"=") + return f"sha256={encoded.decode()}" + + +def record_text(rows: list[str], dist_info: str) -> str: + return "\n".join([*rows, f"{dist_info}/RECORD,,"]) + "\n" diff --git a/tests/cli/test_installation_integrity.py b/tests/cli/test_installation_integrity.py index c428b11f12..35a78984ed 100644 --- a/tests/cli/test_installation_integrity.py +++ b/tests/cli/test_installation_integrity.py @@ -2,8 +2,6 @@ from __future__ import annotations -import base64 -import hashlib import os import sys from pathlib import Path @@ -14,31 +12,203 @@ import autoskillit.cli.update._update_checks as _update_checks from autoskillit.cli.install._install_info import InstallInfo, InstallType from autoskillit.core import Severity +from tests.cli._installation_integrity_helpers import CLEAN, record_hash, record_text pytestmark = [pytest.mark.layer("cli"), pytest.mark.small] -def _record_hash(content: bytes) -> str: - encoded = base64.urlsafe_b64encode(hashlib.sha256(content).digest()).rstrip(b"=") - return f"sha256={encoded.decode()}" - - -def _write_install(root: Path, *, content: bytes = b"VERSION = '1.0'\n") -> Path: - site_packages = root / "autoskillit" / "lib" / "python3.14" / "site-packages" +def _write_install( + root: Path, + *, + content: bytes = b"VERSION = '1.0'\n", + layout: tuple[str, ...] = ("lib", "python3.14", "site-packages"), +) -> Path: + env = root / "autoskillit" + site_packages = env.joinpath(*layout) package = site_packages / "autoskillit" package.mkdir(parents=True) - module = package / "__init__.py" - module.write_bytes(content) + script_parts = ( + ("Scripts", "autoskillit.exe") + if layout == ("Lib", "site-packages") + else ("bin", "autoskillit") + ) + script = env.joinpath(*script_parts) + script.parent.mkdir(parents=True) + files = {package / "__init__.py": content, script: b"#!/bin/sh\nexec python -m autoskillit\n"} + for path, data in files.items(): + path.write_bytes(data) record = site_packages / "autoskillit-1.0.0.dist-info" / "RECORD" record.parent.mkdir() - record.write_text(f"autoskillit/__init__.py,{_record_hash(content)},{len(content)}\n") + rows = [ + f"{Path(os.path.relpath(path, site_packages)).as_posix()},{record_hash(data)},{len(data)}" + for path, data in files.items() + ] + record.write_text(record_text(rows, record.parent.name)) return package +def _record(package: Path) -> Path: + return package.parent / "autoskillit-1.0.0.dist-info" / "RECORD" + + def _findings(*roots: Path) -> list: return _integrity.verify_installations(roots=roots) +@pytest.mark.parametrize( + "layout", + [ + ("lib", "python3.14", "site-packages"), + ("lib64", "python3.14", "site-packages"), + ("lib", "python3.14", "dist-packages"), + ("Lib", "site-packages"), + ], +) +def test_clean_install_reports_integrity_ok(tmp_path: Path, layout: tuple[str, ...]) -> None: + _write_install(tmp_path, layout=layout) + + assert _findings(tmp_path) == [CLEAN] + + +def test_console_script_mutation_detected(tmp_path: Path) -> None: + _write_install(tmp_path) + script = tmp_path / "autoskillit" / "bin" / "autoskillit" + script.write_bytes(b"mutated script") + + assert any( + finding.check == "installation_record_mismatch" + and str(script.resolve()) in finding.message + for finding in _findings(tmp_path) + ) + + +@pytest.mark.parametrize("case", ["parent_escape", "sibling_prefix", "absolute"]) +def test_record_row_outside_installation_is_invalid(tmp_path: Path, case: str) -> None: + package = _write_install(tmp_path) + outside = tmp_path / "outside.txt" + row = { + "parent_escape": "../../../../outside.txt", + "sibling_prefix": "../../../../autoskillit-evil/x.py", + "absolute": str(outside.resolve()), + }[case] + record = _record(package) + record.write_text(record.read_text() + f"{row},{record_hash(b'x')},1\n") + + assert any( + finding.check == "installation_record_invalid" and repr(row) in finding.message + for finding in _findings(tmp_path) + ) + + +@pytest.mark.parametrize( + "case", + [ + "absolute_script", + "redundant_dotdot", + pytest.param( + "double_slash", + marks=pytest.mark.skipif(os.name == "nt", reason="POSIX double-leading slash"), + ), + ], +) +def test_equivalent_spellings_inside_installation_stay_clean(tmp_path: Path, case: str) -> None: + package = _write_install(tmp_path) + record = _record(package) + old, new = ( + ("../../../bin/autoskillit", str((tmp_path / "autoskillit/bin/autoskillit").resolve())) + if case == "absolute_script" + else ("autoskillit/__init__.py", "autoskillit/../autoskillit/__init__.py") + ) + if case == "double_slash": + old = "../../../bin/autoskillit" + new = "/" + str((tmp_path / "autoskillit/bin/autoskillit").resolve()) + record.write_text(record.read_text().replace(old, new)) + + assert _findings(tmp_path) == [CLEAN] + + +@pytest.mark.parametrize( + ("target", "expected"), + [ + ("outside", "installation_record_invalid"), + ("loop", "installation_record_unreadable_entry"), + ], +) +def test_record_symlink_target_is_checked(tmp_path: Path, target: str, expected: str) -> None: + package = _write_install(tmp_path) + link = package / "linked.py" + if target == "outside": + destination = tmp_path / "outside.py" + destination.write_bytes(b"x") + else: + destination = link + link.symlink_to(destination) + record = _record(package) + record.write_text(record.read_text() + f"autoskillit/linked.py,{record_hash(b'x')},1\n") + + assert any(finding.check == expected for finding in _findings(tmp_path)) + + +def test_unrecognized_layout_is_bounded_to_itself(tmp_path: Path) -> None: + package = _write_install(tmp_path, layout=("archive",)) + + assert any( + finding.check == "installation_record_invalid" + and f"outside {package.parent.resolve()}" in finding.message + for finding in _findings(tmp_path) + ) + + +@pytest.mark.parametrize( + ("site_packages", "expected"), + [ + ("/e/lib/python3.13/site-packages", "/e"), + ("/lib/python3.13/site-packages", "/lib/python3.13/site-packages"), + ("/Lib/site-packages", "/Lib/site-packages"), + ("/e/lib64/python3.13/site-packages", "/e"), + ("/e/lib/python3.13t/site-packages", "/e"), + ("/e/Lib/site-packages", "/e"), + ("/e/lib/python3.13/dist-packages", "/e"), + ("/e/share/python3.13/site-packages", "/e/share/python3.13/site-packages"), + ("/e/lib/pythonjunk/site-packages", "/e/lib/pythonjunk/site-packages"), + ("/c/archive-v0/abc", "/c/archive-v0/abc"), + pytest.param( + "C:/e/LIB/SITE-PACKAGES", + "C:/e", + marks=pytest.mark.skipif(os.name != "nt", reason="Native Windows path casing"), + ), + ], +) +def test_scheme_base(site_packages: str, expected: str) -> None: + assert _integrity._scheme_base(Path(site_packages)) == Path(expected) + + +@pytest.mark.parametrize( + ("case", "expected"), + [ + ("nul_path", "installation_record_unreadable_entry"), + ("variable_length_digest", "installation_record_unsupported_algorithm"), + ("unknown_digest", "installation_record_unsupported_algorithm"), + ], +) +def test_read_and_digest_failures_are_distinguished( + tmp_path: Path, case: str, expected: str +) -> None: + package = _write_install(tmp_path) + record = _record(package) + rows = record.read_text() + if case == "nul_path": + rows += f"autoskillit/a\x00b.py,{record_hash(b'x')},1\n" + else: + algorithm = "shake_128" if case == "variable_length_digest" else "not_a_hash_algorithm" + rows = rows.replace( + record_hash((package / "__init__.py").read_bytes()), f"{algorithm}=AAAA" + ) + record.write_text(rows) + + assert any(finding.check == expected for finding in _findings(tmp_path)) + + def test_record_mismatch_detected(tmp_path: Path) -> None: package = _write_install(tmp_path / "install") (package / "__init__.py").write_text("VERSION = 'mutated'\n") diff --git a/tests/cli/test_installation_integrity_uv.py b/tests/cli/test_installation_integrity_uv.py new file mode 100644 index 0000000000..edf5ca1096 --- /dev/null +++ b/tests/cli/test_installation_integrity_uv.py @@ -0,0 +1,76 @@ +"""Medium, offline coverage of installation integrity using a real uv-installed wheel.""" + +from __future__ import annotations + +import csv +import subprocess +import sys +import zipfile +from pathlib import Path + +import pytest + +import autoskillit.cli.doctor._doctor_install as _integrity +from tests.cli._installation_integrity_helpers import CLEAN, record_hash, record_text +from tests.conftest import production_interpreter_env + +pytestmark = [pytest.mark.layer("cli"), pytest.mark.medium] + + +def test_uv_tool_install_verifies_clean(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + wheel = tmp_path / "dist" / "autoskillit-1.0.0-py3-none-any.whl" + wheel.parent.mkdir(parents=True) + dist_info = "autoskillit-1.0.0.dist-info" + members = { + "autoskillit/__init__.py": b"def main() -> int:\n return 0\n", + f"{dist_info}/METADATA": b"Metadata-Version: 2.1\nName: autoskillit\nVersion: 1.0.0\n", + f"{dist_info}/WHEEL": ( + b"Wheel-Version: 1.0\nGenerator: autoskillit-tests\n" + b"Root-Is-Purelib: true\nTag: py3-none-any\n" + ), + f"{dist_info}/entry_points.txt": b"[console_scripts]\nautoskillit = autoskillit:main\n", + } + rows = [f"{name},{record_hash(content)},{len(content)}" for name, content in members.items()] + with zipfile.ZipFile(wheel, "w") as archive: + for name, content in members.items(): + archive.writestr(name, content) + archive.writestr(f"{dist_info}/RECORD", record_text(rows, dist_info)) + + home = tmp_path / "home" + tool_dir = home / ".local" / "share" / "uv" / "tools" + result = subprocess.run( + [ + "uv", + "tool", + "install", + "--offline", + "--no-cache", + "--python", + sys.executable, + str(wheel), + ], + env={ + **production_interpreter_env(), + "UV_TOOL_DIR": str(tool_dir), + "UV_TOOL_BIN_DIR": str(tool_dir.parent / ".tools-bin"), + "UV_NO_CONFIG": "1", + "UV_PYTHON_DOWNLOADS": "never", + }, + cwd=tmp_path, + capture_output=True, + text=True, + timeout=50, + ) + assert result.returncode == 0, result.stdout + result.stderr + record = next( + (tool_dir / "autoskillit").glob( + "lib/python*/site-packages/autoskillit-1.0.0.dist-info/RECORD" + ) + ) + assert "../../../bin/autoskillit" in { + row[0] for row in csv.reader(record.read_text().splitlines()) + } + + monkeypatch.setattr(_integrity, "_active_record_paths", lambda: iter(())) + findings = _integrity.verify_installations(home=home) + assert findings == [CLEAN], "\n".join(finding.message for finding in findings)