Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## Unreleased

### 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
Expand Down
35 changes: 34 additions & 1 deletion src/t2-fprint-enrollment-worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from __future__ import annotations

import argparse
import json
import os
import sys
from pathlib import Path
Expand All @@ -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 "<empty>")
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",
Expand Down Expand Up @@ -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
Expand Down
113 changes: 113 additions & 0 deletions tests/test_fprint_enrollment_worker_report.py
Original file line number Diff line number Diff line change
@@ -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], "<empty>")

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()