Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Move vault creation register logic to Rust and refactor ApiError return (#2413)
Leendert de Borst3dfefd971ba9
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsxindex baeccc1d6..e88371474 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/auth/Login.tsx@@ -1,4 +1,4 @@-import { ApiAuthError } from '@aliasvault/client/api/errors/ApiAuthError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import { hasErrorCode, getErrorMessage } from '@aliasvault/client/api/errors/AppErrorCodes';import { ClientUpgradeRequiredError } from '@aliasvault/client/api/errors/ClientUpgradeRequiredError';import { ServerUpdateRequiredError } from '@aliasvault/client/api/errors/ServerUpdateRequiredError';@@ -25,6 +25,7 @@ import { useLoading } from '@/entrypoints/popup/context/LoadingContext';import { useWebApi } from '@/entrypoints/popup/context/WebApiContext';import { PopoutUtility } from '@/entrypoints/popup/utils/PopoutUtility';+import { apiErrorMessage } from '@/utils/ApiErrors';import { StorageKeys } from '@/utils/constants/storageKeys';import { logFailure } from '@/utils/Diagnostics';import { sendMessage } from '@/utils/messaging/ExtensionMessaging';@@ -105,9 +106,9 @@ const Login: React.FC = () => {} else if (err instanceof VaultProcessingError) {// The vault was fetched but couldn't be decrypted/materialized, surface the real error (copyable) for support.setVaultError(err);- } else if (err instanceof ApiAuthError) {- // Show API authentication errors as-is.- setError(t('common.apiErrors.' + err.message));+ } else if (apiErrorCodeOf(err)) {+ // The server refused the login with a coded reason (wrong password, account locked).+ setError(apiErrorMessage(err, t, t('common.errors.serverError')));} else if (hasErrorCode(err)) {// Error contains an error code (E-XXX), show the formatted message.setError(getErrorMessage(err, t('common.errors.serverError')));diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsxindex 5e8f0be13..cfaca3ff3 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/FamilySharingSettings.tsx@@ -1,4 +1,4 @@-import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import { MasterPasswordService } from '@aliasvault/client/auth/MasterPasswordService';import { canAdministerGroup, describeMemberAccess, familySharingText, holdsManifestKey, ownUserIdIn, roleLabel, sharingErrorMessage } from '@aliasvault/client/sharing/FamilySharingView';import { multiManifestRendering } from '@aliasvault/client/sharing/MultiManifestRendering';@@ -116,9 +116,7 @@ const FamilySharingSettings: React.FC = () => {return actionError.message.length > 0 ? actionError.message : fallback;}- const code = actionError instanceof BackgroundApiError- ? actionError.message- : actionError instanceof ApiRequestError ? actionError.apiErrorCode : null;+ const code = actionError instanceof BackgroundApiError ? actionError.message : apiErrorCodeOf(actionError);const knownError = sharingErrorMessage(code);if (knownError) {@@ -222,7 +220,7 @@ const FamilySharingSettings: React.FC = () => {try {await SharingService.deleteSharedManifest(webApi, target.group.groupId, target.manifest.manifestId, async challenge => (await MasterPasswordService.answerSrpChallenge(challenge, password)).proof);} catch (deleteError) {- if (deleteError instanceof ApiRequestError && deleteError.apiErrorCode === 'PASSWORD_MISMATCH') {+ if (apiErrorCodeOf(deleteError) === 'PASSWORD_MISMATCH') {throw new Error(t('common.errors.wrongPassword'));}diff --git a/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx b/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsxindex e77957f69..d12b39228 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/settings/security/ChangePasswordSettings.tsx@@ -1,4 +1,3 @@-import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';import { IncorrectPasswordError, MasterPasswordService, PasswordChangedElsewhereError } from '@aliasvault/client/auth/MasterPasswordService';import { MIN_ACCEPTED_PASSWORD_LENGTH } from '@aliasvault/client/utilities/PasswordStrength';import React, { useEffect, useState } from 'react';@@ -14,6 +13,7 @@ import { useLoading } from '@/entrypoints/popup/context/LoadingContext';import { useWebApi } from '@/entrypoints/popup/context/WebApiContext';import { useVaultSync } from '@/entrypoints/popup/hooks/useVaultSync';+import { apiErrorMessage } from '@/utils/ApiErrors';import { logFailure } from '@/utils/Diagnostics';import { removeAndDisablePin } from '@/utils/PinUnlockService';@@ -66,7 +66,7 @@ const PasswordInput: React.FC<PasswordInputProps> = ({ id, label, value, setValu* Change master password settings page.*/const ChangePasswordSettings: React.FC = () => {- const { t, i18n } = useTranslation();+ const { t } = useTranslation();const dbContext = useDb();const webApi = useWebApi();const { setIsInitialLoading, showLoading, hideLoading } = useLoading();@@ -96,10 +96,7 @@ const ChangePasswordSettings: React.FC = () => {if (err instanceof PasswordChangedElsewhereError) {return t('common.errors.passwordChanged');}- if (err instanceof ApiRequestError && err.apiErrorCode && i18n.exists(`common.apiErrors.${err.apiErrorCode}`)) {- return t(`common.apiErrors.${err.apiErrorCode}`);- }- return t('common.errors.unknownErrorTryAgain');+ return apiErrorMessage(err, t, t('common.errors.unknownErrorTryAgain'));};/**diff --git a/apps/browser-extension/src/utils/ApiErrors.ts b/apps/browser-extension/src/utils/ApiErrors.tsnew file mode 100644index 000000000..156e6bf77--- /dev/null+++ b/apps/browser-extension/src/utils/ApiErrors.ts@@ -0,0 +1,19 @@+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';++import type { TFunction } from 'i18next';++/**+ * The message for a failed API call: the translated API error code when the server sent one, else the fallback.+ * @param error - the thrown error+ * @param t - the translation function+ * @param fallback - the message when no known code is present+ */+export function apiErrorMessage(error: unknown, t: TFunction, fallback: string): string {+ const code = apiErrorCodeOf(error);+ if (code) {+ const key = `common.apiErrors.${code}`;+ const translated = t(key);+ return translated === key ? fallback : translated;+ }+ return fallback;+}diff --git a/apps/mobile-app/app/(tabs)/settings/family-sharing.tsx b/apps/mobile-app/app/(tabs)/settings/family-sharing.tsxindex 2beebf1db..0a6eb18ff 100644--- a/apps/mobile-app/app/(tabs)/settings/family-sharing.tsx+++ b/apps/mobile-app/app/(tabs)/settings/family-sharing.tsx@@ -1,4 +1,4 @@-import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import { canAdministerGroup, describeMemberAccess, familySharingText, holdsManifestKey, ownUserIdIn, roleLabel, sharingErrorMessage } from '@aliasvault/client/sharing/FamilySharingView';import { multiManifestRendering } from '@aliasvault/client/sharing/MultiManifestRendering';import { SharingService } from '@aliasvault/client/sharing/SharingService';@@ -97,7 +97,7 @@ export default function FamilySharingScreen(): React.ReactNode {return actionError.message;}- const code = actionError instanceof ApiRequestError ? actionError.apiErrorCode : null;+ const code = apiErrorCodeOf(actionError);const knownError = sharingErrorMessage(code);if (knownError) {return knownError;@@ -244,7 +244,7 @@ export default function FamilySharingScreen(): React.ReactNode {await SharingService.deleteSharedManifest(webApi, group.groupId, manifest.manifestId, challenge => NativeVaultManager.deriveSrpProof(challenge.salt, challenge.srpIdentity, challenge.serverEphemeral));} catch (deleteError) {// Local unlock key could mismatch what is actually stored on server (recent password change on other device), if so we show a incorrect password error.- if (deleteError instanceof ApiRequestError && deleteError.apiErrorCode === 'PASSWORD_MISMATCH') {+ if (apiErrorCodeOf(deleteError) === 'PASSWORD_MISMATCH') {throw new SharingOperationError(t('auth.errors.incorrectPassword'));}diff --git a/apps/mobile-app/app/login.tsx b/apps/mobile-app/app/login.tsxindex e763ad328..dc821b70c 100644--- a/apps/mobile-app/app/login.tsx+++ b/apps/mobile-app/app/login.tsx@@ -1,6 +1,6 @@import { Buffer } from 'buffer';-import { ApiAuthError } from '@aliasvault/client/api/errors/ApiAuthError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import { ClientUpgradeRequiredError } from '@aliasvault/client/api/errors/ClientUpgradeRequiredError';import { SrpAuthService } from '@aliasvault/client/auth/SrpAuthService';import { SrpLoginService } from '@aliasvault/client/auth/SrpLoginService';@@ -11,6 +11,7 @@ import React, { useState, useEffect, useRef } from 'react';import { StyleSheet, View, Text, TextInput, ActivityIndicator, Animated, ScrollView, KeyboardAvoidingView, Platform, Dimensions } from 'react-native';import { SafeAreaView } from 'react-native-safe-area-context';+import { apiErrorMessage } from '@/utils/ApiErrors';import { useApiUrl } from '@/utils/ApiUrlUtility';import { AppUnlockUtility } from '@/utils/AppUnlockUtility';import EncryptionUtility from '@/utils/EncryptionUtility';@@ -435,9 +436,9 @@ export default function LoginScreen() : React.ReactNode {// Server refused this app version; show the notice translated, keyed on its error code.console.error('Client upgrade required:', err);setError(t('vault.errors.versionNotSupported'));- } else if (err instanceof ApiAuthError) {- console.error('ApiAuthError error:', err);- setError(t(`apiErrors.${err.message}`));+ } else if (apiErrorCodeOf(err)) {+ console.error('Login refused:', err);+ setError(apiErrorMessage(err, t, t('auth.errors.serverError')));} else if (err instanceof LocalAuthError) {console.error('Network/SSL error:', err);setError((err as LocalAuthError).message);@@ -468,7 +469,7 @@ export default function LoginScreen() : React.ReactNode {const code = twoFactorCode.trim();if (!/^\d{6}$/.test(code)) {- throw new ApiAuthError(t('auth.errors.invalidAuthCode'));+ throw new LocalAuthError(t('auth.errors.invalidAuthCode'));}const validationResponse = await srpUtil.validateLogin2Fa(@@ -497,8 +498,8 @@ export default function LoginScreen() : React.ReactNode {if (err instanceof ClientUpgradeRequiredError) {// Server refused this app version; show the notice translated, keyed on its error code.setError(t('vault.errors.versionNotSupported'));- } else if (err instanceof ApiAuthError) {- setError(t(`apiErrors.${err.message}`));+ } else if (apiErrorCodeOf(err)) {+ setError(apiErrorMessage(err, t, t('auth.errors.serverError')));} else if (err instanceof LocalAuthError) {setError((err as Error).message);} else {diff --git a/apps/mobile-app/utils/ApiErrors.ts b/apps/mobile-app/utils/ApiErrors.tsnew file mode 100644index 000000000..542eac341--- /dev/null+++ b/apps/mobile-app/utils/ApiErrors.ts@@ -0,0 +1,19 @@+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';++import type { TFunction } from 'i18next';++/**+ * The message for a failed API call: the translated API error code when the server sent one, else the fallback.+ * @param error - the thrown error+ * @param t - the translation function+ * @param fallback - the message when no known code is present+ */+export function apiErrorMessage(error: unknown, t: TFunction, fallback: string): string {+ const code = apiErrorCodeOf(error);+ if (code) {+ const key = `apiErrors.${code}`;+ const translated = t(key);+ return translated === key ? fallback : translated;+ }+ return fallback;+}diff --git a/apps/mobile-app/utils/types/errors/LocalAuthError.ts b/apps/mobile-app/utils/types/errors/LocalAuthError.tsindex 8f92ecad9..34eec45be 100644--- a/apps/mobile-app/utils/types/errors/LocalAuthError.ts+++ b/apps/mobile-app/utils/types/errors/LocalAuthError.ts@@ -1,5 +1,5 @@/**- * Custom error class for API authentication-related errors.+ * A login failure detected on the device, whose message is shown to the user as-is.*/export class LocalAuthError extends Error {/**diff --git a/apps/web/src/components/email/RecentEmails.tsx b/apps/web/src/components/email/RecentEmails.tsxindex 04f931fc4..5f53f195b 100644--- a/apps/web/src/components/email/RecentEmails.tsx+++ b/apps/web/src/components/email/RecentEmails.tsx@@ -1,4 +1,4 @@-import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import EncryptionUtility from '@aliasvault/client/crypto/EncryptionUtility';import React, { useCallback, useEffect, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';@@ -30,11 +30,6 @@ const formatDate = (value: string): string => {return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;};-/**- * The structured API error code of a failed request, null for any other error.- */-const apiErrorCode = (error: unknown): string | null => error instanceof ApiRequestError ? error.apiErrorCode : null;-/*** The recent emails received on an item's email address, with an in-place email modal.*/@@ -76,9 +71,9 @@ const RecentEmails: React.FC<RecentEmailsProps> = ({ emailAddress }) => {setError('');}} catch (err) {- if (apiErrorCode(err) === 'CLAIM_DOES_NOT_MATCH_USER') {+ if (apiErrorCodeOf(err) === 'CLAIM_DOES_NOT_MATCH_USER') {setError(t('components.main.email.recentEmails.EmailAddressInUseError'));- } else if (apiErrorCode(err) === 'CLAIM_DOES_NOT_EXIST') {+ } else if (apiErrorCodeOf(err) === 'CLAIM_DOES_NOT_EXIST') {/** The server learns about a new address when the vault push that follows a save lands. A load that races* that push is retried once instead of shown as an error.diff --git a/apps/web/src/pages/auth/Login.tsx b/apps/web/src/pages/auth/Login.tsxindex 55bfaa130..226137a1a 100644--- a/apps/web/src/pages/auth/Login.tsx+++ b/apps/web/src/pages/auth/Login.tsx@@ -1,4 +1,4 @@-import { ApiAuthError } from '@aliasvault/client/api/errors/ApiAuthError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import { getErrorMessage, hasErrorCode } from '@aliasvault/client/api/errors/AppErrorCodes';import { ClientUpgradeRequiredError } from '@aliasvault/client/api/errors/ClientUpgradeRequiredError';import { ServerUpdateRequiredError } from '@aliasvault/client/api/errors/ServerUpdateRequiredError';@@ -20,6 +20,7 @@ import { useLoading } from '@/context/LoadingContext';import { useNotifications } from '@/context/NotificationContext';import { useWebApi } from '@/context/WebApiContext';import { usePageTitle } from '@/hooks/usePageTitle';+import { apiErrorMessage } from '@/utils/ApiErrors';import { StorageKeys } from '@/utils/StorageKeys';import { vaultStore } from '@/vault/VaultStore';@@ -96,9 +97,8 @@ const Login: React.FC = () => {if (err instanceof ServerUpdateRequiredError) {return [t('common.errors.serverVersionNotSupported')];}- if (err instanceof ApiAuthError) {- const key = `apiErrors.${err.message}`;- return [t(key) === key ? err.message : t(key)];+ if (apiErrorCodeOf(err)) {+ return [apiErrorMessage(err, t, t('components.auth.login.LoginErrorMessage'))];}if (hasErrorCode(err)) {return [getErrorMessage(err, t('components.auth.login.LoginErrorMessage'))];diff --git a/apps/web/src/services/RegistrationService.ts b/apps/web/src/services/RegistrationService.tsnew file mode 100644index 000000000..e364856b6--- /dev/null+++ b/apps/web/src/services/RegistrationService.ts@@ -0,0 +1,26 @@+import { SrpLoginService } from '@aliasvault/client/auth/SrpLoginService';+import { VaultKeyService } from '@aliasvault/client/auth/VaultKeyService';+import { UnlockMethodType } from '@aliasvault/models/webapi';++import { vaultStore } from '@/vault/VaultStore';++import type { WebApiService } from '@aliasvault/client/api/WebApiService';++/**+ * Registration: create the account. The first sync writes the empty vault.+ */+export const RegistrationService = {+ /**+ * Register an account.+ * @param setAuthTokens - stores the username and tokens (the app context's setter, so the UI state follows)+ * @throws ApiRequestError with the server's error code when the server refuses the registration+ */+ async register(webApi: WebApiService, username: string, password: string, setAuthTokens: (username: string, accessToken: string, refreshToken: string) => Promise<void>): Promise<void> {+ const registered = await new SrpLoginService(webApi).register(username, password);++ await setAuthTokens(registered.username, registered.token.token, registered.token.refreshToken);+ await VaultKeyService.cacheVaultKeyBlobs({ type: UnlockMethodType.Password, ...registered.keys.accountKeys, ...registered.derivationParams });+ await vaultStore.storeUnlockKeyDerivationParams(registered.derivationParams);+ await vaultStore.storeUnlockKey(registered.derivedKey);+ },+};diff --git a/apps/web/src/utils/ApiErrors.ts b/apps/web/src/utils/ApiErrors.tsindex 67bb3cde6..542eac341 100644--- a/apps/web/src/utils/ApiErrors.ts+++ b/apps/web/src/utils/ApiErrors.ts@@ -1,4 +1,4 @@-import { ApiRequestError } from '@aliasvault/client/api/errors/ApiRequestError';+import { apiErrorCodeOf } from '@aliasvault/client/api/errors/ApiRequestError';import type { TFunction } from 'i18next';@@ -9,8 +9,9 @@ import type { TFunction } from 'i18next';* @param fallback - the message when no known code is present*/export function apiErrorMessage(error: unknown, t: TFunction, fallback: string): string {- if (error instanceof ApiRequestError && error.apiErrorCode) {- const key = `apiErrors.${error.apiErrorCode}`;+ const code = apiErrorCodeOf(error);+ if (code) {+ const key = `apiErrors.${code}`;const translated = t(key);return translated === key ? fallback : translated;}diff --git a/core/client/src/api/WebApiService.ts b/core/client/src/api/WebApiService.tsindex 9b554f5cb..b3fb734c3 100644--- a/core/client/src/api/WebApiService.ts+++ b/core/client/src/api/WebApiService.ts@@ -172,7 +172,7 @@ export class WebApiService {if (retryResponse.status === 413) {throw new PayloadTooLargeError(`Request rejected with HTTP 413: payload exceeds server limit`);}- throw new ApiRequestError(retryResponse.status, await this.extractApiErrorCode(retryResponse));+ throw await ApiRequestError.fromResponse(retryResponse);}return parseJson ? retryResponse.json() : retryResponse as unknown as T;@@ -189,30 +189,12 @@ export class WebApiService {}if (!response.ok && throwOnError) {- throw new ApiRequestError(response.status, await this.extractApiErrorCode(response));+ throw await ApiRequestError.fromResponse(response);}return parseJson ? response.json() : response as unknown as T;}- /**- * Extract the structured API error code (e.g. "VAULT_NOT_UP_TO_DATE") from an error response body.- */- private async extractApiErrorCode(response: Response): Promise<string | null> {- try {- const body = await response.clone().json() as { code?: unknown; title?: unknown };- for (const value of [body.code, body.title]) {- // Server error codes are uppercase enum names- if (typeof value === 'string' && /^[A-Z0-9_]{2,64}$/.test(value)) {- return value;- }- }- } catch {- // Body is empty or not JSON (e.g. proxy error page).- }- return null;- }-/*** Fetch data from the API without authentication headers and without access token refresh retry.*/diff --git a/core/client/src/api/errors/ApiAuthError.ts b/core/client/src/api/errors/ApiAuthError.tsindex d40acb1fd..224d8a302 100644--- a/core/client/src/api/errors/ApiAuthError.ts+++ b/core/client/src/api/errors/ApiAuthError.ts@@ -1,5 +1,5 @@/**- * Custom error class for API authentication-related errors.+ * Thrown when the session is no longer valid (token refresh refused), which logs the user out.*/export class ApiAuthError extends Error {/**diff --git a/core/client/src/api/errors/ApiRequestError.ts b/core/client/src/api/errors/ApiRequestError.tsindex 53dfaf5fc..42caaa441 100644--- a/core/client/src/api/errors/ApiRequestError.ts+++ b/core/client/src/api/errors/ApiRequestError.ts@@ -21,4 +21,39 @@ export class ApiRequestError extends Error {this.apiErrorCode = apiErrorCode;Object.setPrototypeOf(this, ApiRequestError.prototype);}++ /**+ * The error for a failed response, carrying the structured API error code (e.g. "USER_NOT_FOUND") when the body names one.+ * @param response - The failed response+ */+ public static async fromResponse(response: Response): Promise<ApiRequestError> {+ return new ApiRequestError(response.status, await extractApiErrorCode(response));+ }+}++/**+ * The structured API error code of a failed request, or null for any other error.+ * @param error - The thrown error+ */+export function apiErrorCodeOf(error: unknown): string | null {+ return error instanceof ApiRequestError ? error.apiErrorCode : null;+}++/**+ * Extract the structured API error code from an error response body.+ * @param response - The failed response+ */+async function extractApiErrorCode(response: Response): Promise<string | null> {+ try {+ const body = await response.clone().json() as { code?: unknown; title?: unknown };+ for (const value of [body.code, body.title]) {+ // Server error codes are uppercase enum names.+ if (typeof value === 'string' && /^[A-Z0-9_]{2,64}$/.test(value)) {+ return value;+ }+ }+ } catch {+ // Body is empty or not JSON (e.g. proxy error page).+ }+ return null;}diff --git a/core/client/src/api/errors/ExpectedFailure.ts b/core/client/src/api/errors/ExpectedFailure.tsindex 64bc8c4be..686213554 100644--- a/core/client/src/api/errors/ExpectedFailure.ts+++ b/core/client/src/api/errors/ExpectedFailure.ts@@ -33,7 +33,8 @@ export function isExpectedFailure(error: unknown): boolean {return true;}if (error instanceof ApiRequestError) {- return error.statusCode >= 500 || EXPECTED_STATUS_CODES.has(error.statusCode);+ // A coded 400 is the server refusing on purpose (wrong password, username taken), not a malformed request.+ return error.statusCode >= 500 || EXPECTED_STATUS_CODES.has(error.statusCode) || (error.statusCode === 400 && error.apiErrorCode !== null);}return isAbort(error);}diff --git a/core/rust/src/vault_sync/engine.rs b/core/rust/src/vault_sync/engine.rsindex d211734d8..039f0ccac 100644--- a/core/rust/src/vault_sync/engine.rs+++ b/core/rust/src/vault_sync/engine.rs@@ -442,13 +442,18 @@ async fn materialized_vault_result(ctx: &mut Ctx, pulled: &PulledVault) -> SyncR}/// Store a pulled vault as the local one and only then commit its revisions as the local truth. A store the host-/// refuses (a mutation raced the pull) re-runs the sync instead, which is the returned flow.+/// refuses (a mutation raced the pull) re-runs the sync instead, which is the returned flow. A new vault that was+/// never written is stored dirty, so a push that does not land now is retried by the next sync.async fn commit_pulled_vault(ctx: &mut Ctx, pulled: &PulledVault) -> SyncResult<Option<Flow>> {- let stored = ctx.store_vault(&pulled.encrypted_vault, false, Some(ctx.mutation_sequence), Some(pulled.revision)).await?;+ let stored = ctx.store_vault(&pulled.encrypted_vault, pulled.needs_first_write, Some(ctx.mutation_sequence), Some(pulled.revision)).await?;if !stored.success {ctx.log("[VaultSync] Mutation detected during sync, re-syncing...").await;return Ok(Some(Flow::Resync { outdated: false }));}+ if pulled.needs_first_write {+ ctx.is_dirty = true;+ ctx.mutation_sequence = stored.mutation_sequence;+ }ctx.vault_changed = true;pull::commit_revisions(ctx, &pulled.manifest_revisions, &pulled.bucket_revisions).await?;Ok(None)@@ -459,6 +464,12 @@ async fn store_server_vault(ctx: &mut Ctx, pulled: &PulledVault) -> SyncResult<Fif let Some(resync) = commit_pulled_vault(ctx, pulled).await? {return Ok(resync);}+ if pulled.needs_first_write {+ ctx.log("[VaultSync] Writing the first revision of the new vault.").await;+ if push_local_changes(ctx, None, true, false).await?.is_none() {+ return Ok(Flow::Resync { outdated: true });+ }+ }Ok(Flow::Done(materialized_vault_result(ctx, pulled).await?))}diff --git a/core/rust/src/vault_sync/legacy.rs b/core/rust/src/vault_sync/legacy.rsindex d1fb49fee..a30684794 100644--- a/core/rust/src/vault_sync/legacy.rs+++ b/core/rust/src/vault_sync/legacy.rs@@ -32,7 +32,7 @@ pub(crate) async fn open_legacy_snapshot(ctx: &Ctx, snapshot: &GetResponse) -> Smanifest_revisions.insert(personal.clone(), revision);}ctx.log("[V2Pull] Legacy sqlite-blob pass-through (user not yet migrated), returning the blob as-is.").await;- Ok(PulledVault { encrypted_vault: snapshot.legacy_vault_blob.clone().unwrap_or_default(), revision, email_routing: email_routing_of(snapshot), manifest_revisions, bucket_revisions: HashMap::new() })+ Ok(PulledVault { encrypted_vault: snapshot.legacy_vault_blob.clone().unwrap_or_default(), revision, email_routing: email_routing_of(snapshot), manifest_revisions, bucket_revisions: HashMap::new(), needs_first_write: false })}/// The one-way move of a sqlite-blob account onto the manifest storage format: the local vault is rebuilt onto thediff --git a/core/rust/src/vault_sync/pull.rs b/core/rust/src/vault_sync/pull.rsindex d71f64af3..384b20222 100644--- a/core/rust/src/vault_sync/pull.rs+++ b/core/rust/src/vault_sync/pull.rs@@ -10,7 +10,8 @@ use super::types::{self, BlobDto, BlobHashesRequest, Db, EmailRoutingDto, GetResuse super::blob_keys::{self, EncryptedBlob};use super::{db, http, keys, legacy};use crate::crypto;-use crate::vault_codec::{self, DataBucket, Manifest, MaterializeInput, UnpackedPayload};+use crate::vault_codec::{self, CanonicalizeInput, CodecTableData, DataBucket, Manifest, ManifestSpec, MaterializeInput, UnpackedPayload};+use crate::vault_model::SYNCABLE_TABLE_NAMES;const BLOBS_DOWNLOAD_ENDPOINT: &str = "Vault/blobs/download";@@ -32,6 +33,8 @@ pub(crate) struct OpenedManifestSet {pub blob_map: HashMap<String, Vec<u8>>,pub manifest_names: HashMap<String, String>,pub contentless_manifest_ids: Vec<String>,+ /// The personal manifest is the empty placeholder of a new account, which this sync has to write.+ pub personal_needs_first_write: bool,pub personal_revision: i64,pub manifest_revisions: HashMap<String, i64>,pub bucket_revisions: HashMap<String, i64>,@@ -44,7 +47,7 @@ impl OpenedManifestSet {/// The opened set as a vault ready to become the local one.pub fn pulled_vault(&self, encrypted_vault: String, email_routing: EmailRoutingDto) -> PulledVault {- PulledVault { encrypted_vault, revision: self.personal_revision, email_routing, manifest_revisions: self.manifest_revisions.clone(), bucket_revisions: self.bucket_revisions.clone() }+ PulledVault { encrypted_vault, revision: self.personal_revision, email_routing, manifest_revisions: self.manifest_revisions.clone(), bucket_revisions: self.bucket_revisions.clone(), needs_first_write: self.personal_needs_first_write }}}@@ -55,6 +58,8 @@ pub(crate) struct PulledVault {pub email_routing: EmailRoutingDto,pub manifest_revisions: HashMap<String, i64>,pub bucket_revisions: HashMap<String, i64>,+ /// The vault is new and has never been written to the server; it is stored dirty and pushed by the same sync.+ pub needs_first_write: bool,}/// `GET v2/Vault`.@@ -145,8 +150,10 @@ pub(crate) async fn pull(ctx: &mut Ctx) -> SyncResult<PulledVault> {/// The revision maps travel on the result for the caller to commit once the pulled vault is stored.pub(crate) async fn open_manifests_and_record_sync_state(ctx: &mut Ctx, snapshot: &GetResponse, vek: &str) -> SyncResult<OpenedManifestSet> {let personal_dto = select_personal_manifest(snapshot).ok_or_else(|| SyncError::Snapshot("server returned no personal manifest, refusing to assemble".to_string()))?;- if personal_dto.blob.as_deref().unwrap_or("").is_empty() {- return Err(SyncError::Snapshot("server returned no manifest blob, nothing to assemble".to_string()));+ // Registration creates the personal manifest without content at revision 0; any later revision without content is damage.+ let personal_needs_first_write = personal_dto.blob.as_deref().unwrap_or("").is_empty();+ if personal_needs_first_write && personal_dto.revision != 0 {+ return Err(SyncError::Snapshot(format!("server returned no manifest blob at revision {}, nothing to assemble", personal_dto.revision)));}let mut pulled_fingerprints: HashMap<String, String> = HashMap::new();@@ -160,6 +167,14 @@ pub(crate) async fn open_manifests_and_record_sync_state(ctx: &mut Ctx, snapshotfor dto in ordered {let is_personal = dto.manifest_id == personal_dto.manifest_id;let manifest_key = resolve_manifest_vek(ctx, dto, &personal_dto.manifest_id, vek, is_personal).await?;+ if is_personal && personal_needs_first_write {+ ctx.log("[V2Pull] Personal manifest has no content yet (new account); starting from an empty vault.").await;+ let entry = empty_personal_manifest(ctx, dto, &manifest_key).await?;+ state::set(&ctx.host, state::VAULT_MANIFEST_SALT, &entry.manifest.manifest_salt).await?;+ state::set(&ctx.host, state::VAULT_PERSONAL_MANIFEST_ID, &entry.manifest_id).await?;+ resolved.push(entry);+ continue;+ }if dto.blob.as_deref().unwrap_or("").is_empty() {// A shared manifest served without content yet (created but never written); its grant and revision are still tracked.let (encrypted_vek, encryption_public_key, algorithm) = grant_of(dto).ok_or_else(|| SyncError::Snapshot(format!("shared manifest {} was served without content and without a grant, refusing to assemble", dto.manifest_id)))?;@@ -204,7 +219,8 @@ pub(crate) async fn open_manifests_and_record_sync_state(ctx: &mut Ctx, snapshotlet blob_map = download_referenced_blobs(ctx, &resolved, vek).await?;- for entry in &resolved {+ // An empty personal manifest has no server content to compare a push against, so it gets no baseline.+ for entry in resolved.iter().filter(|m| !(m.is_personal && personal_needs_first_write)) {pulled_fingerprints.insert(state::fingerprint_manifest_key(&entry.manifest_id), entry.content_fingerprint.clone());}state::set(&ctx.host, state::VAULT_CONTENT_FINGERPRINTS, &pulled_fingerprints).await?;@@ -216,6 +232,7 @@ pub(crate) async fn open_manifests_and_record_sync_state(ctx: &mut Ctx, snapshotblob_map,manifest_names,contentless_manifest_ids: contentless_revisions.keys().cloned().collect(),+ personal_needs_first_write,personal_revision,manifest_revisions,bucket_revisions,@@ -367,6 +384,17 @@ fn open_manifest(dto: &ManifestDto, vek: &str, is_personal: bool) -> SyncResult<})}+/// The empty personal manifest of a new account, canonicalized from every syncable table with no rows.+async fn empty_personal_manifest(ctx: &Ctx, dto: &ManifestDto, vek: &str) -> SyncResult<ResolvedManifest> {+ // A salt from an earlier attempt is kept, so blob hashes stay stable until the first write lands.+ let manifest_salt = state::get::<String>(&ctx.host, state::VAULT_MANIFEST_SALT).await?.unwrap_or_else(vault_codec::generate_manifest_salt);+ let tables = SYNCABLE_TABLE_NAMES.iter().map(|name| CodecTableData { name: name.to_string(), records: Vec::new() }).collect();+ let spec = ManifestSpec { manifest_id: dto.manifest_id.clone(), manifest_salt, name: None };+ let canonicalized = vault_codec::canonicalize_from_sqlite(CanonicalizeInput { tables, canonicalized_at: crate::timestamp::now_iso_utc(), manifests: vec![spec], stamp_unstamped_into: None })?;+ let manifest = canonicalized.manifests.into_iter().next().map(|m| m.manifest).ok_or_else(|| SyncError::Other("canonicalize returned no manifest for the empty vault".to_string()))?;+ Ok(ResolvedManifest { manifest_id: dto.manifest_id.clone(), is_personal: true, manifest, vek: vek.to_string(), revision: dto.revision, blob_references: Vec::new(), content_fingerprint: String::new() })+}+/// The key that opens one snapshot manifest.async fn resolve_manifest_vek(ctx: &Ctx, dto: &ManifestDto, personal_manifest_id: &str, personal_vek: &str, is_personal: bool) -> SyncResult<String> {let key_type = dto.key_type.clone().unwrap_or_else(|| if is_personal { types::KEY_TYPE_ACCOUNT_KEY.to_string() } else { types::KEY_TYPE_GRANT_KEY.to_string() });diff --git a/core/rust/src/vault_sync/tests/mod.rs b/core/rust/src/vault_sync/tests/mod.rsindex eb32d30de..46afee9a4 100644--- a/core/rust/src/vault_sync/tests/mod.rs+++ b/core/rust/src/vault_sync/tests/mod.rs@@ -123,6 +123,102 @@ fn fresh_client_pulls_and_materializes_the_server_vault() {assert!(host.requests_to("Vault").len() == 1);}+/// The status and snapshot of an account whose personal manifest the server holds without content.+fn contentless_personal_snapshot(revision: i64) -> (Value, Value) {+ let status = json!({+ "clientVersionSupported": true,+ "serverVersion": "0.31.0",+ "manifestRevisions": [{ "manifestId": PERSONAL_MANIFEST_ID, "revision": revision }],+ "bucketRevisions": [],+ "personalManifestId": PERSONAL_MANIFEST_ID,+ "srpSalt": "salt",+ "capabilities": {},+ });+ let vault = json!({+ "status": 0,+ "storageFormat": 1,+ "personalManifestId": PERSONAL_MANIFEST_ID,+ "manifests": [{ "manifestId": PERSONAL_MANIFEST_ID, "blob": null, "ciphertextHash": null, "revision": revision, "blobReferences": [], "canAdminister": false, "keyType": "accountkey" }],+ "buckets": [],+ "emailRouting": { "privateEmailDomainList": ["private.io"], "publicEmailDomainList": [], "hiddenPrivateEmailDomainList": [], "emailAddressList": [] },+ });+ (status, vault)+}++#[test]+fn new_account_starts_from_an_empty_vault_and_writes_its_first_revision() {+ let vek = crypto::generate_key_base64();+ let mut host = TestHost::new(&vek);+ host.state.insert(state::ENCRYPTED_ACCOUNT_KEY.to_string(), json!("wrapped"));+ let (status, vault) = contentless_personal_snapshot(0);+ host.respond("GET", "Status", status);+ host.respond("GET", "Vault", vault);+ host.respond("POST", "Vault/blobs/missing", json!({ "missing": [] }));+ host.respond("POST", "Vault", json!({ "status": 0, "manifestRevisions": [{ "manifestId": PERSONAL_MANIFEST_ID, "revision": 1 }], "bucketRevisions": [], "missingBlobHashes": [] }));++ let result = host.drive(&SyncSession::new(&request("fullSync", &vek, false, 0)).unwrap());++ assert_eq!(result["success"], true, "{}", result);+ assert_eq!(result["hasNewVault"], true);+ assert!(item_names(&host.local).is_empty());+ let posts: Vec<_> = host.requests_to("Vault").into_iter().filter(|r| r.method == "POST").collect();+ assert_eq!(posts.len(), 1, "the new vault is written once");+ let body = posts[0].body.as_ref().unwrap();+ assert_eq!(body["manifests"][0]["manifestId"], PERSONAL_MANIFEST_ID);+ assert_eq!(body["manifests"][0]["currentRevision"], 0);+ assert_eq!(host.state[state::SERVER_MANIFEST_REVISIONS][PERSONAL_MANIFEST_ID], 1);+ assert!(!host.is_dirty);++ // The written manifest opens with the VEK and names the personal manifest.+ let blob = body["manifests"][0]["manifestBlob"].as_str().unwrap();+ let plain = crypto::symmetric_decrypt_bytes(&crate::encoding::base64_decode(blob).unwrap(), &vek).unwrap();+ let manifest: Value = serde_json::from_str(&vault_codec::unpack_payload(&plain).unwrap()).unwrap();+ assert_eq!(manifest["manifestId"], PERSONAL_MANIFEST_ID);+ assert_eq!(manifest["manifestSalt"], host.state[state::VAULT_MANIFEST_SALT]);+}++#[test]+fn new_account_whose_first_write_fails_retries_it_on_the_next_sync() {+ let vek = crypto::generate_key_base64();+ let mut host = TestHost::new(&vek);+ host.state.insert(state::ENCRYPTED_ACCOUNT_KEY.to_string(), json!("wrapped"));+ let (status, vault) = contentless_personal_snapshot(0);+ host.respond("GET", "Status", status);+ host.respond("GET", "Vault", vault);+ host.respond("POST", "Vault/blobs/missing", json!({ "missing": [] }));++ // No response for the write: the vault stays dirty and the sync goes offline on the stored empty vault.+ let result = host.drive(&SyncSession::new(&request("fullSync", &vek, false, 0)).unwrap());+ assert_eq!(result["success"], true, "{}", result);+ assert!(host.is_dirty, "the unwritten new vault stays dirty");++ host.respond("POST", "Vault", json!({ "status": 0, "manifestRevisions": [{ "manifestId": PERSONAL_MANIFEST_ID, "revision": 1 }], "bucketRevisions": [], "missingBlobHashes": [] }));+ let mutation_sequence = host.mutation_sequence;+ let result = host.drive(&SyncSession::new(&request("fullSync", &vek, true, mutation_sequence)).unwrap());++ assert_eq!(result["success"], true, "{}", result);+ let posts: Vec<_> = host.requests_to("Vault").into_iter().filter(|r| r.method == "POST").collect();+ assert_eq!(posts.last().unwrap().body.as_ref().unwrap()["manifests"][0]["currentRevision"], 0);+ assert_eq!(host.state[state::SERVER_MANIFEST_REVISIONS][PERSONAL_MANIFEST_ID], 1);+ assert!(!host.is_dirty);+}++#[test]+fn contentless_personal_manifest_past_revision_zero_is_refused() {+ let vek = crypto::generate_key_base64();+ let mut host = TestHost::new(&vek);+ host.state.insert(state::ENCRYPTED_ACCOUNT_KEY.to_string(), json!("wrapped"));+ let (status, vault) = contentless_personal_snapshot(4);+ host.respond("GET", "Status", status);+ host.respond("GET", "Vault", vault);++ let result = host.drive(&SyncSession::new(&request("fullSync", &vek, false, 0)).unwrap());++ assert_eq!(result["success"], false, "{}", result);+ assert!(host.requests_to("Vault").iter().all(|r| r.method != "POST"), "nothing is written over a damaged manifest");+ assert!(host.store_calls.is_empty());+}+#[test]fn unknown_storage_format_is_refused_not_read_as_legacy() {let vek = crypto::generate_key_base64();diff --git a/core/rust/src/vault_sync/tests/test_host.rs b/core/rust/src/vault_sync/tests/test_host.rsindex 801aa3838..67600c9f3 100644--- a/core/rust/src/vault_sync/tests/test_host.rs+++ b/core/rust/src/vault_sync/tests/test_host.rs@@ -154,13 +154,9 @@ impl TestHost {self.rekeyed_stores_found_the_chain.push(found_the_chain);self.vault_key = key;}- if let Some(expected) = expected_mutation_seq {- if expected != self.mutation_sequence {- json!({ "success": false, "mutationSequence": self.mutation_sequence })- } else {- self.store_blob(&encrypted_blob);- json!({ "success": true, "mutationSequence": self.mutation_sequence })- }+ // Like the app hosts: a sync store is refused after a mutation, and either kind may mark the vault dirty.+ if expected_mutation_seq.is_some_and(|expected| expected != self.mutation_sequence) {+ json!({ "success": false, "mutationSequence": self.mutation_sequence })} else {if mark_dirty {self.mutation_sequence += 1;