diff --git a/CHANGELOG.md b/CHANGELOG.md index e8199cc..43db73e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,28 @@ follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). ## Unreleased +### Fixed + +- Let `recover-outcome` close a first enrollment whose master Catacomb save + stopped at its prepare request after a cold bridgeOS restart removed the + never-committed identity. The E3 classifier now admits a bootstrap baseline + that returned to the absent-Catacomb surface, the journal forgets a witnessed + identity only when its persistence failure left nothing on the host, and the + staged `user_.cat` is discarded only once the live inventory already + shows the empty baseline. Previously the bootstrap Catacomb UUID rule + rejected that surface, so the journal stayed `outcome-unknown` and blocked + every further enrollment with no supported way out. + +### Changed + +- Report the messages of the project's own exception chain in the enrollment + worker's `enrollment stopped` line as a JSON `detail=` list, alongside the + existing class and reason fields. Only messages raised by `t2_*` modules are + included, so the line still carries no identifiers or raw bridge replies. + Without it, a Catacomb persistence failure logged only + `classes=...>CatacombBridgeError reason=unclassified`, which cannot be told + apart from the other bridge checks when reporting an issue. + ## 0.1.2 - 2026-10-28 On our test Mac, after T2Touch had run in Linux, macOS removed the account's diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index f2855ec..19edea9 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -368,6 +368,32 @@ The installed services own Bridge network preparation, discovery, authority activation, and reconciliation. Manual keybag-loading sequences from early research are not installation prerequisites. +## First enrollment saved the user Catacomb but not the master + +On a Linux-only install the first enrollment creates the whole Catacomb +generation. `t2-touchid-manage status` then shows `new_mutation_blocked: true` +with every listed category at zero, and the enrollment journal ends with +`CATACOMB_CONFIRMED` for `user_.cat` followed by +`CATACOMB_PERSISTENCE_OUTCOME_UNKNOWN` for `master.cat` at +`stage=prepare` with `host_commit_possible=False`. The worker's +`enrollment stopped` line carries the failing bridge check in `detail=`. +Locally, `/var/lib/t2-touchid/catacomb/prepare/` holds only the staged user +file and no `commit/` directory exists. + +The SEP still holds the new identity, but nothing was committed on the host. +While bridgeOS stays warm, that staged file is the only durable copy of the +fingerprint; do not remove it, and do not rerun enrollment. No command finishes +this save. Report the `detail=` text with the journal milestones. + +If you accept re-enrolling that finger, a cold bridgeOS restart returns the +SEP to the pre-enrollment surface (no identities, no Catacomb, master at state +1). After such a restart, `sudo t2-touchid-enroll recover-outcome` proves the +SEP and host both equal the baseline, discards the staged user file, and +closes the journal as a no-change recovery. It refuses while the SEP still +reports the identity or a Catacomb, so a warm restart changes nothing and the +staged file stays in place. Enrollment is unblocked once `status` reports the +mutation as complete. + ## Deletion failed and enrollment is now blocked A deletion error can mean SEP removed the fingerprint but local persistence did diff --git a/src/t2-fprint-enrollment-worker.py b/src/t2-fprint-enrollment-worker.py index 418bd21..cfa7fbe 100644 --- a/src/t2-fprint-enrollment-worker.py +++ b/src/t2-fprint-enrollment-worker.py @@ -5,6 +5,7 @@ from __future__ import annotations import argparse +import json import os import sys from pathlib import Path @@ -25,6 +26,37 @@ def _exception_classes(error: BaseException) -> str: return ">".join(names) +# Messages raised by the project's own exception classes are fixed strings +# with no identifiers, so they are safe to log. Anything else (OSError text, +# socket errors, third-party exceptions) is dropped. +_DETAIL_MODULE_PREFIX = "t2_" +_DETAIL_MAX_MESSAGE = 120 +_DETAIL_MAX_MESSAGES = 6 + + +def _exception_detail(error: BaseException) -> str: + messages: list[str] = [] + current: BaseException | None = error + seen: set[int] = set() + while ( + current is not None + and id(current) not in seen + and len(messages) < _DETAIL_MAX_MESSAGES + ): + seen.add(id(current)) + module = type(current).__module__ + if isinstance(module, str) and module.startswith(_DETAIL_MODULE_PREFIX): + text = "".join( + character + for character in str(current)[:_DETAIL_MAX_MESSAGE] + if character.isascii() and character.isprintable() + ) + messages.append(text or "") + cause = current.__cause__ + current = cause if isinstance(cause, BaseException) else None + return json.dumps(messages, separators=(",", ":")) + + def _authorization_reason(error: BaseException) -> str: allowed = { "authorization login session differs from the claim": "session-changed", @@ -63,7 +95,8 @@ def main() -> int: print( "t2-fprint-enrollment-worker: enrollment stopped " f"stage={stage} classes={_exception_classes(error)} " - f"reason={_authorization_reason(error)}", + f"reason={_authorization_reason(error)} " + f"detail={_exception_detail(error)}", file=sys.stderr, ) return 1 diff --git a/src/t2-native-enroll.py b/src/t2-native-enroll.py index e45a8ba..c8e37dd 100755 --- a/src/t2-native-enroll.py +++ b/src/t2-native-enroll.py @@ -1956,8 +1956,8 @@ def _run_outcome_unknown_reconciliation( live = t2_bridge_inventory.collect_stable_private_inventory( lease, selected.apple_uid ) - local_prepare_discarded = ( - _discard_cancelled_biolockout_prepare(store, history) + local_prepare_discarded = _discard_recoverable_prepare( + store, history, live ) host = t2_enrollment_finalizer.read_local_host_snapshot( store, history.baseline @@ -1989,18 +1989,29 @@ def _run_outcome_unknown_reconciliation( os.close(lock_descriptor) -def _discard_cancelled_biolockout_prepare( +def _discard_recoverable_prepare( store: t2_catacomb_store.CatacombStore, history: t2_enrollment_journal.EnrollmentHistory, + live: dict[str, object], ) -> bool: - """Remove the empty rollback side left by an ambiguous cancel save. - - Mesa cancellation is already terminal and identity-free at this point. - The BioLockout save request is a read/export operation, but a lost reply - can leave the host transaction directory open before any component bytes - exist. Never replay that request. Admit only the exact cancelled, - BioLockout-only, pre-host-stage shape and let the fresh stable inventory - plus normal E3 classifier prove that the identity set stayed unchanged. + """Remove a journal-bound rollback-side transaction before E3 read-back. + + Two exact shapes are admitted; anything else is left for the maintainer. + + Cancelled BioLockout save: Mesa cancellation is already terminal and + identity-free at this point. The BioLockout save request is a read/export + operation, but a lost reply can leave the host transaction directory open + before any component bytes exist. Never replay that request. Admit only + the cancelled, BioLockout-only, pre-host-stage shape and let the fresh + stable inventory plus normal E3 classifier prove that the identity set + stayed unchanged. + + Lost first identity: the user Catacomb was staged and confirmed, the + master save stopped at its prepare request with nothing committed on the + host, and a later cold bridgeOS restart removed the never-persisted + identity. The fresh inventory must already show the empty baseline + surface before the staged user file is discarded, because while the SEP + still holds the identity that file is the only durable copy of it. """ prepare = CATACOMB_ROOT / "prepare" commit = CATACOMB_ROOT / "commit" @@ -2009,6 +2020,29 @@ def _discard_cancelled_biolockout_prepare( if not prepare_pending and not commit_pending: return False persistence = history.persistence + user_name = f"user_{history.baseline['apple_uid']:08x}.cat" + staged = dict(persistence.staged_files) + lost_first_identity = ( + history.phase is t2_enrollment_journal.EnrollmentPhase.OUTCOME_UNKNOWN + and history.terminal_identity_uuid is not None + and persistence.phase + is t2_enrollment_persistence_journal.PersistencePhase.OUTCOME_UNKNOWN + and persistence.outcome_unknown_stage == "prepare" + and persistence.outcome_unknown_host_commit_possible is False + and persistence.batch_index == 0 + and persistence.component_index == 1 + and len(persistence.batches) >= 1 + and [name for name, _digest in persistence.batches[0]] + == [user_name, "master.cat"] + and set(staged) == {user_name} + and live.get("per_user_identity_records") == [] + and live.get("global_identity_records") == [] + and isinstance(live.get("catacomb"), dict) + and live["catacomb"].get("present") is False + ) + if lost_first_identity and prepare_pending and not commit_pending: + store.discard_prepare({user_name}, staged) + return True exact_cancel_save = ( history.phase is t2_enrollment_journal.EnrollmentPhase.OUTCOME_UNKNOWN and history.terminal_identity_uuid is None diff --git a/src/t2_enrollment_journal.py b/src/t2_enrollment_journal.py index e099268..c58bb43 100644 --- a/src/t2_enrollment_journal.py +++ b/src/t2_enrollment_journal.py @@ -748,6 +748,20 @@ def validate_history(records: list[dict[str, Any]]) -> EnrollmentHistory: raise EnrollmentJournalError( "outcome-unknown recovery observed a persistent identity delta" ) + elif terminal_identity_uuid is not None: + # E2 saw the identity, but the fresh-generation read-back now + # proves the SEP and host both equal the baseline: the identity + # was lost before any host commit (for example at a cold + # bridgeOS restart). Nothing remains to verify after reboot. + if ( + persistence.phase + is not persistence_journal.PersistencePhase.OUTCOME_UNKNOWN + or persistence._outcome_unknown_host_commit_possible is not False + ): + raise EnrollmentJournalError( + "witnessed identity vanished without an uncommitted persistence failure" + ) + terminal_identity_uuid = None reconciled_snapshot_sha256 = evidence["snapshot_sha256"] phase = EnrollmentPhase.RECONCILED continue diff --git a/src/t2_enrollment_reconciliation.py b/src/t2_enrollment_reconciliation.py index cfd1431..fab65b0 100644 --- a/src/t2_enrollment_reconciliation.py +++ b/src/t2_enrollment_reconciliation.py @@ -547,7 +547,20 @@ def classify( raise EnrollmentReconciliationError("host component metadata changed") if bootstrap: baseline_catacomb_uuid = baseline["sep_catacomb"]["uuid"] - if uuid.UUID(baseline_catacomb_uuid).int == 0: + # A first enrollment that never committed a host component can be + # followed by a cold bridgeOS restart, after which the SEP again + # reports no Catacomb at all. That is the baseline surface, not a new + # generation, so it is admitted only when nothing else changed either. + returned_to_absent_baseline = ( + not catacomb_present + and not live_after + and not added + and baseline["sep_catacomb"]["present"] is False + and catacomb["uuid"] == baseline_catacomb_uuid + ) + if returned_to_absent_baseline: + invalid_bootstrap_uuid = False + elif uuid.UUID(baseline_catacomb_uuid).int == 0: invalid_bootstrap_uuid = ( uuid.UUID(catacomb["uuid"]).int == 0 or catacomb["uuid"] == baseline_catacomb_uuid diff --git a/tests/test_enrollment_reconciliation.py b/tests/test_enrollment_reconciliation.py index ec35de2..2d15035 100644 --- a/tests/test_enrollment_reconciliation.py +++ b/tests/test_enrollment_reconciliation.py @@ -962,5 +962,303 @@ def test_identity_event_without_changed_persistence_is_rejected(self): ) + def create_lost_first_identity(self, directory: str) -> tuple[Path, str, dict]: + """A bootstrap enrollment whose master save stopped at its prepare request. + + This is the journal shape observed on MacBookPro16,3 / bridgeOS 24P6005: + the user Catacomb was confirmed and staged, then the master prepare + reply failed validation before any host commit. + """ + value = baseline() + value.update( + { + "baseline_version": 2, + "identity_records": [], + "capacity": {"used": 0, "maximum": 5}, + "sep_catacomb": { + "present": False, + "uuid": str(uuid.UUID(int=0)), + "hash": None, + }, + "host_components": [], + "master_enrollment_count": 0, + "backup_references": [], + } + ) + generation = value["connection_generation"] + path = Path(directory) / "operation.jsonl" + operation_id, _record = mutation_journal.create(path, "enroll", value) + enrollment_journal.append_checked( + path, + operation_id, + "ENROLL_START_INTENT", + { + "apple_uid": 501, + "protocol_version": 2, + "connection_generation": generation, + "request_length": 68, + "request_sha256": "a" * 64, + }, + ) + enrollment_journal.append_checked( + path, operation_id, "ENROLL_START_OBSERVED", {"status": 0} + ) + enrollment_journal.append_checked( + path, + operation_id, + "E2_TERMINAL_RESULT_WITNESSED", + { + "connection_generation": generation, + "event_sequence": 1, + "envelope_type": enrollment_journal.SERVICE_ENROLLMENT_RESULT, + "event_version": 2, + "payload_length": 40, + "event_sha256": "b" * 64, + "embedded_user_matches": True, + }, + ) + enrollment_journal.append_checked( + path, + operation_id, + "E2_WITNESS_IDENTITY_READBACK_OBSERVED", + { + "connection_generation": generation, + "user_id": 501, + "identity_uuid": str(uuid.UUID(int=8)), + "source": "stable-readback", + "single_identity_added": True, + "host_unchanged": True, + "sep_catacomb_advanced": True, + "per_user_global_equal": True, + "mapping_generation": value["mapping_generation"], + }, + ) + enrollment_journal.append_checked( + path, + operation_id, + "CATACOMB_PERSISTENCE_PLAN", + { + "connection_generation": generation, + "batches": [ + [ + {"name": "user_000001f5.cat", "descriptor_sha256": "1" * 64}, + {"name": "master.cat", "descriptor_sha256": "2" * 64}, + ], + [{"name": "biolockout.cat", "descriptor_sha256": "3" * 64}], + ], + }, + ) + user = _component_ref(0, 0, "user_000001f5.cat", "1" * 64) + enrollment_journal.append_checked( + path, operation_id, "CATACOMB_PREPARE_INTENT", user + ) + enrollment_journal.append_checked( + path, + operation_id, + "CATACOMB_PREPARED", + {**user, "status": 0, "expected_blob_length": 15902}, + ) + enrollment_journal.append_checked( + path, operation_id, "CATACOMB_COMPLETE_INTENT", user + ) + enrollment_journal.append_checked( + path, + operation_id, + "CATACOMB_SECURE_BLOB_CAPTURED", + { + **user, + "status": 0, + "blob_length": 15902, + "secure_blob_sha256": "4" * 64, + }, + ) + enrollment_journal.append_checked( + path, + operation_id, + "CATACOMB_HOST_STAGED", + {**user, "secure_blob_sha256": "4" * 64, "final_file_sha256": "7" * 64}, + ) + enrollment_journal.append_checked( + path, operation_id, "CATACOMB_CONFIRM_INTENT", user + ) + enrollment_journal.append_checked( + path, operation_id, "CATACOMB_CONFIRMED", {**user, "status": 0} + ) + master = _component_ref(0, 1, "master.cat", "2" * 64) + enrollment_journal.append_checked( + path, operation_id, "CATACOMB_PREPARE_INTENT", master + ) + enrollment_journal.append_checked( + path, + operation_id, + "CATACOMB_PERSISTENCE_OUTCOME_UNKNOWN", + { + **master, + "stage": "prepare", + "reason": "transport-error", + "sep_mutation_possible": True, + "host_commit_possible": False, + }, + ) + return path, operation_id, value + + def cold_bootstrap_snapshots(self, value: dict) -> tuple[dict, dict]: + """Empty host store and the SEP surface after a cold bridgeOS restart.""" + host = { + "account_uuid": value["account_uuid"], + "bag_uuid": value["bag_uuid"], + "identity_records": [], + "host_components": [], + "master_enrollment_count": 0, + } + live = { + "double_collection_equal": True, + "connection_generation": str(uuid.UUID(int=99)), + "apple_uid": 501, + "biometric_protocol_version": 2, + "per_user_identity_records": [], + "global_identity_records": [], + "maximum_capacity": 5, + "configured_user_free_capacity": 5, + "catacomb": { + "present": False, + "uuid": str(uuid.UUID(int=0)), + "hash": "0" * 64, + }, + } + return host, live + + def test_lost_first_identity_reconciles_as_no_change_after_cold_restart(self): + with tempfile.TemporaryDirectory() as directory: + path, operation_id, value = self.create_lost_first_identity(directory) + before = enrollment_journal.read(path) + self.assertIs(before.phase, enrollment_journal.EnrollmentPhase.OUTCOME_UNKNOWN) + self.assertIsNotNone(before.terminal_identity_uuid) + host, live = self.cold_bootstrap_snapshots(value) + result = reconciliation.append_reconciled( + path, + operation_id, + host=host, + live=live, + mapping_generation=value["mapping_generation"], + ) + records = mutation_journal.read(path) + self.assertEqual(result.phase, enrollment_journal.EnrollmentPhase.RECONCILED) + self.assertIsNone(result.terminal_identity_uuid) + self.assertEqual( + records[-1]["milestone"], "E3_RECOVERY_NO_CHANGE_RECONCILED" + ) + self.assertFalse(records[-1]["evidence"]["identity_present"]) + + def test_lost_first_identity_is_not_reconciled_while_sep_differs(self): + with tempfile.TemporaryDirectory() as directory: + path, operation_id, value = self.create_lost_first_identity(directory) + host, live = self.cold_bootstrap_snapshots(value) + # Warm bridgeOS: the identity and its Catacomb are still there. + live["per_user_identity_records"] = [ + {"user_id": 501, "identity_uuid": str(uuid.UUID(int=8))} + ] + live["global_identity_records"] = [ + { + "user_id": 501, + "identity_uuid": str(uuid.UUID(int=8)), + "group_type": 1, + "group_uuid": str(uuid.UUID(int=0)), + } + ] + live["configured_user_free_capacity"] = 4 + live["catacomb"] = { + "present": True, + "uuid": str(uuid.UUID(int=9)), + "hash": "3" * 64, + } + with self.assertRaisesRegex( + reconciliation.EnrollmentReconciliationError, "inventories diverge" + ): + reconciliation.append_reconciled( + path, + operation_id, + host=host, + live=live, + mapping_generation=value["mapping_generation"], + ) + # Absent Catacomb but a surviving identity is not the baseline. + host, live = self.cold_bootstrap_snapshots(value) + live["per_user_identity_records"] = [ + {"user_id": 501, "identity_uuid": str(uuid.UUID(int=8))} + ] + live["global_identity_records"] = [ + { + "user_id": 501, + "identity_uuid": str(uuid.UUID(int=8)), + "group_type": 1, + "group_uuid": str(uuid.UUID(int=0)), + } + ] + with self.assertRaisesRegex( + reconciliation.EnrollmentReconciliationError, "inventories diverge" + ): + reconciliation.append_reconciled( + path, + operation_id, + host=host, + live=live, + mapping_generation=value["mapping_generation"], + ) + # A zero Catacomb UUID that claims presence is still rejected. + host, live = self.cold_bootstrap_snapshots(value) + live["catacomb"]["present"] = True + with self.assertRaisesRegex( + reconciliation.EnrollmentReconciliationError, "Catacomb UUID is invalid" + ): + reconciliation.append_reconciled( + path, + operation_id, + host=host, + live=live, + mapping_generation=value["mapping_generation"], + ) + self.assertIs( + enrollment_journal.read(path).phase, + enrollment_journal.EnrollmentPhase.OUTCOME_UNKNOWN, + ) + + def test_no_change_recovery_keeps_witnessed_identity_after_possible_commit(self): + """A vanished identity is only forgotten when nothing could have been committed.""" + with tempfile.TemporaryDirectory() as directory: + path, operation_id, value = self.create_lost_first_identity(directory) + records = mutation_journal.read(path) + # Rewrite the failure as a host-commit ambiguity of the same component. + failure = dict(records[-1]["evidence"]) + failure.update({"stage": "host-commit", "host_commit_possible": True}) + prefix = records[:-1] + rewritten = Path(directory) / "rewritten.jsonl" + rewritten_id, _record = mutation_journal.create(rewritten, "enroll", value) + # Replay the recorded chain onto the fresh file to keep the hash chain valid. + for record in prefix[1:]: + enrollment_journal.append_checked( + rewritten, rewritten_id, record["milestone"], record["evidence"] + ) + enrollment_journal.append_checked( + rewritten, rewritten_id, "CATACOMB_PERSISTENCE_OUTCOME_UNKNOWN", failure + ) + host, live = self.cold_bootstrap_snapshots(value) + with self.assertRaisesRegex( + enrollment_journal.EnrollmentJournalError, + "vanished without an uncommitted persistence failure", + ): + reconciliation.append_reconciled( + rewritten, + rewritten_id, + host=host, + live=live, + mapping_generation=value["mapping_generation"], + ) + self.assertIs( + enrollment_journal.read(rewritten).phase, + enrollment_journal.EnrollmentPhase.OUTCOME_UNKNOWN, + ) + + if __name__ == "__main__": unittest.main() diff --git a/tests/test_fprint_enrollment_worker_report.py b/tests/test_fprint_enrollment_worker_report.py new file mode 100644 index 0000000..17b45df --- /dev/null +++ b/tests/test_fprint_enrollment_worker_report.py @@ -0,0 +1,113 @@ +# SPDX-License-Identifier: GPL-2.0-only +import importlib.util +import json +import sys +import unittest +from pathlib import Path + + +SOURCE = Path(__file__).parents[1] / "src" +sys.path.insert(0, str(SOURCE)) +SPEC = importlib.util.spec_from_file_location( + "t2_fprint_enrollment_worker", SOURCE / "t2-fprint-enrollment-worker.py" +) +MODULE = importlib.util.module_from_spec(SPEC) +assert SPEC.loader +SPEC.loader.exec_module(MODULE) + +import t2_catacomb_bridge as catacomb_bridge +import t2_catacomb_protocol as catacomb_protocol +import t2_enrollment_persistence_operation as persistence_operation +import t2_fprint_worker as fprint_worker + + +def _chain(*errors: BaseException) -> BaseException: + """Link errors so that errors[0].__cause__ is errors[1], and so on.""" + for outer, inner in zip(errors, errors[1:]): + outer.__cause__ = inner + return errors[0] + + +class EnrollmentWorkerReportTests(unittest.TestCase): + def test_detail_reports_project_messages_along_the_cause_chain(self): + error = _chain( + fprint_worker.FprintWorkerError( + "enrollment stopped", stage="recover-terminal-identity" + ), + persistence_operation.PersistenceOperationError( + "Catacomb persistence stopped at prepare; reconciliation is required" + ), + catacomb_bridge.CatacombBridgeError( + "Catacomb command emitted an unexpected service event" + ), + ) + self.assertEqual( + MODULE._exception_classes(error), + "FprintWorkerError>PersistenceOperationError>CatacombBridgeError", + ) + self.assertEqual( + json.loads(MODULE._exception_detail(error)), + [ + "enrollment stopped", + "Catacomb persistence stopped at prepare; reconciliation is required", + "Catacomb command emitted an unexpected service event", + ], + ) + + def test_detail_includes_protocol_errors_and_drops_foreign_messages(self): + error = _chain( + fprint_worker.FprintWorkerError("enrollment stopped"), + catacomb_bridge.CatacombBridgeError("Catacomb prepare reply is invalid"), + catacomb_protocol.CatacombProtocolError( + "Catacomb prepare returned nonzero status" + ), + OSError(111, "Connection refused: /run/t2-touchid/private/socket"), + ValueError("expected message frame, received type 7"), + ) + self.assertEqual( + json.loads(MODULE._exception_detail(error)), + [ + "enrollment stopped", + "Catacomb prepare reply is invalid", + "Catacomb prepare returned nonzero status", + ], + ) + + def test_detail_sanitizes_and_bounds_messages(self): + long_message = "x" * 500 + error = _chain( + fprint_worker.FprintWorkerError("tab\there\nnewline é end"), + catacomb_bridge.CatacombBridgeError(long_message), + catacomb_bridge.CatacombBridgeError(""), + ) + detail = json.loads(MODULE._exception_detail(error)) + self.assertEqual(detail[0], "tabherenewline end") + self.assertEqual(detail[1], "x" * MODULE._DETAIL_MAX_MESSAGE) + self.assertEqual(detail[2], "") + + def test_detail_stops_at_the_message_limit_and_on_cycles(self): + errors = [ + catacomb_bridge.CatacombBridgeError(f"message {index}") + for index in range(MODULE._DETAIL_MAX_MESSAGES + 3) + ] + _chain(*errors) + self.assertEqual( + len(json.loads(MODULE._exception_detail(errors[0]))), + MODULE._DETAIL_MAX_MESSAGES, + ) + first = catacomb_bridge.CatacombBridgeError("first") + second = catacomb_bridge.CatacombBridgeError("second") + first.__cause__ = second + second.__cause__ = first + self.assertEqual( + json.loads(MODULE._exception_detail(first)), ["first", "second"] + ) + + def test_detail_without_project_errors_is_an_empty_list(self): + self.assertEqual( + MODULE._exception_detail(RuntimeError("/etc/t2-touchid.conf")), "[]" + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_native_enrollment_authority.py b/tests/test_native_enrollment_authority.py index 50a44aa..3d8d9fc 100644 --- a/tests/test_native_enrollment_authority.py +++ b/tests/test_native_enrollment_authority.py @@ -135,7 +135,7 @@ def test_active_outcome_unknown_reconciles_no_change_without_replay(self) -> Non ), mock.patch.object( native_enroll, - "_discard_cancelled_biolockout_prepare", + "_discard_recoverable_prepare", return_value=False, ), mock.patch.object( @@ -188,6 +188,7 @@ def test_cancel_save_ambiguity_discards_only_empty_prepare_before_readback( terminal_identity_uuid=None, terminal_status=66, persistence=persistence, + baseline={"apple_uid": 501}, ) store = SimpleNamespace(discard_empty_prepare=mock.Mock()) with mock.patch.object( @@ -195,13 +196,161 @@ def test_cancel_save_ambiguity_discards_only_empty_prepare_before_readback( "lexists", side_effect=lambda path: Path(path).name == "prepare", ): - discarded = native_enroll._discard_cancelled_biolockout_prepare( - store, history + discarded = native_enroll._discard_recoverable_prepare( + store, history, {} ) self.assertTrue(discarded) store.discard_empty_prepare.assert_called_once_with() + def _lost_first_identity_history(self, **overrides: object) -> SimpleNamespace: + """A first enrollment whose master save stopped at its prepare request.""" + + persistence = SimpleNamespace( + phase=( + native_enroll.t2_enrollment_persistence_journal.PersistencePhase.OUTCOME_UNKNOWN + ), + outcome_unknown_stage="prepare", + outcome_unknown_host_commit_possible=False, + batch_index=0, + component_index=1, + staged_files=(("user_000001f5.cat", "7" * 64),), + batches=( + (("user_000001f5.cat", "1" * 64), ("master.cat", "2" * 64)), + (("biolockout.cat", "3" * 64),), + ), + ) + for key, value in overrides.items(): + setattr(persistence, key, value) + return SimpleNamespace( + phase=native_enroll.t2_enrollment_journal.EnrollmentPhase.OUTCOME_UNKNOWN, + terminal_identity_uuid=identifier(8), + terminal_status=None, + persistence=persistence, + baseline={"apple_uid": 501}, + ) + + @staticmethod + def _cold_live() -> dict[str, object]: + return { + "per_user_identity_records": [], + "global_identity_records": [], + "catacomb": {"present": False, "uuid": identifier(0), "hash": "0" * 64}, + } + + def test_lost_first_identity_discards_staged_user_only_on_empty_surface( + self, + ) -> None: + """The staged user file goes only after the SEP shows the baseline again.""" + + history = self._lost_first_identity_history() + store = SimpleNamespace( + discard_prepare=mock.Mock(), discard_empty_prepare=mock.Mock() + ) + with mock.patch.object( + native_enroll.os.path, + "lexists", + side_effect=lambda path: Path(path).name == "prepare", + ): + discarded = native_enroll._discard_recoverable_prepare( + store, history, self._cold_live() + ) + + self.assertTrue(discarded) + store.discard_prepare.assert_called_once_with( + {"user_000001f5.cat"}, {"user_000001f5.cat": "7" * 64} + ) + store.discard_empty_prepare.assert_not_called() + + def test_lost_first_identity_keeps_staged_user_while_sep_holds_identity( + self, + ) -> None: + """A warm bridgeOS still holds the identity; the staged file is its only copy.""" + + history = self._lost_first_identity_history() + store = SimpleNamespace( + discard_prepare=mock.Mock(), discard_empty_prepare=mock.Mock() + ) + warm_variants = [ + { + **self._cold_live(), + "per_user_identity_records": [ + {"user_id": 501, "identity_uuid": identifier(8)} + ], + }, + { + **self._cold_live(), + "catacomb": {"present": True, "uuid": identifier(9), "hash": "3" * 64}, + }, + {}, + ] + for live in warm_variants: + with ( + self.subTest(live=live), + mock.patch.object( + native_enroll.os.path, + "lexists", + side_effect=lambda path: Path(path).name == "prepare", + ), + self.assertRaisesRegex( + native_enroll.NativeEnrollmentError, + "unsupported local Catacomb transaction", + ), + ): + native_enroll._discard_recoverable_prepare(store, history, live) + store.discard_prepare.assert_not_called() + store.discard_empty_prepare.assert_not_called() + + def test_lost_first_identity_requires_the_exact_persistence_shape(self) -> None: + """Any other stage, component, or staged set is left for manual review.""" + + store = SimpleNamespace( + discard_prepare=mock.Mock(), discard_empty_prepare=mock.Mock() + ) + variants = { + "complete stage": {"outcome_unknown_stage": "complete"}, + "host commit possible": {"outcome_unknown_host_commit_possible": True}, + "user component": {"component_index": 0}, + "nothing staged": {"staged_files": ()}, + "master staged too": { + "staged_files": ( + ("user_000001f5.cat", "7" * 64), + ("master.cat", "8" * 64), + ) + }, + "other user staged": {"staged_files": (("user_000001f6.cat", "7" * 64),)}, + } + for label, overrides in variants.items(): + history = self._lost_first_identity_history(**overrides) + with ( + self.subTest(label), + mock.patch.object( + native_enroll.os.path, + "lexists", + side_effect=lambda path: Path(path).name == "prepare", + ), + self.assertRaisesRegex( + native_enroll.NativeEnrollmentError, + "unsupported local Catacomb transaction", + ), + ): + native_enroll._discard_recoverable_prepare( + store, history, self._cold_live() + ) + history = self._lost_first_identity_history() + with ( + mock.patch.object(native_enroll.os.path, "lexists", return_value=True), + self.assertRaisesRegex( + native_enroll.NativeEnrollmentError, + "unsupported local Catacomb transaction", + ), + ): + native_enroll._discard_recoverable_prepare( + store, history, self._cold_live() + ) + store.discard_prepare.assert_not_called() + store.discard_empty_prepare.assert_not_called() + def test_exact_interrupted_authority_temp_is_resumed(self) -> None: """Only byte-identical publication state may cross the atomic rename."""