Skip to content

fix: report a missing keychain key instead of silently starting a new one - #32

Merged
matej21 merged 4 commits into
mainfrom
fix/t1034-missing-keychain-notice
Nov 24, 2026
Merged

matej21 merged 4 commits into
mainfrom
fix/t1034-missing-keychain-notice

Conversation

@matej21

@matej21 matej21 commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Summary

When the app database recorded its key's fingerprint (keychain.masterKeyCheck) but the recorded keychain item is gone:

  • a normal start (keepRecordedItem) created a fresh key under the recorded name and reported current;
  • a move (moveKey) whose previous item was missing recorded the new item and fingerprint and reported current.

Either way saved passwords silently stopped decrypting, and the fresh item or the rewritten settings stood in the way of restoring the original key.

These paths now return a new outcome, key-missing, which mirrors key-mismatch. So does keepPrevious, a move that could not create its new item or was deferred. It fires when the item is absent (store.get returned null), the database recorded a fingerprint, and one of these holds:

  • this database holds a non-empty saved password that no key at hand decrypts (the machine-derived key, plus on a move the new item's key);
  • for a normal start, another channel's database records the same fingerprint and holds such a password. Creating a key under the shared name would make that channel refuse it.

On key-missing:

  • nothing is created under the recorded name and neither setting is touched, so a restored item works again (a normal start reports current, a move reports migrated);
  • the machine-derived key is used for the session, and passwords saved meanwhile are re-encrypted once the key is back or reset;
  • the window shows a notice naming the item: saved passwords can't be decrypted, restore the item (for example from a keychain backup) and restart, or reset the key; new passwords use the weaker machine-derived key until then. It links to a new "When the key is missing" section in docs/keychain.md. When another channel is the reason, the log names its database.

Everything else stays as before and quiet: a fresh install, a database that only ever ran without a keychain, one with no saved passwords or only empty ones, and the start after the documented reset all get a fresh key without a word. An item that holds something other than a key keeps the earlier fallback path.

Decisions

Decision (brief) Where Status
"when a fingerprint was recorded but the item is gone, return an outcome that makes the app show a notice (what happened, that saved passwords cannot be decrypted, what the user can do), instead of "current"" keepRecordedItem → keyMissing in src/backend-shared/services/master-key.ts; notice in src/frontend-shared/stores/key-status.ts deviation: narrowed. Only when saved non-empty passwords (here, or in a channel sharing the item) need the key; otherwise the notice would claim a loss that did not happen and show on every start (review round 1 #1, round 2 #1)
"Same for moveKey" moveKey, before the recording transaction; also keepPrevious done, same narrowing
"Do not delete or overwrite anything the user might still recover" keyMissing creates no item and writes no setting; the sibling check stops a fresh key under a name another channel's passwords need; tests assert the settings are unchanged and that restoring the item brings the passwords back done
"Tests for both paths and for the normal path staying quiet" tests/master-key.test.ts (normal start, move, failed and deferred move, shared item, via setupMasterKey and storage.keyStatus; quiet starts without a fingerprint, without lost passwords and after a reset), tests/key-status-store.test.ts, src/backend-shared/services/encryption.test.ts done

Verification

  • bun test tests/master-key.test.ts tests/key-status-store.test.ts src/backend-shared/services/encryption.test.ts: 84 pass, 1 skip (a macOS-only codesign test). The new tests fail with the guards removed: the 4 key-missing tests against the original master-key.ts, and 3 tests with the sibling and empty-password guards removed.
  • bunx tsc --noEmit, bun run lint, bun run format:check: clean. CI runs the full suite.
  • UI: the notice was rendered in demo mode by feeding the store the status each version reports (before: current, no notice; after: key-missing, notice shown).

Status

  • Review rounds: 1 changes (4b99528), 2 changes (29b5f39), 3 approve (fb50b85). Round 3's minor and nit findings were fixed in 8958f70; see the response comment.
  • CI: green on 8958f70 (run 45401204094).
  • Merged as 815fdfa (merge commit) on the user's decision. No release or tag.

🤖 Generated with Claude Code

matej21 and others added 2 commits November 24, 2026 13:58
… one

When the database recorded its key's fingerprint but the recorded keychain
item is gone, a normal start created a fresh key under that name and a move
recorded the new item, both reporting "current". Saved passwords then stopped
decrypting without a word, and the fresh item or the rewritten settings stood
in the way of restoring the original key.

Both paths now return `key-missing`: nothing is created under the recorded
name, neither setting is touched, the machine-derived key is used for the
session, and the window names the item and says how to restore it or reset.
A database without a recorded fingerprint still gets a fresh key quietly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A database with no saved passwords lost nothing when its key item vanished,
yet got the "can't be decrypted" notice on every start with no way out but
editing settings by hand. `key-missing` now also needs a saved secret that
no key at hand decrypts; otherwise a fresh key is started quietly.

A move that cannot create its new item and finds the recorded item gone now
reports `key-missing` too, instead of the keychain-unavailable notice.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Review round 1

Reviewer: independent Claude agent (Opus 5.5, general-purpose), read-only. Commit reviewed: 4b99528. Verdict: changes.

Confirmed correct: a start with no recorded fingerprint stays quiet (fresh install, fallback-only database, after the documented reset). A sibling channel's move cannot delete an item this channel still records (stillNeeded checks the sibling's fingerprint). The previous-unreadable and move-deferred paths are unchanged. Nothing recoverable is deleted or overwritten. A restored item recovers, and passwords saved in the meantime are re-encrypted. The decision table matches the code.

# Severity Where Finding
1 major master-key.ts keepRecordedItem / moveKey key-missing also fires for a database with no saved secrets (only SQLite connections, or none). That user gets "can't be decrypted" on every start, and the only way out is editing settings by hand. Before the PR this was a harmless quiet new key.
2 minor master-key.ts moveKey The new item is created before the old item is found missing, so each key-missing start leaves an unused new item behind. Passwords saved under that key during an earlier previous-unreadable start show empty during key-missing starts.
3 minor master-key.ts keepPrevious When a move can't create or defers and the recorded item is missing, the start reports fallback, whose notice gives the wrong fix ("unlock the keychain").
4 nit docs/keychain.md "It records nothing" leaves out the new item the move path leaves in the keychain.
5 nit docs/keychain.md Channels Deleting app-encryption-key-v1 by hand now gives key-missing on a channel that still records it.
6 nit tests/master-key.test.ts No test for the reset path after key-missing.

@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Response to review round 1

# Outcome
1 fixed in 29b5f39: key-missing now also needs a saved secret that no key at hand decrypts (new AppDatabase.holdsSecretNoneDecrypt, next to usesKey). Otherwise a fresh key is started quietly. Test: a database with only a SQLite connection plus a missing item gives current, on the normal path and on the move path.
2 partly fixed in 29b5f39: on the move path the check counts the new key as at hand, so passwords saved under it during a previous-unreadable start no longer trigger key-missing by themselves. Won't fix for the rest: the session keeps the machine-derived key so that both paths behave the same and the notice's "weaker key" sentence stays true. The leftover item is harmless, since nothing vouches for it and the next move replaces it, and the docs now name it.
3 fixed in 29b5f39: keepPrevious returns key-missing when the recorded item is gone and passwords need it. Test added.
4 fixed in 29b5f39: the docs say which setting stays unchanged and name the item a move leaves behind.
5 fixed in 29b5f39: one sentence in Channels.
6 fixed in 29b5f39: test that a reset after key-missing gives current, and that a password saved meanwhile decrypts.

Finding 1 was major, so round 2 reviews 29b5f39.

An empty password is stored encrypted too, so a connection without one still
raised the missing-key notice although nothing was lost; it no longer counts.

A channel without saved passwords no longer starts a fresh key under an item
it shares with a channel whose passwords still need the missing key: that
channel would get its key refused, and restoring the item would replace the
fresh one. Both report `key-missing` instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Review round 2

Reviewer: independent Claude agent (Opus 5.5, general-purpose), read-only. Commit reviewed: 29b5f39. Verdict: changes.

Confirmed correct: lostKey and the keys each path passes, the connectionsUseKey refactor (vouching unchanged), the nothing-lost path drops nothing in this database, and the existing fallback tests still match.

# Severity Where Finding
1 major app-db.ts holdsSecretNoneDecrypt An empty password is stored encrypted (IV and tag, no ciphertext), so a connection without a password still gives key-missing on every start.
2 minor master-key.ts keepRecordedItem A channel with no secrets that shares the item with a channel whose secrets need it creates a fresh key under the shared name. The other channel then gets key-mismatch, and restoring the item would replace the fresh key. The base behaved the same, but the docs claim otherwise.
3 nit master-key.ts outcome docs, docs/keychain.md item-unavailable, move-deferred and the move steps still say only fallback for a missing recorded item.
4 nit tests/master-key.test.ts No test for move-deferred with the item missing and passwords needed.

@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Response to review round 2

# Outcome
1 fixed in fb50b85: new encryptsEmptyPassword in encryption.ts. The lost-key check skips empty passwords, and connectionsUseKey (vouching) is unchanged. The no-passwords test now also has a Postgres connection with an empty password.
2 fixed in fb50b85: before creating a key under the recorded name, keepRecordedItem also checks sibling channels that record the same fingerprint (new path-based databaseHoldsSecretNoneDecrypt; an unreadable sibling counts as needing the key). Test: canary without passwords gives key-missing, and stable gets its passwords back once the item is restored.
3 fixed in fb50b85 in all three places.
4 fixed in fb50b85: test for a deferred move with the item missing.

Round 2 had a major finding, so round 3 reviews fb50b85.

…items on the old path

- An item holding something other than a key is not "missing": it keeps the
  earlier fallback outcome, so the reset advice stays right.
- When another channel's passwords need the shared item, the log names that
  channel's database and the docs say how to get out of it.
- An unreadable sibling database is ignored rather than said to block.
- Tests for one-character passwords and for siblings that must not block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Review round 3

Reviewer: independent Claude agent (Opus 5.5, general-purpose), read-only. Commit reviewed: fb50b85. Verdict: approve (no blocker or major).

Confirmed: encryptsEmptyPassword never judges a non-empty password empty. The machine-derived key is the same for every channel. Nothing recoverable is deleted or overwritten. The tests check behaviour.

# Severity Finding
1 minor The PR description is stale: keepPrevious, the narrowed condition, the sibling case, test counts and Status.
2 minor A stale sibling channel can hold this channel at key-missing. Nothing names that channel, and the docs don't say how to get out.
3 minor ifUnreadable: true in the sibling check is dead: readDatabaseSetting already returns null for an unreadable file. The round 2 response overstated it.
4 nit An item holding something that is not a key was reported as missing, and the reset advice then fails.
5 nit Two comments are worded imprecisely (encryptsEmptyPassword, the key-missing outcome).
6 nit No test for a 1-character password, or for siblings that must not block.

@matej21

matej21 commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Response to review round 3

Only minor and nit findings, so no further round.

# Outcome
1 fixed: the description is rewritten to match 8958f70, with decision row 1 marked as narrowed.
2 fixed in 8958f70: the log names the sibling database whose passwords need the key, and "When the key is missing" says to restore the item or remove an unused channel folder, and what resetting this channel costs.
3 fixed in 8958f70: the sibling check passes false, so an unreadable sibling is ignored. This corrects the round 2 response, which said it counts as needing the key.
4 fixed in 8958f70: key-missing only when store.get returned null. An undecodable value keeps the earlier path. Test added.
5 fixed in 8958f70.
6 fixed in 8958f70: encryption.test.ts covers '', 'a' and malformed values, and a test covers siblings that recorded a different key or whose passwords the machine-derived key reads.

@matej21
matej21 merged commit 815fdfa into main Nov 24, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant