Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add key match sanity checks and logout if an error occurs

Leendert de Borstc32d01146adc
diff --git a/apps/browser-extension/src/entrypoints/background/AuthHandler.ts b/apps/browser-extension/src/entrypoints/background/AuthHandler.ts
index ad174b43f..98e57f89c 100644
--- a/apps/browser-extension/src/entrypoints/background/AuthHandler.ts
+++ b/apps/browser-extension/src/entrypoints/background/AuthHandler.ts
@@ -1,3 +1,4 @@
+import { AppErrorCode, getAppErrorCode, getErrorTranslationKey } from '@aliasvault/client/api/errors/AppErrorCodes';
import { ClientUpgradeRequiredError } from '@aliasvault/client/api/errors/ClientUpgradeRequiredError';
import { ServerUpdateRequiredError } from '@aliasvault/client/api/errors/ServerUpdateRequiredError';
import { VaultVersionIncompatibleError } from '@aliasvault/client/api/errors/VaultVersionIncompatibleError';
@@ -84,6 +85,9 @@ async function failureResult(context: string, err: unknown, options?: AuthErrorO
if (err instanceof ClientUpgradeRequiredError) {
return { status: 'logout', reasonKey: 'common.errors.clientNotSupported' };
}
+ if (getAppErrorCode(err) === AppErrorCode.KEY_OUT_OF_SYNC) {
+ return { status: 'logout', reasonKey: getErrorTranslationKey(AppErrorCode.KEY_OUT_OF_SYNC) };
+ }
logFailure(context, err);
return { status: 'error', error: await describeAuthError(err, options) };
diff --git a/core/client/src/auth/VaultKeyService.ts b/core/client/src/auth/VaultKeyService.ts
index 04fc4c9ad..42e264c6a 100644
--- a/core/client/src/auth/VaultKeyService.ts
+++ b/core/client/src/auth/VaultKeyService.ts
@@ -75,6 +75,10 @@ export class VaultKeyService {
}
if (!result.vaultKey) {
+ // Sanity check: a server without the key chain this device holds is behind the session.
+ if (await VaultKeyService.hasLocalVaultKey()) {
+ throw new Error(formatErrorWithCode('The server holds no key chain for this account', AppErrorCode.KEY_OUT_OF_SYNC));
+ }
await getPlatform().storage.removeMany([StorageKeys.ENCRYPTED_VEK, StorageKeys.ENCRYPTED_ACCOUNT_KEY, StorageKeys.ACCOUNT_PUBLIC_KEY, StorageKeys.ENCRYPTED_ACCOUNT_PRIVATE_KEY, StorageKeys.SIGNING_PUBLIC_KEY, StorageKeys.ENCRYPTED_SIGNING_PRIVATE_KEY]);
return unlockKeyBase64;
}
diff --git a/core/rust/src/vault_sync/engine.rs b/core/rust/src/vault_sync/engine.rs
index 2f66c49b3..0d66ac258 100644
--- a/core/rust/src/vault_sync/engine.rs
+++ b/core/rust/src/vault_sync/engine.rs
@@ -125,12 +125,7 @@ async fn full_sync_once(ctx: &mut Ctx) -> SyncResult<Flow> {
// A hierarchy another device created since this legacy login shows up as a revision change, so it is checked here.
if needs_pull || ctx.is_dirty {
- match keys::ensure_key_chain_accepted(ctx).await {
- Err(SyncError::KeyOutOfSync) => return Ok(Flow::Done(logout(LogoutReason::PasswordChanged))),
- other => {
- other?;
- }
- }
+ keys::ensure_key_chain_accepted(ctx).await?;
}
announce_phase(ctx, needs_pull, ctx.is_dirty).await;
diff --git a/core/rust/src/vault_sync/errors.rs b/core/rust/src/vault_sync/errors.rs
index 0cca5238a..adb88ba9a 100644
--- a/core/rust/src/vault_sync/errors.rs
+++ b/core/rust/src/vault_sync/errors.rs
@@ -201,12 +201,14 @@ impl SyncError {
SyncError::Timeout(_) => Failure::Coded(ErrorCode::UploadTimeout),
SyncError::ServerUpdateRequired => Failure::Coded(ErrorCode::ServerUpdateRequired),
SyncError::PayloadTooLarge => Failure::Coded(ErrorCode::UploadTooLarge),
+ // The server holds no key chain for this session: only a re-login recovers.
+ SyncError::Http { code: Some(code), .. } if code == "VAULT_KEY_NOT_FOUND" => Failure::Logout(LogoutReason::PasswordChanged),
SyncError::Http { .. } => Failure::Coded(ErrorCode::SyncServerError),
SyncError::VaultLocked => Failure::Coded(ErrorCode::VaultLocked),
SyncError::VaultDecryptFailed(_) => Failure::Coded(ErrorCode::VaultDecryptFailed),
SyncError::UnlockKeyRejected => Failure::Coded(ErrorCode::UnlockKeyRejected),
SyncError::KeyChainUnreadable(_) => Failure::Coded(ErrorCode::KeyChainUnreadable),
- SyncError::KeyOutOfSync => Failure::Coded(ErrorCode::KeyOutOfSync),
+ SyncError::KeyOutOfSync => Failure::Logout(LogoutReason::PasswordChanged),
SyncError::ServerVaultUnreadable(_) => Failure::Coded(ErrorCode::SyncVaultDecryptFailed),
SyncError::Snapshot(_) => Failure::Coded(ErrorCode::SyncVaultFetchFailed),
SyncError::VaultDataRejected(_) => Failure::Coded(ErrorCode::SyncCodecFailed),
@@ -244,7 +246,7 @@ mod tests {
assert_eq!(SyncError::VaultDecryptFailed(String::new()).failure(), Failure::Coded(ErrorCode::VaultDecryptFailed));
assert_eq!(SyncError::UnlockKeyRejected.failure(), Failure::Coded(ErrorCode::UnlockKeyRejected));
assert_eq!(SyncError::KeyChainUnreadable(String::new()).failure(), Failure::Coded(ErrorCode::KeyChainUnreadable));
- assert_eq!(SyncError::KeyOutOfSync.failure(), Failure::Coded(ErrorCode::KeyOutOfSync));
+ assert_eq!(SyncError::KeyOutOfSync.failure(), Failure::Logout(LogoutReason::PasswordChanged));
assert_eq!(serde_json::to_string(&LogoutReason::PasswordChanged).unwrap(), "\"passwordChanged\"");
assert_eq!(SyncError::Auth.failure(), Failure::Logout(LogoutReason::SessionExpired));
assert_eq!(SyncError::Staging(String::new()).failure(), Failure::Coded(ErrorCode::DatabaseInitFailed));
diff --git a/core/rust/src/vault_sync/legacy.rs b/core/rust/src/vault_sync/legacy.rs
index 0af006de9..d13692613 100644
--- a/core/rust/src/vault_sync/legacy.rs
+++ b/core/rust/src/vault_sync/legacy.rs
@@ -25,6 +25,10 @@ pub(crate) const STORAGE_FORMAT_SQLITE_BLOB: &str = "sqlite-blob";
/// Take a legacy snapshot apart for local storage: the blob passes through untouched, the manifest-v1 fingerprints
/// are reset, and the personal manifest id is recorded for the migration push.
pub(crate) async fn open_legacy_snapshot(ctx: &Ctx, snapshot: &GetResponse) -> SyncResult<PulledVault> {
+ // Sanity check: a server that serves a sqlite-blob vault to a device holding a key chain is behind the session.
+ if keys::has_cached_key_chain(&ctx.host).await? {
+ return Err(SyncError::KeyOutOfSync);
+ }
state::remove(&ctx.host, state::VAULT_CONTENT_FINGERPRINTS).await?;
let revision = snapshot.legacy_revision.unwrap_or(0);
let mut manifest_revisions = HashMap::new();
diff --git a/core/rust/src/vault_sync/pull.rs b/core/rust/src/vault_sync/pull.rs
index 6c43764d8..34e995dc4 100644
--- a/core/rust/src/vault_sync/pull.rs
+++ b/core/rust/src/vault_sync/pull.rs
@@ -426,6 +426,7 @@ async fn resolve_granted_vek(ctx: &Ctx, dto: &ManifestDto) -> SyncResult<String>
if !keys::grant_signature_verifies(dto, &public_key, &algorithm, &encrypted_vek) {
return Err(SyncError::ServerVaultUnreadable(format!("the grant on shared manifest {} carries no valid signature, refusing to assemble", dto.manifest_id)));
}
- let private_key = keys::resolve_grant_private_key(ctx, &public_key).ok_or_else(|| SyncError::Snapshot(format!("this session holds no account private key that opens the grant on shared manifest {}, refusing to assemble", dto.manifest_id)))?;
+ // Sanity check: no key this session holds opens the grant, so the session is behind the server.
+ let private_key = keys::resolve_grant_private_key(ctx, &public_key).ok_or(SyncError::KeyOutOfSync)?;
keys::decrypt_manifest_vek(&encrypted_vek, &dto.manifest_id, &private_key).map_err(|e| SyncError::ServerVaultUnreadable(format!("failed to decrypt the VEK of shared manifest {}, refusing to assemble: {}", dto.manifest_id, e)))
}
diff --git a/core/rust/src/vault_sync/tests/sync.rs b/core/rust/src/vault_sync/tests/sync.rs
index 352e2ce11..6457b5539 100644
--- a/core/rust/src/vault_sync/tests/sync.rs
+++ b/core/rust/src/vault_sync/tests/sync.rs
@@ -376,6 +376,18 @@ fn password_changed_elsewhere_requires_logout() {
assert_eq!(result["logoutReason"], "passwordChanged");
}
+#[test]
+fn a_write_the_server_refuses_for_a_missing_key_chain_requires_logout() {
+ let mut s = synced(|db| insert_item(db, ITEM_A, "Server item"));
+ s.host.responders.insert(0, Box::new(|m, p, _| if m == "POST" && p == "Vault" { Some((400, json!({ "code": "VAULT_KEY_NOT_FOUND", "statusCode": 400 }))) } else { None }));
+
+ s.host.edit(|db| insert_item(db, ITEM_B, "Local item"));
+ let result = s.host.sync();
+
+ assert_eq!(result["requiresLogout"], true, "{}", result);
+ assert_eq!(result["logoutReason"], "passwordChanged");
+}
+
#[test]
fn status_check_reports_newer_server_state_without_touching_the_vault() {
let mut s = synced(|db| insert_item(db, ITEM_A, "Server item"));