Skip to content

Commit ca504a4

Browse files
committed
fix(desktop): keep keyring identity until a recovery path exists
recover_from_keyring deleted the OS keyring entry on parse failure before checking for a leftover identity.key. Steady-state installs are marker-only, so that delete destroyed the only copy and forced Lost. Probe the file first, and leave a corrupt keyring value in place when entering Lost. Signed-off-by: Taksh <[email protected]>
1 parent c3132c3 commit ca504a4

2 files changed

Lines changed: 51 additions & 19 deletions

File tree

‎desktop/src-tauri/src/app_state.rs‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -634,23 +634,23 @@ fn resolve_identity_with_store(
634634
})
635635
}
636636

637-
/// Recover from a corrupt nsec in the keyring (parse failed). Clear the bad
638-
/// keyring value, then migrate a valid leftover `identity.key` if one exists.
639-
/// If the migration marker is present but no valid file exists, the prior
640-
/// identity is unrecoverable — return `Lost` recovery rather than silently
641-
/// generating a new identity. Generating fresh is only correct when no prior
642-
/// identity ever existed (no marker). The keyring delete is best-effort: a
643-
/// delete failure logs and continues — it must never block startup.
637+
/// Recover from a corrupt nsec in the keyring (parse failed). Prefer a valid
638+
/// leftover `identity.key` first. Only clear the keyring entry once a
639+
/// replacement exists, or when there was never a prior identity (no migration
640+
/// marker). If the marker is present but no valid file exists, return `Lost`
641+
/// and leave the corrupt keyring value in place for support / manual export.
644642
fn recover_from_keyring(
645643
store: &impl IdentityKeyStore,
646644
legacy_path: &std::path::Path,
647645
data_dir: &std::path::Path,
648646
error: &str,
649647
) -> Result<ResolvedIdentity, String> {
650-
eprintln!("buzz-desktop: corrupt nsec in keyring ({error}), clearing and recovering from file");
651-
if let Err(e) = store.delete(IDENTITY_KEY_NAME) {
652-
eprintln!("buzz-desktop: failed to clear corrupt keyring value: {e}");
653-
}
648+
eprintln!(
649+
"buzz-desktop: corrupt nsec in keyring ({error}), looking for a recovery path before clearing"
650+
);
651+
// Never delete the keyring entry until a replacement exists. Steady-state
652+
// installs are marker-only: clearing first would destroy the only copy of
653+
// the identity even when the parse failure is transient or tool-side.
654654
if legacy_path.exists() {
655655
if let Some(keys) = migrate_identity_file(store, legacy_path, data_dir)? {
656656
return Ok(ResolvedIdentity {
@@ -661,14 +661,12 @@ fn recover_from_keyring(
661661
}
662662
}
663663
// No valid file to recover from. If the migration marker exists, a prior
664-
// identity was stored in the keyring and is now corrupt AND gone — the key
665-
// is unrecoverable. Enter Lost recovery instead of silently rotating.
664+
// identity was stored in the keyring — keep the corrupt entry for support /
665+
// manual export and enter Lost rather than silently rotating.
666666
if migration_marker_path(data_dir).exists() {
667667
let ephemeral = Keys::generate();
668668
eprintln!(
669-
"buzz-desktop: identity lost — keyring had corrupt data and no valid identity.key \
670-
backup; prior identity (migration marker present) is unrecoverable; \
671-
using ephemeral key {}, awaiting user re-import",
669+
"buzz-desktop: identity lost — keyring value failed to parse and no valid identity.key backup exists; leaving the keyring entry in place; using ephemeral key {}, awaiting user re-import",
672670
ephemeral.public_key().to_hex()
673671
);
674672
return Ok(ResolvedIdentity {
@@ -677,7 +675,11 @@ fn recover_from_keyring(
677675
storage: IdentityStorage::Ephemeral,
678676
});
679677
}
680-
// No marker: genuine first launch with a corrupt keyring. Generate fresh.
678+
// No marker: genuine first launch with a corrupt keyring. Safe to clear and
679+
// generate fresh — there was never a durable identity to protect.
680+
if let Err(e) = store.delete(IDENTITY_KEY_NAME) {
681+
eprintln!("buzz-desktop: failed to clear corrupt keyring value: {e}");
682+
}
681683
let (keys, storage) = generate_and_persist(store, legacy_path, data_dir)?;
682684
Ok(ResolvedIdentity {
683685
keys,
@@ -686,8 +688,6 @@ fn recover_from_keyring(
686688
})
687689
}
688690

689-
/// Load the `0o600` identity file, quarantining corruption, else generate and
690-
/// save a fresh key to the file. Used when no keyring is available.
691691
fn load_file_or_generate(
692692
legacy_path: &std::path::Path,
693693
data_dir: &std::path::Path,

‎desktop/src-tauri/src/app_state_tests.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,11 +1385,43 @@ fn corrupt_keyring_marker_present_no_file_is_lost() {
13851385
"corrupt keyring + marker + no file must return Lost recovery, not a fresh key"
13861386
);
13871387

1388+
// Leave the corrupt keyring value in place; deleting it would destroy the
1389+
// only remaining copy before any support/export path can run.
1390+
assert!(
1391+
!store
1392+
.deleted
1393+
.borrow()
1394+
.contains(&IDENTITY_KEY_NAME.to_string()),
1395+
"Lost recovery must not delete the corrupt keyring entry"
1396+
);
1397+
assert!(
1398+
store.slot.borrow().contains_key(IDENTITY_KEY_NAME),
1399+
"corrupt keyring entry must still be present after Lost recovery"
1400+
);
1401+
13881402
// No identity.key written — the ephemeral key is in-memory only.
13891403
assert!(!legacy_path.exists());
13901404
}
13911405

13921406
#[test]
1407+
1408+
#[test]
1409+
fn corrupt_keyring_with_valid_file_recovers_before_delete() {
1410+
// Present(corrupt) + valid identity.key (+ marker) must adopt the file
1411+
// without entering Lost, and without needing the corrupt keyring value.
1412+
let dir = tempfile::tempdir().unwrap();
1413+
let legacy_path = dir.path().join("identity.key");
1414+
let file_keys = Keys::generate();
1415+
save_key_file(&legacy_path, &file_keys).unwrap();
1416+
write_migration_marker(&migration_marker_path(dir.path())).unwrap();
1417+
1418+
let store = FakeIdentityStore::present_with("not-a-valid-nsec");
1419+
let resolved = resolve_identity_with_store(&store, &legacy_path, dir.path()).unwrap();
1420+
1421+
assert_eq!(resolved.recovery, RecoveryState::None);
1422+
assert_key_eq(&file_keys, &resolved.keys);
1423+
}
1424+
13931425
fn corrupt_keyring_no_marker_no_file_generates_fresh() {
13941426
// I2 (counter-case): Present(corrupt) + NO marker + no identity.key →
13951427
// genuine first launch with a corrupt keyring, no prior identity to

0 commit comments

Comments
 (0)