diff --git a/lanes/dev1/repair_overlay/packaging/build_windows.ps1 b/lanes/dev1/repair_overlay/packaging/build_windows.ps1 index af756fee0..99b9fdaa5 100644 --- a/lanes/dev1/repair_overlay/packaging/build_windows.ps1 +++ b/lanes/dev1/repair_overlay/packaging/build_windows.ps1 @@ -196,7 +196,7 @@ $updaterManifest = [ordered]@{ size_bytes = $updaterSize sha256 = $updaterHash git_sha = $actualGitSha - application_version_authority = "scripture_archive_platform.desktop_host.version.CURRENT_APPLICATION_VERSION" + application_version_authority = "host-owned UpdaterProcessPlan.current_version + runner exact-handoff equality" authenticode_authority = "scripture_archive_platform.desktop_host.authenticode.verify_same_publisher_authenticode" } $updaterManifest | ConvertTo-Json -Depth 4 | Set-Content -Encoding utf8 $UpdaterManifestPath diff --git a/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/main.py b/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/main.py index ccaa3a57d..b6a4ac055 100644 --- a/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/main.py +++ b/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/main.py @@ -98,7 +98,10 @@ def main() -> int: ) selector.bind_window(window) pending_layer.bind_apply_execution( - lambda: launch_packaged_updater(staging_root), + lambda: launch_packaged_updater( + staging_root, + current_version=CURRENT_APPLICATION_VERSION, + ), window.destroy, ) logging.info("Starting EdgeChromium WebView; frontend=%s log=%s", front, log_path) diff --git a/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_launch.py b/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_launch.py index e99098bf7..630e1ee15 100644 --- a/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_launch.py +++ b/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_launch.py @@ -33,6 +33,7 @@ def packaged_updater_path(installed_executable: Path) -> Path: def launch_packaged_updater( staging_root: str | os.PathLike[str], *, + current_version: str, current_executable: str | os.PathLike[str] | None = None, verify_same_publisher: PublisherVerifier = verify_same_publisher_authenticode, build_plan: PlanBuilder = build_updater_process_plan, @@ -40,7 +41,7 @@ def launch_packaged_updater( ) -> int: """Verify and launch only the canonical same-publisher sibling updater. - The browser supplies none of these values. The installed executable defaults to + The browser supplies none of these values, including ``current_version``. The installed executable defaults to the currently running packaged process, the updater path is mechanically derived beside it, and the existing Authenticode authority must positively prove that current host and updater share a valid signer before #245's fixed process plan is @@ -68,6 +69,7 @@ def launch_packaged_updater( updater_executable=updater, installed_executable=installed, staging_root=requested_staging, + current_version=current_version, ) if not isinstance(plan, UpdaterProcessPlan): raise UpdateProcessError("updater process-plan builder returned an invalid plan") @@ -81,6 +83,7 @@ def launch_packaged_updater( plan.updater_executable != expected_updater or plan.installed_executable != expected_installed or plan.staging_root != expected_staging + or plan.current_version != current_version ): raise UpdateProcessError("updater process plan changed host-owned launch authority") try: diff --git a/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_main.py b/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_main.py index c4d04f1f9..a58f261f9 100644 --- a/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_main.py +++ b/lanes/dev1/repair_overlay/scripture_archive_platform/desktop_host/updater_main.py @@ -9,7 +9,6 @@ from scripture_archive_platform.desktop_host.authenticode import ( verify_same_publisher_authenticode, ) -from scripture_archive_platform.desktop_host.version import CURRENT_APPLICATION_VERSION from runtime_engine.scripture_archive_runtime.application_update_process import ( parse_trusted_updater_argv, ) @@ -27,10 +26,10 @@ def _resolve_packaged_installed_sibling( argv: Sequence[str], *, updater_executable: Path, -) -> tuple[Path, Path]: +) -> tuple[Path, Path, str]: """Bind direct invocation to the exact app sibling of this updater executable.""" - _, installed_argument, _ = parse_trusted_updater_argv(tuple(argv)) + _, installed_argument, _, current_version = parse_trusted_updater_argv(tuple(argv)) updater = Path(updater_executable) updater_meta = updater.lstat() if stat.S_ISLNK(updater_meta.st_mode) or not stat.S_ISREG(updater_meta.st_mode): @@ -47,7 +46,7 @@ def _resolve_packaged_installed_sibling( installed = installed_argument.resolve(strict=True) if installed != expected_installed: raise ValueError("installed application is not the canonical updater sibling") - return installed, updater + return installed, updater, current_version def run_updater( @@ -62,13 +61,15 @@ def run_updater( Direct invocation is bound to the exact application sibling mechanically derived from this updater's own executable identity. That installed app and this updater must also share a valid publisher before the runner can consume any apply handoff. - The runner independently verifies installed app versus staged candidate again. + The running host carries its current semantic version in the fixed process handoff, + so an older same-publisher sibling updater remains usable after the app executable updates. + The runner independently revalidates that handoff version and the staged candidate. """ if not callable(execute) or not callable(verifier): return UPDATER_FAILED try: - installed, updater = _resolve_packaged_installed_sibling( + installed, updater, current_version = _resolve_packaged_installed_sibling( tuple(argv), updater_executable=Path(sys.executable if updater_executable is None else updater_executable), ) @@ -76,7 +77,7 @@ def run_updater( return UPDATER_FAILED execute( tuple(argv), - current_version=CURRENT_APPLICATION_VERSION, + current_version=current_version, verify_same_publisher=verifier, ) except Exception: diff --git a/lanes/dev1/repair_overlay/tests/test_packaged_updater_entrypoint.py b/lanes/dev1/repair_overlay/tests/test_packaged_updater_entrypoint.py index 053dd0a73..5e25bb26f 100644 --- a/lanes/dev1/repair_overlay/tests/test_packaged_updater_entrypoint.py +++ b/lanes/dev1/repair_overlay/tests/test_packaged_updater_entrypoint.py @@ -9,7 +9,7 @@ UPDATER_OK, run_updater, ) -from scripture_archive_platform.desktop_host.version import CURRENT_APPLICATION_VERSION +HOST_CURRENT_VERSION = "0.6.2" class PackagedUpdaterEntrypointTests(unittest.TestCase): @@ -28,11 +28,13 @@ def setUp(self) -> None: def tearDown(self) -> None: self.temp.cleanup() - def argv(self, installed: Path | None = None) -> tuple[str, ...]: + def argv(self, installed: Path | None = None, current_version: str = HOST_CURRENT_VERSION) -> tuple[str, ...]: return ( "--scripture-archive-apply", "--wait-pid", "41", + "--current-version", + current_version, "--installed-executable", str(self.installed if installed is None else installed), "--staging-root", @@ -64,9 +66,33 @@ def verifier(installed: Path, updater: Path) -> bool: self.assertEqual(len(execute_calls), 1) forwarded, kwargs = execute_calls[0] self.assertEqual(forwarded, argv) - self.assertEqual(kwargs["current_version"], CURRENT_APPLICATION_VERSION) + self.assertEqual(kwargs["current_version"], HOST_CURRENT_VERSION) self.assertIs(kwargs["verify_same_publisher"], verifier) + def test_entrypoint_forwards_newer_running_host_version_without_embedded_version_authority(self) -> None: + calls = [] + result = run_updater( + self.argv(current_version="0.7.0"), + execute=lambda argv, **kwargs: calls.append((tuple(argv), kwargs)), + verifier=lambda installed, updater: True, + updater_executable=self.updater, + ) + self.assertEqual(result, UPDATER_OK) + self.assertEqual("0.7.0", calls[0][1]["current_version"]) + + def test_invalid_handoff_version_fails_before_verifier_or_executor(self) -> None: + verified = [] + executed = [] + result = run_updater( + self.argv(current_version="0.7"), + execute=lambda *args, **kwargs: executed.append((args, kwargs)), + verifier=lambda *args: verified.append(args) or True, + updater_executable=self.updater, + ) + self.assertEqual(result, UPDATER_FAILED) + self.assertEqual([], verified) + self.assertEqual([], executed) + def test_existing_noncanonical_installed_target_never_reaches_verifier_or_executor(self) -> None: other = self.install / "Other.exe" other.write_bytes(b"other") diff --git a/lanes/dev1/repair_overlay/tests/test_packaged_updater_host_launch.py b/lanes/dev1/repair_overlay/tests/test_packaged_updater_host_launch.py index 4b6b34402..d3dc56f56 100644 --- a/lanes/dev1/repair_overlay/tests/test_packaged_updater_host_launch.py +++ b/lanes/dev1/repair_overlay/tests/test_packaged_updater_host_launch.py @@ -82,6 +82,7 @@ def build_plan(**kwargs): updater_executable=kwargs["updater_executable"].resolve(strict=True), installed_executable=kwargs["installed_executable"].resolve(strict=True), staging_root=kwargs["staging_root"].resolve(strict=True), + current_version=kwargs["current_version"], parent_pid=41, ) @@ -91,6 +92,7 @@ def launch(plan): pid = launch_packaged_updater( self.staging, + current_version=CURRENT_VERSION, current_executable=self.current, verify_same_publisher=verify, build_plan=build_plan, @@ -102,6 +104,7 @@ def launch(plan): self.assertEqual(self.updater, events[1][1]["updater_executable"]) self.assertEqual(self.current, events[1][1]["installed_executable"]) self.assertEqual(self.staging, events[1][1]["staging_root"]) + self.assertEqual(CURRENT_VERSION, events[1][1]["current_version"]) self.assertEqual(("verify", self.current.resolve(), self.updater.resolve()), events[2]) self.assertEqual("launch", events[3][0]) @@ -115,12 +118,37 @@ def substituted_plan(**kwargs): updater_executable=external, installed_executable=kwargs["installed_executable"].resolve(), staging_root=kwargs["staging_root"].resolve(), + current_version=kwargs["current_version"], + parent_pid=41, + ) + + with self.assertRaisesRegex(UpdateProcessError, "changed host-owned launch authority"): + launch_packaged_updater( + self.staging, + current_version=CURRENT_VERSION, + current_executable=self.current, + verify_same_publisher=lambda _current, _updater: True, + build_plan=substituted_plan, + launch_process=lambda plan: launched.append(plan) or 1, + ) + self.assertEqual([], launched) + + def test_process_plan_cannot_substitute_host_owned_current_version(self): + launched = [] + + def substituted_plan(**kwargs): + return UpdaterProcessPlan( + updater_executable=kwargs["updater_executable"].resolve(), + installed_executable=kwargs["installed_executable"].resolve(), + staging_root=kwargs["staging_root"].resolve(), + current_version="9.9.9", parent_pid=41, ) with self.assertRaisesRegex(UpdateProcessError, "changed host-owned launch authority"): launch_packaged_updater( self.staging, + current_version=CURRENT_VERSION, current_executable=self.current, verify_same_publisher=lambda _current, _updater: True, build_plan=substituted_plan, @@ -140,6 +168,7 @@ def verify(current, updater): with self.assertRaisesRegex(UpdateProcessError, "changed before launch"): launch_packaged_updater( self.staging, + current_version=CURRENT_VERSION, current_executable=self.current, verify_same_publisher=verify, launch_process=lambda plan: launched.append(plan) or 1, @@ -156,6 +185,7 @@ def test_negative_or_failed_publisher_proof_never_builds_process_plan(self): with self.subTest(verifier=verifier), self.assertRaises(UpdateProcessError): launch_packaged_updater( self.staging, + current_version=CURRENT_VERSION, current_executable=self.current, verify_same_publisher=verifier, build_plan=lambda **kwargs: built.append(kwargs), @@ -251,7 +281,7 @@ def test_browser_payload_cannot_supply_process_or_path_authority(self): response = layer.handle( request( APPLY_AND_RESTART_COMMAND, - {"pid": 1, "updater": r"C:\attacker.exe"}, + {"pid": 1, "updater": r"C:\attacker.exe", "current_version": "0.0.0"}, ) ) self.assertFalse(response["ok"]) diff --git a/runtime_engine/scripture_archive_runtime/application_update_process.py b/runtime_engine/scripture_archive_runtime/application_update_process.py index 6fd50fdc9..75279373e 100644 --- a/runtime_engine/scripture_archive_runtime/application_update_process.py +++ b/runtime_engine/scripture_archive_runtime/application_update_process.py @@ -6,6 +6,8 @@ import subprocess from typing import Callable, Sequence +from .application_update import ApplicationUpdateError, SemVer + class UpdateProcessError(RuntimeError): """Fail-closed updater process orchestration error.""" @@ -24,6 +26,7 @@ class UpdaterProcessPlan: updater_executable: Path installed_executable: Path staging_root: Path + current_version: str parent_pid: int def argv(self) -> tuple[str, ...]: @@ -32,6 +35,8 @@ def argv(self) -> tuple[str, ...]: "--scripture-archive-apply", "--wait-pid", str(self.parent_pid), + "--current-version", + self.current_version, "--installed-executable", str(self.installed_executable), "--staging-root", @@ -76,6 +81,7 @@ def build_updater_process_plan( updater_executable: Path, installed_executable: Path, staging_root: Path, + current_version: str, parent_pid: int | None = None, ) -> UpdaterProcessPlan: """Build a fixed host-side process handoff. @@ -91,6 +97,10 @@ def build_updater_process_plan( updater = _require_plain_file(Path(updater_executable), label="updater executable") installed = _require_plain_file(Path(installed_executable), label="installed executable") staging = _require_plain_directory(Path(staging_root), label="staging root") + try: + SemVer.parse(current_version) + except ApplicationUpdateError as exc: + raise UpdateProcessError("current version must be valid semantic version") from exc if updater.parent != installed.parent: raise UpdateProcessError("updater executable must be packaged beside the installed application") @@ -110,6 +120,7 @@ def build_updater_process_plan( updater_executable=updater, installed_executable=installed, staging_root=staging, + current_version=current_version, parent_pid=effective_parent_pid, ) @@ -155,23 +166,38 @@ def launch_updater_process( return pid -def parse_trusted_updater_argv(argv: Sequence[str]) -> tuple[int, Path, Path]: - """Parse only the fixed updater command generated by ``UpdaterProcessPlan``. +def parse_trusted_updater_argv(argv: Sequence[str]) -> tuple[int, Path, Path, str]: + """Parse only the fixed updater command generated by UpdaterProcessPlan. - This is intentionally not a general CLI parser. Exact positional shape, - marker and flag order are part of the host/updater contract; extra flags or - duplicated options fail closed. The updater must subsequently apply the - same filesystem validation and durable-authority verification as its core. + Exact positional shape, marker and flag order are part of the host/updater + contract. The current version is supplied only by the running packaged host; + extra flags, duplicate options, and malformed semantic versions fail closed. """ values = tuple(argv) - if len(values) != 7: + if len(values) != 9: raise UpdateProcessError("invalid updater command shape") - marker, wait_flag, wait_value, installed_flag, installed_value, staging_flag, staging_value = values + ( + marker, + wait_flag, + wait_value, + version_flag, + current_version, + installed_flag, + installed_value, + staging_flag, + staging_value, + ) = values if marker != "--scripture-archive-apply": raise UpdateProcessError("invalid updater command marker") if wait_flag != "--wait-pid": raise UpdateProcessError("invalid updater wait flag") + if version_flag != "--current-version": + raise UpdateProcessError("invalid updater current-version flag") + try: + SemVer.parse(current_version) + except ApplicationUpdateError as exc: + raise UpdateProcessError("invalid updater current version") from exc if installed_flag != "--installed-executable": raise UpdateProcessError("invalid installed executable flag") if staging_flag != "--staging-root": @@ -186,4 +212,4 @@ def parse_trusted_updater_argv(argv: Sequence[str]) -> tuple[int, Path, Path]: raise UpdateProcessError("invalid installed executable path") if not staging_value or "\x00" in staging_value: raise UpdateProcessError("invalid staging root path") - return parent_pid, Path(installed_value), Path(staging_value) + return parent_pid, Path(installed_value), Path(staging_value), current_version diff --git a/runtime_engine/scripture_archive_runtime/application_update_runner.py b/runtime_engine/scripture_archive_runtime/application_update_runner.py index b923237eb..787d87d08 100644 --- a/runtime_engine/scripture_archive_runtime/application_update_runner.py +++ b/runtime_engine/scripture_archive_runtime/application_update_runner.py @@ -98,7 +98,9 @@ def execute_trusted_updater( if not callable(dependency): raise ApplicationUpdateError(f"{label} is required") - parent_pid, installed_executable, staging_root = parse_trusted_updater_argv(argv) + parent_pid, installed_executable, staging_root, handoff_version = parse_trusted_updater_argv(argv) + if handoff_version != current_version: + raise ApplicationUpdateError("trusted updater current version does not match host handoff") wait_for_exit(parent_pid) result = consume( diff --git a/runtime_engine/tests/test_application_update_process.py b/runtime_engine/tests/test_application_update_process.py index ddb75ba75..cc95ad137 100644 --- a/runtime_engine/tests/test_application_update_process.py +++ b/runtime_engine/tests/test_application_update_process.py @@ -5,6 +5,8 @@ import tempfile import unittest +CURRENT_VERSION = "1.0.0" + from scripture_archive_runtime.application_update_process import ( UpdateProcessError, build_updater_process_plan, @@ -39,6 +41,7 @@ def test_build_plan_binds_sibling_packaged_updater_current_pid_and_fixed_argv(se updater_executable=updater, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) self.assertEqual(plan.parent_pid, os.getpid()) self.assertEqual(plan.updater_executable, updater.resolve()) @@ -51,6 +54,8 @@ def test_build_plan_binds_sibling_packaged_updater_current_pid_and_fixed_argv(se "--scripture-archive-apply", "--wait-pid", str(os.getpid()), + "--current-version", + CURRENT_VERSION, "--installed-executable", str(app.resolve()), "--staging-root", @@ -67,6 +72,7 @@ def test_build_plan_rejects_external_updater(self) -> None: updater_executable=external, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) def test_build_plan_rejects_current_app_as_updater(self) -> None: @@ -76,6 +82,7 @@ def test_build_plan_rejects_current_app_as_updater(self) -> None: updater_executable=app, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) def test_build_plan_rejects_symlinked_updater_target_and_staging(self) -> None: @@ -92,12 +99,14 @@ def test_build_plan_rejects_symlinked_updater_target_and_staging(self) -> None: updater_executable=updater_link, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) with self.assertRaisesRegex(UpdateProcessError, "real directory"): build_updater_process_plan( updater_executable=updater, installed_executable=app, staging_root=staging_link, + current_version=CURRENT_VERSION, ) def test_build_plan_rejects_foreign_parent_pid(self) -> None: @@ -107,6 +116,7 @@ def test_build_plan_rejects_foreign_parent_pid(self) -> None: updater_executable=updater, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, parent_pid=os.getpid() + 1, ) @@ -116,6 +126,7 @@ def test_launch_is_shell_free_fixed_cwd_and_returns_child_pid(self) -> None: updater_executable=updater, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) seen: dict[str, object] = {} @@ -141,6 +152,7 @@ def test_launch_wraps_process_failure(self) -> None: updater_executable=updater, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) def broken_popen(*args, **kwargs): @@ -155,22 +167,26 @@ def test_parser_accepts_only_fixed_host_contract(self) -> None: updater_executable=updater, installed_executable=app, staging_root=staging, + current_version=CURRENT_VERSION, ) - pid, parsed_app, parsed_staging = parse_trusted_updater_argv(plan.argv()[1:]) + pid, parsed_app, parsed_staging, parsed_version = parse_trusted_updater_argv(plan.argv()[1:]) self.assertEqual(pid, os.getpid()) self.assertEqual(parsed_app, app.resolve()) self.assertEqual(parsed_staging, staging.resolve()) + self.assertEqual(parsed_version, CURRENT_VERSION) def test_parser_rejects_noncanonical_or_extended_commands(self) -> None: invalid_commands = [ (), ("--scripture-archive-apply",), - ("--wrong", "--wait-pid", "1", "--installed-executable", "a", "--staging-root", "b"), - ("--scripture-archive-apply", "--wait-pid", "01", "--installed-executable", "a", "--staging-root", "b"), + ("--wrong", "--wait-pid", "1", "--current-version", CURRENT_VERSION, "--installed-executable", "a", "--staging-root", "b"), + ("--scripture-archive-apply", "--wait-pid", "01", "--current-version", CURRENT_VERSION, "--installed-executable", "a", "--staging-root", "b"), ( "--scripture-archive-apply", "--wait-pid", "1", + "--current-version", + CURRENT_VERSION, "--installed-executable", "a", "--staging-root", diff --git a/runtime_engine/tests/test_application_update_runner.py b/runtime_engine/tests/test_application_update_runner.py index 87a7551eb..25544a08f 100644 --- a/runtime_engine/tests/test_application_update_runner.py +++ b/runtime_engine/tests/test_application_update_runner.py @@ -28,6 +28,7 @@ def _plan(tmp_path: Path): updater_executable=updater, installed_executable=app, staging_root=staging, + current_version="1.0.0", ) @@ -288,6 +289,26 @@ def consume(*args, **kwargs): ) self.assertFalse(consumed) + def test_runner_rejects_version_mismatch_between_api_and_fixed_handoff(self) -> None: + plan = _plan(self.tmp_path) + waited = False + + def wait_for_exit(parent_pid: int) -> None: + nonlocal waited + waited = True + + with self.assertRaisesRegex( + ApplicationUpdateError, + "does not match host handoff", + ): + execute_trusted_updater( + plan.argv()[1:], + current_version="1.0.1", + verify_same_publisher=lambda installed, staged: True, + wait_for_exit=wait_for_exit, + ) + self.assertFalse(waited) + def test_runner_rejects_malformed_command_before_wait_or_consume(self) -> None: waited = False consumed = False