store: name the wrong passphrase, not the raw crypto error

An import of an `ncryptsec` with the wrong password surfaced the NIP-49
crypto failure verbatim — an opaque `aead::Error`. Translate it to
"Wrong ncryptsec password." by matching on the error kind, so an
unsupported or tampered `log_n` is not mislabelled as a wrong password.

Shorten the vault-passphrase error to "Wrong vault passphrase." as well:
the old sentence was too long for the dialog's toast.
This commit is contained in:
raucao committed 2026-11-30 16:14:14 +04:00
1 parent 969154d790
commit 787931898c
1 file changed
+23 -9
+23 -9
View File
@@ -395,8 +395,20 @@ impl KeyStore {
let ncryptsec = Zeroizing::new(ncryptsec.trim().to_string());
let import_password = Zeroizing::new(import_password.to_string());
let keys = tokio::task::spawn_blocking(move || {
let encrypted = EncryptedSecretKey::from_bech32(ncryptsec.as_str())?;
Ok::<_, Error>(Keys::new(encrypted.decrypt(import_password.as_str())?))
let encrypted = EncryptedSecretKey::from_bech32(ncryptsec.as_str())
.map_err(|error| format!("That is not a valid ncryptsec: {error}"))?;
let secret = encrypted
.decrypt(import_password.as_str())
.map_err(|error| match error.kind() {
// NIP-49 authenticates the ciphertext, so on a well-formed
// `ncryptsec` a crypto failure means the password is wrong —
// and the raw error is an opaque `aead::Error`.
ErrorKind::Crypto => "Wrong ncryptsec password.".to_string(),
// Anything else (e.g. an unsupported/tampered `log_n`) is not
// a password problem.
_ => format!("Could not open the ncryptsec: {error}"),
})?;
Ok::<_, Error>(Keys::new(secret))
})
.await
.map_err(|error| format!("the import task failed: {error}"))??;
@@ -603,7 +615,7 @@ impl KeyStore {
if opens {
Ok(())
} else {
Err("that passphrase does not open this vault".into())
Err("Wrong vault passphrase.".into())
}
}
@@ -886,7 +898,7 @@ mod tests {
.import_user_key("Sneaky", &keys.secret_key().to_bech32().unwrap(), "typo")
.await
.expect_err("a second passphrase must not be accepted");
assert!(error.to_string().contains("does not open"));
assert!(error.to_string().contains("vault passphrase"));
// Nothing was written, and the vault still opens with the real one.
store.lock();
@@ -957,11 +969,13 @@ mod tests {
.await
.expect("import");
assert_eq!(imported, key);
let error = store
.import_user_key_encrypted("Backup", &ncryptsec, "wrong", "hunter2")
.await
.expect_err("a wrong ncryptsec password fails");
assert!(
store
.import_user_key_encrypted("Backup", &ncryptsec, "wrong", "hunter2")
.await
.is_err()
error.to_string().contains("ncryptsec password"),
"the raw crypto error must not leak: {error}"
);
let _ = std::fs::remove_dir_all(path.parent().unwrap());
}
@@ -1025,7 +1039,7 @@ mod tests {
.import_user_key("Sneaky", &secret, "typo")
.await
.expect_err("the verifier must refuse a wrong passphrase");
assert!(error.to_string().contains("does not open"), "got: {error}");
assert!(error.to_string().contains("vault passphrase"), "got: {error}");
assert!(store.list_user_keys().is_empty(), "nothing was stored");
let added = store