From 749516020e09f02a42068a64fb3765d5356867b2 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:59:17 +0000 Subject: [PATCH 01/15] TD-5420: a computer with no secret service has no keychain The 0.5.14 packaged check upgraded a 0.5.13 data folder on a session bus with no keyring: secret-tool exited at once with "The name org.freedesktop.secrets was not provided by any .service files", which _classify_cli_failure kept as a plain KeychainError, an unknown answer. By design an unknown answer begins no chain over existing rows, so the upgraded log stayed unchained: Doctor's audit row failed and the export refused, for good, on every Linux computer without a secret service. That stderr, D-Bus ServiceUnknown, and "Cannot autolaunch D-Bus" (no session bus) now classify as KeychainUnavailable: no keychain here, so the data folder decides, as when secret-tool is not installed. A keyring that is there but locked (a hung lookup, a dismissed prompt) is still locked; the confirm-missing search path is unchanged. Tests: the three messages classify as no keychain; a secret-tool lookup with that stderr raises KeychainUnavailable and the audit key's answer is "unavailable". Five tests, all failing on main. Co-Authored-By: Claude Opus 5.5 --- DECISIONS.md | 11 +++++++++++ core/tests/test_keychain.py | 17 ++++++++++++++++ core/tests/test_keychain_not_found.py | 28 +++++++++++++++++++++++++++ core/tstd/keychain.py | 25 ++++++++++++++++++++++-- docs/configuration.md | 5 ++++- 5 files changed, 83 insertions(+), 3 deletions(-) diff --git a/DECISIONS.md b/DECISIONS.md index 8832e864..d39e306b 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -21666,6 +21666,17 @@ row), `keychain_secret_tool.py` (Linux reads). when it cannot be used, the file). The begin's reads are lenient, as verify's are, so stored text that is not UTF-8 fails its hash rather than raising and losing every write. +- **No secret service is no keychain** (the 0.5.14 packaged check). On a Linux + computer with `secret-tool` but no secret service to start on the session bus + ("was not provided by any .service files", `ServiceUnknown`) or no session bus + ("Cannot autolaunch D-Bus"), the CLI's error was read as an unknown answer, so + an upgraded log was never chained over its old rows and Doctor and the export + failed for good. `keychain._classify_cli_failure` now reads those as + `KeychainUnavailable`: there is no keychain to ask, so the data folder decides, + as when `secret-tool` is not installed. A keyring that is installed but locked + (a hung lookup, a dismissed unlock prompt) is still locked. This gives a + same-user attacker nothing new: making the keychain unreachable for TST Desk + already needs control of its environment, which also reaches `audit.key`. - **Genesis without UPDATE, and never twice over rows.** The first time the chain is used, one `chain_genesis` row per table is inserted, in its own transaction committed before any chained insert, holding a digest over diff --git a/core/tests/test_keychain.py b/core/tests/test_keychain.py index f029c996..6f731147 100644 --- a/core/tests/test_keychain.py +++ b/core/tests/test_keychain.py @@ -448,3 +448,20 @@ async def communicate(self, input: bytes | None = None) -> tuple[bytes, bytes]: async def wait(self) -> int: return 0 + + +@pytest.mark.parametrize( + "stderr", + [ + "secret-tool: The name org.freedesktop.secrets was not provided by any .service files", + "Error: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: no such name", + "secret-tool: Cannot autolaunch D-Bus without X11 $DISPLAY", + ], +) +def test_no_secret_service_classifies_as_no_keychain(stderr: str) -> None: + """A computer with no secret service has no keychain; it is not a locked one.""" + from tstd.keychain import KeychainUnavailable + + err = _classify_cli_failure(stderr, "Failed to read keychain") + assert isinstance(err, KeychainUnavailable) + assert "No keychain on this computer" in str(err) diff --git a/core/tests/test_keychain_not_found.py b/core/tests/test_keychain_not_found.py index 50d7a12a..51722ba6 100644 --- a/core/tests/test_keychain_not_found.py +++ b/core/tests/test_keychain_not_found.py @@ -195,3 +195,31 @@ async def test_audit_values_only_reach_audit_accounts() -> None: await kc_mod.get_audit_secret("tst-openrouter") with pytest.raises(KeychainError, match="not an audit keychain account"): await kc_mod.store_audit_secret("tst-openrouter", "ab" * 32) + + +@pytest.mark.parametrize( + "stderr", + [ + b"secret-tool: The name org.freedesktop.secrets was not provided by any .service files", + b"secret-tool: Cannot autolaunch D-Bus without X11 $DISPLAY", + ], +) +async def test_linux_no_secret_service_is_no_keychain( + stderr: bytes, monkeypatch: pytest.MonkeyPatch +) -> None: + """The 0.5.14 packaged check: a computer with no keyring read as an unknown answer. + + Then an upgraded audit log was never chained, and Doctor and the export + failed for good. There is no keychain to ask: the data folder decides. + """ + run = _runner({"lookup": (1, b"", stderr)}, []) + with pytest.raises(KeychainUnavailable): + await keychain_secret_tool.lookup("svc", "audit-integrity-key-0", run) + + from tstd import audit_key + + async def unavailable(_account: str) -> str: + return await keychain_secret_tool.lookup("svc", "audit-integrity-key-0", run) + + monkeypatch.setattr("tstd.keychain.get_audit_secret", unavailable) + assert audit_key.keychain_get("audit-integrity-key-0")[0] == "unavailable" diff --git a/core/tstd/keychain.py b/core/tstd/keychain.py index 40d27ee9..6714520e 100644 --- a/core/tstd/keychain.py +++ b/core/tstd/keychain.py @@ -64,6 +64,24 @@ class KeychainLockedError(KeychainError): "locked collection", ) +# stderr substrings that mean there is no keychain to ask on this computer: +# no secret service is installed to start on the session bus, or there is no +# session bus at all. Not a locked keychain, which is there and could answer. +# A computer without one keeps its audit key and records in the data folder +# (TD-5420, the 0.5.14 packaged check: it was read as an unknown answer, so an +# upgraded log was never chained and Doctor and the export failed for good). +_ABSENT_MARKERS = ( + "was not provided by any .service files", + "org.freedesktop.dbus.error.serviceunknown", + "cannot autolaunch d-bus", +) + +_ABSENT_GUIDANCE = ( + "No keychain on this computer: no secret service is running on the session bus. " + "Install and start one (for example GNOME Keyring or KeePassXC's secret service) to keep " + "keys there." +) + _LOCKED_GUIDANCE = ( "The login keychain is locked — most often a macOS password change left " "the keychain on the old password. Open Keychain Access, unlock the login " @@ -130,10 +148,13 @@ async def _spawn_cli(*args: str, stdin: bool = False) -> asyncio.subprocess.Proc def _classify_cli_failure(stderr_text: str, fallback: str) -> KeychainError: """Map keychain-CLI stderr to the right error type. - Locked/drifted keychains get KeychainLockedError with unlock guidance; - anything else keeps the raw stderr in a plain KeychainError. + No secret service (or no session bus) is KeychainUnavailable: there is no + keychain here. Locked/drifted keychains get KeychainLockedError with unlock + guidance; anything else keeps the raw stderr in a plain KeychainError. """ low = stderr_text.lower() + if any(marker in low for marker in _ABSENT_MARKERS): + return KeychainUnavailable(_ABSENT_GUIDANCE) if any(marker in low for marker in _LOCKED_MARKERS): return KeychainLockedError(_LOCKED_GUIDANCE) return KeychainError(f"{fallback}: {stderr_text}") diff --git a/docs/configuration.md b/docs/configuration.md index 9ae3b278..097fc1a7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1631,7 +1631,10 @@ locked or cannot be read, that run begins no chain at all (a log with no rows still begins): Doctor fails with "Unlock the OS keychain and restart TST Desk", and the first run that can read the keychain begins it over every row then present, or reports it removed if the record is there. On a computer with no -keychain at all the file record alone decides. An `audit.key` that is not a +keychain at all the file record alone decides: that includes a Linux computer +with `secret-tool` installed but no secret service to start on the session bus, +or no session bus at all (a keyring that is installed but locked is still +locked, not absent). An `audit.key` that is not a regular file (a FIFO, a folder, a symbolic link) counts as unreadable. The database's structure must be exactly what this version's migrations make. From 6d838b9042a51d5579abe6f5140c8095fab63cda Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:13:34 +0000 Subject: [PATCH 02/15] TD-5420: revert reading no secret service as no keychain This reverts commit 74951602. Reading secret-tool's "no secret service" errors as KeychainUnavailable let a keyring that is only unreachable for one run (no session bus, KeePassXC not yet started) mint audit.key and sign with it; once the keyring was back, Doctor reported the record "changed" for good. Main's reading, an unknown answer that begins no chain, heals on the next run that can read the keychain. The next commits keep that and solve the packaged check's problem another way: Doctor says what to do, an opt-in audit.key_store: data_folder, and exports that work while the key store is unavailable. Co-Authored-By: Claude Opus 5.5 --- DECISIONS.md | 11 ----------- core/tests/test_keychain.py | 17 ---------------- core/tests/test_keychain_not_found.py | 28 --------------------------- core/tstd/keychain.py | 25 ++---------------------- docs/configuration.md | 5 +---- 5 files changed, 3 insertions(+), 83 deletions(-) diff --git a/DECISIONS.md b/DECISIONS.md index d39e306b..8832e864 100644 --- a/DECISIONS.md +++ b/DECISIONS.md @@ -21666,17 +21666,6 @@ row), `keychain_secret_tool.py` (Linux reads). when it cannot be used, the file). The begin's reads are lenient, as verify's are, so stored text that is not UTF-8 fails its hash rather than raising and losing every write. -- **No secret service is no keychain** (the 0.5.14 packaged check). On a Linux - computer with `secret-tool` but no secret service to start on the session bus - ("was not provided by any .service files", `ServiceUnknown`) or no session bus - ("Cannot autolaunch D-Bus"), the CLI's error was read as an unknown answer, so - an upgraded log was never chained over its old rows and Doctor and the export - failed for good. `keychain._classify_cli_failure` now reads those as - `KeychainUnavailable`: there is no keychain to ask, so the data folder decides, - as when `secret-tool` is not installed. A keyring that is installed but locked - (a hung lookup, a dismissed unlock prompt) is still locked. This gives a - same-user attacker nothing new: making the keychain unreachable for TST Desk - already needs control of its environment, which also reaches `audit.key`. - **Genesis without UPDATE, and never twice over rows.** The first time the chain is used, one `chain_genesis` row per table is inserted, in its own transaction committed before any chained insert, holding a digest over diff --git a/core/tests/test_keychain.py b/core/tests/test_keychain.py index 6f731147..f029c996 100644 --- a/core/tests/test_keychain.py +++ b/core/tests/test_keychain.py @@ -448,20 +448,3 @@ async def communicate(self, input: bytes | None = None) -> tuple[bytes, bytes]: async def wait(self) -> int: return 0 - - -@pytest.mark.parametrize( - "stderr", - [ - "secret-tool: The name org.freedesktop.secrets was not provided by any .service files", - "Error: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: no such name", - "secret-tool: Cannot autolaunch D-Bus without X11 $DISPLAY", - ], -) -def test_no_secret_service_classifies_as_no_keychain(stderr: str) -> None: - """A computer with no secret service has no keychain; it is not a locked one.""" - from tstd.keychain import KeychainUnavailable - - err = _classify_cli_failure(stderr, "Failed to read keychain") - assert isinstance(err, KeychainUnavailable) - assert "No keychain on this computer" in str(err) diff --git a/core/tests/test_keychain_not_found.py b/core/tests/test_keychain_not_found.py index 51722ba6..50d7a12a 100644 --- a/core/tests/test_keychain_not_found.py +++ b/core/tests/test_keychain_not_found.py @@ -195,31 +195,3 @@ async def test_audit_values_only_reach_audit_accounts() -> None: await kc_mod.get_audit_secret("tst-openrouter") with pytest.raises(KeychainError, match="not an audit keychain account"): await kc_mod.store_audit_secret("tst-openrouter", "ab" * 32) - - -@pytest.mark.parametrize( - "stderr", - [ - b"secret-tool: The name org.freedesktop.secrets was not provided by any .service files", - b"secret-tool: Cannot autolaunch D-Bus without X11 $DISPLAY", - ], -) -async def test_linux_no_secret_service_is_no_keychain( - stderr: bytes, monkeypatch: pytest.MonkeyPatch -) -> None: - """The 0.5.14 packaged check: a computer with no keyring read as an unknown answer. - - Then an upgraded audit log was never chained, and Doctor and the export - failed for good. There is no keychain to ask: the data folder decides. - """ - run = _runner({"lookup": (1, b"", stderr)}, []) - with pytest.raises(KeychainUnavailable): - await keychain_secret_tool.lookup("svc", "audit-integrity-key-0", run) - - from tstd import audit_key - - async def unavailable(_account: str) -> str: - return await keychain_secret_tool.lookup("svc", "audit-integrity-key-0", run) - - monkeypatch.setattr("tstd.keychain.get_audit_secret", unavailable) - assert audit_key.keychain_get("audit-integrity-key-0")[0] == "unavailable" diff --git a/core/tstd/keychain.py b/core/tstd/keychain.py index 6714520e..40d27ee9 100644 --- a/core/tstd/keychain.py +++ b/core/tstd/keychain.py @@ -64,24 +64,6 @@ class KeychainLockedError(KeychainError): "locked collection", ) -# stderr substrings that mean there is no keychain to ask on this computer: -# no secret service is installed to start on the session bus, or there is no -# session bus at all. Not a locked keychain, which is there and could answer. -# A computer without one keeps its audit key and records in the data folder -# (TD-5420, the 0.5.14 packaged check: it was read as an unknown answer, so an -# upgraded log was never chained and Doctor and the export failed for good). -_ABSENT_MARKERS = ( - "was not provided by any .service files", - "org.freedesktop.dbus.error.serviceunknown", - "cannot autolaunch d-bus", -) - -_ABSENT_GUIDANCE = ( - "No keychain on this computer: no secret service is running on the session bus. " - "Install and start one (for example GNOME Keyring or KeePassXC's secret service) to keep " - "keys there." -) - _LOCKED_GUIDANCE = ( "The login keychain is locked — most often a macOS password change left " "the keychain on the old password. Open Keychain Access, unlock the login " @@ -148,13 +130,10 @@ async def _spawn_cli(*args: str, stdin: bool = False) -> asyncio.subprocess.Proc def _classify_cli_failure(stderr_text: str, fallback: str) -> KeychainError: """Map keychain-CLI stderr to the right error type. - No secret service (or no session bus) is KeychainUnavailable: there is no - keychain here. Locked/drifted keychains get KeychainLockedError with unlock - guidance; anything else keeps the raw stderr in a plain KeychainError. + Locked/drifted keychains get KeychainLockedError with unlock guidance; + anything else keeps the raw stderr in a plain KeychainError. """ low = stderr_text.lower() - if any(marker in low for marker in _ABSENT_MARKERS): - return KeychainUnavailable(_ABSENT_GUIDANCE) if any(marker in low for marker in _LOCKED_MARKERS): return KeychainLockedError(_LOCKED_GUIDANCE) return KeychainError(f"{fallback}: {stderr_text}") diff --git a/docs/configuration.md b/docs/configuration.md index 097fc1a7..9ae3b278 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1631,10 +1631,7 @@ locked or cannot be read, that run begins no chain at all (a log with no rows still begins): Doctor fails with "Unlock the OS keychain and restart TST Desk", and the first run that can read the keychain begins it over every row then present, or reports it removed if the record is there. On a computer with no -keychain at all the file record alone decides: that includes a Linux computer -with `secret-tool` installed but no secret service to start on the session bus, -or no session bus at all (a keyring that is installed but locked is still -locked, not absent). An `audit.key` that is not a +keychain at all the file record alone decides. An `audit.key` that is not a regular file (a FIFO, a folder, a symbolic link) counts as unreadable. The database's structure must be exactly what this version's migrations make. From f216b50f43d3bc4cfc5c37d5c8bf837d8a22c393 Mon Sep 17 00:00:00 2001 From: Adam Crocker <77861255+captcrock@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:50:17 +0000 Subject: [PATCH 03/15] TD-5420: no secret service, audit.key_store, and exports a key store cannot verify The 0.5.14 packaged check: on Linux with no keyring running, a log upgraded from 0.5.13 never got its chain over its old rows, Doctor's audit row stayed failed, and both exports refused, taking away the usage export those users had in 0.5.13. By the manager's decision: - Doctor. secret-tool's "was not provided by any .service files", D-Bus ServiceUnknown and "Cannot autolaunch D-Bus" are KeychainNoService: still a plain KeychainError (the audit key's answer "no_service" decides as "error", so nothing is minted), named apart only so Doctor's audit row says "No secret service is running. Start your keyring once and restart TST Desk, or set audit.key_store: data_folder." Status is unchanged; a locked keychain keeps its own text. Listed as a known limit in docs/linux.md. - audit.key_store (keychain by default, or data_folder): read once at daemon start and passed to every AuditStore. data_folder never asks the keychain: audit.key is made if missing (0600, never replaced), the chain-begun record is the file, and the chain begins at once over existing rows. Doctor warns (not ok) that the key is not in the keychain, saying data_folder chose it. A managed policy can lock it. - Switching back: when the keychain first makes its key for a folder it knows nothing of (no key, no chain-begun record), a readable audit.key is copied into it first (audit-folder-key-). That copy, never the file, vouches for a table's rows before the keychain's key signs one there, so the old rows still verify and a key planted later does not. - Exports (audit_integrity): refuse only on a real break (bad link, hash or MAC, a removed marker, a changed table, a tampered or damaged set-aside, an unreadable kept file; and, as before, a log that could not be read for the check). An unanchored chain, rows whose key is not here once the key store was found unavailable (or with data_folder), a newer set-aside, and unsigned rows written after the recorded outage export as "integrity: not verified, key store unavailable since