Skip to content

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

Add legacy vault migration revert functionality to admin (#2159)

Leendert de Borst809ed4e88307
diff --git a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/ManifestTable.razor b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/ManifestTable.razor
index 62805af0c..465873fd0 100644
--- a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/ManifestTable.razor
+++ b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/ManifestTable.razor
@@ -17,17 +17,16 @@
{
<span class="bg-blue-100 text-blue-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded dark:bg-blue-900 dark:text-blue-300">Current</span>
}
- @if (HasPasswordChanged(entry, Revisions))
- {
- <span class="bg-green-100 text-green-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded dark:bg-green-900 dark:text-green-300">Password Changed</span>
- }
- @if (IsUnrestorableLegacyRevision(entry))
- {
- <span class="bg-gray-100 text-gray-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded dark:bg-gray-700 dark:text-gray-300" title="Predates this user's migration to the current vault storage format.">Pre-migration</span>
- }
</SortableTableColumn>
<SortableTableColumn>
- @if (!entry.IsCurrent && !IsUnrestorableLegacyRevision(entry))
+ @if (IsPreMigration(entry))
+ {
+ @if (AllowRevertToLegacy)
+ {
+ <Button Color="danger" OnClick="() => RevertToLegacyAsync(entry)">Revert to legacy</Button>
+ }
+ }
+ else if (!entry.IsCurrent)
{
<Button OnClick="() => MakeCurrentAsync(entry)">Restore</Button>
}
@@ -44,11 +43,11 @@
public List<ManifestRevisionRow> Revisions { get; set; } = [];
/// <summary>
- /// Gets or sets whether the user has moved to the manifest key model, which decides where the password signal
- /// comes from and which revisions may still be restored.
+ /// Gets or sets whether a pre-migration revision may offer the revert action. False when the user takes part in
+ /// vault sharing, which the revert would break.
/// </summary>
[Parameter]
- public bool HasManifestAccessKeys { get; set; }
+ public bool AllowRevertToLegacy { get; set; }
/// <summary>
/// Gets or sets the event callback to make a history revision the current one.
@@ -56,6 +55,12 @@
[Parameter]
public EventCallback<ManifestRevisionRow> OnMakeCurrent { get; set; }
+ /// <summary>
+ /// Gets or sets the event callback to revert the user to a pre-migration revision and the legacy storage format.
+ /// </summary>
+ [Parameter]
+ public EventCallback<ManifestRevisionRow> OnRevertToLegacy { get; set; }
+
private string SortColumn { get; set; } = "RevisionNumber";
private SortDirection SortDirection { get; set; } = SortDirection.Descending;
@@ -120,32 +125,21 @@
}
/// <summary>
- /// Whether restoring this revision would put the user back on the legacy storage format which is not supported anymore.
+ /// Whether this revision predates the migration of a manifest that is now on the current storage format. Making
+ /// such a revision current is not a restore but a revert, which takes the whole account back to the legacy format.
/// </summary>
- private bool IsUnrestorableLegacyRevision(ManifestRevisionRow entry)
+ private bool IsPreMigration(ManifestRevisionRow entry)
{
- return HasManifestAccessKeys && entry.IsLegacy;
+ return !entry.IsCurrent && entry.IsLegacy && Revisions.Any(x => x.ManifestId == entry.ManifestId && x.IsCurrent && !x.IsLegacy);
}
- /// <summary>
- /// Checks if the password has changed between this revision and the previous one of the same manifest. Only
- /// legacy revisions carry credentials, so this stays silent on the current key model where the header line above
- /// reports the password change instead.
- /// </summary>
- private static bool HasPasswordChanged(ManifestRevisionRow current, IEnumerable<ManifestRevisionRow> revisions)
+ private async Task MakeCurrentAsync(ManifestRevisionRow entry)
{
- var previousEntry = revisions.FirstOrDefault(v => v.ManifestId == current.ManifestId && v.RevisionNumber == current.RevisionNumber - 1);
- if (previousEntry == null)
- {
- // Nothing to compare to, so assume it has not changed.
- return false;
- }
-
- return current.Salt != previousEntry.Salt || current.Verifier != previousEntry.Verifier;
+ await OnMakeCurrent.InvokeAsync(entry);
}
- private async Task MakeCurrentAsync(ManifestRevisionRow entry)
+ private async Task RevertToLegacyAsync(ManifestRevisionRow entry)
{
- await OnMakeCurrent.InvokeAsync(entry);
+ await OnRevertToLegacy.InvokeAsync(entry);
}
}
diff --git a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/PasswordHistoryTable.razor b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/PasswordHistoryTable.razor
index 60702366e..024082797 100644
--- a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/PasswordHistoryTable.razor
+++ b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Components/PasswordHistoryTable.razor
@@ -1,3 +1,4 @@
+@using AliasServerDb.Retention
@using AliasVault.Shared.Models.Enums
@using AliasVault.RazorComponents.Tables
diff --git a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor
index acc359fdc..393bdcadb 100644
--- a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor
+++ b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor
@@ -1,8 +1,11 @@
@page "/users/{Id}"
+@using AliasServerDb.Legacy
+@using AliasServerDb.Retention
@using AliasVault.Admin.Main.Pages.Users.View.Components
@using AliasVault.Admin.Main.Models
@using AliasVault.Admin.Services
@using AliasVault.Auth
+@using AliasVault.Shared.Models.Enums
@using AliasVault.Shared.Server.Services
@using Microsoft.EntityFrameworkCore
@inherits MainBase
@@ -240,7 +243,7 @@ else
</div>
<h4 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white">Manifest revisions (@StorageOverview.ManifestRevisions.Count)</h4>
- <ManifestTable Revisions="@StorageOverview.ManifestRevisions" HasManifestAccessKeys="@HasManifestAccessKeys" OnMakeCurrent="@MakeCurrentAsync" />
+ <ManifestTable Revisions="@StorageOverview.ManifestRevisions" AllowRevertToLegacy="@(!TakesPartInSharing)" OnMakeCurrent="@MakeCurrentAsync" OnRevertToLegacy="@RevertToLegacyAsync" />
<h4 class="mt-6 mb-2 text-lg font-semibold text-gray-900 dark:text-white">Data bucket revisions</h4>
<DataBucketTable Revisions="@StorageOverview.BucketRevisions" OnRestore="@RestoreBucketAsync" />
@@ -316,6 +319,7 @@ else
private List<UserUnlockKeysHistory> PasswordHistoryList { get; set; } = [];
private int UnlockKeyHistoryRetentionDays { get; set; }
private bool HasManifestAccessKeys { get; set; }
+ private bool TakesPartInSharing { get; set; }
private List<AuthLog> AuthLogList { get; set; } = [];
private UserUsageStatistics? UserUsageStats { get; set; }
private bool IsEditingEmailLimits { get; set; }
@@ -398,6 +402,9 @@ else
// Whether the user is on the key model where the vault manifest no longer carries the login credentials.
HasManifestAccessKeys = await dbContext.VaultManifestAccessKeys.AnyAsync(x => x.UserId == User.Id);
+ // Whether this user can still be taken back to the legacy storage format, the escape hatch for a bad migration.
+ TakesPartInSharing = await TakesPartInSharingAsync(dbContext, User.Id);
+
// Load the archived master password credentials that are still inside the retention window.
UnlockKeyHistoryRetentionDays = (await SettingsService.GetAllSettingsAsync()).UnlockKeyHistoryRetentionDays;
PasswordHistoryList = await UnlockKeyHistoryPolicy
@@ -516,13 +523,6 @@ else
await using var dbContext = await DbContextFactory.CreateDbContextAsync();
- // A pre-migration revision is not allowed to be restored as this would conflict with the current vault storage format.
- if (HasManifestAccessKeys && revision.IsLegacy)
- {
- GlobalNotificationService.AddErrorMessage("This revision predates the user's migration to the current vault storage format and cannot be restored. Restoring it would leave the account unable to open its vault.", true);
- return;
- }
-
var passwordNote = HasManifestAccessKeys
? "• The user's master password is not affected; use \"Previous master passwords\" to revert a password change."
: "• If the user has changed their password recently, their password will be reverted to the one associated with this vault.";
@@ -545,6 +545,14 @@ Do you want to proceed with the restoration?")) {
return;
}
+ // Crossing storage formats leaves the account with a vault its keys cannot open, so it only ever happens
+ // through the deliberate revert action below.
+ if (currentVault.StorageFormat != revisionToRestore.StorageFormat)
+ {
+ GlobalNotificationService.AddErrorMessage("This revision is in a different vault storage format than the one the account is on and cannot be restored. Restoring it would leave the account unable to open its vault.", true);
+ return;
+ }
+
// Archive the current revision into history first, then copy the restored payload onto the current row
// with a new revision number one above the previous current.
var newRevisionNumber = currentVault.RevisionNumber + 1;
@@ -571,6 +579,59 @@ Do you want to proceed with the restoration?")) {
}
}
+ /// <summary>
+ /// Whether the user holds any state that only exists on the current storage format because of vault sharing: a
+ /// grant on a shared manifest, a membership of a shared group, or an invitation sealed to their account keypair.
+ /// Reverting such a user would delete the keypair all of that is encrypted to, so the revert is not offered.
+ /// </summary>
+ /// <param name="dbContext">The database context to query.</param>
+ /// <param name="userId">The user to check.</param>
+ /// <returns>True when the user takes part in vault sharing.</returns>
+ private static async Task<bool> TakesPartInSharingAsync(AliasServerDbContext dbContext, string userId)
+ {
+ return await dbContext.VaultManifestAccessKeys.AnyAsync(x => x.UserId == userId && x.Type == ManifestKeyType.GrantKey)
+ || await dbContext.GroupMembers.AnyAsync(x => x.UserId == userId && x.Group.Type != GroupType.Personal)
+ || await dbContext.GroupInvitations.AnyAsync(x => x.InviteeUserId == userId);
+ }
+
+ /// <summary>
+ /// Handles the action of putting the user back on the legacy storage format by making a pre-migration revision
+ /// current again. This is the escape hatch for a migration that went wrong: it tears down everything the
+ /// manifest format added so the account can be opened by a v1 client and migrate a second time.
+ /// </summary>
+ /// <param name="revision">The pre-migration revision to revert to.</param>
+ private async Task RevertToLegacyAsync(ManifestRevisionRow revision)
+ {
+ await using var dbContext = await DbContextFactory.CreateDbContextAsync();
+
+ // Re-check against the database rather than trusting the state the page was rendered with.
+ if (await TakesPartInSharingAsync(dbContext, User!.Id))
+ {
+ GlobalNotificationService.AddErrorMessage("This user can no longer be reverted to the legacy vault storage format because it takes part in vault sharing.", true);
+ await RefreshData();
+ return;
+ }
+
+ if (!await ConfirmModalService.ShowConfirmation(
+ title: "Confirm Revert To Legacy Format",
+ message: $@"Are you sure you want to revert this user to the legacy sqlite-blob vault storage format, using revision {revision.RevisionNumber}?
+
+This is meant as a recovery step for a migration that went wrong. All migrated data and any updates done since the migration will be lost. It cannot be undone.
+
+Do you want to proceed with the revert?"))
+ {
+ return;
+ }
+
+ var newRevisionNumber = await LegacyVaultRollback.RevertAsync(DbContextFactory, User.Id, revision.ManifestId, revision.RevisionNumber);
+
+ Logger.LogWarning("Reverted user {UserName} ({UserId}) to the legacy vault storage format using revision {RevisionNumber}, now current at revision {NewRevisionNumber}. Account keys removed and all sessions revoked.",
+ User.UserName, User.Id, revision.RevisionNumber, newRevisionNumber);
+ GlobalNotificationService.AddSuccessMessage("User reverted to the legacy vault storage format. They can log in again with the master password that belonged to this vault revision.", true);
+
+ await RefreshData();
+ }
+
/// <summary>
/// Handles the action of restoring a data bucket history revision as the current one.
/// </summary>
diff --git a/apps/server/AliasVault.Admin/Services/VaultStorageQueries.cs b/apps/server/AliasVault.Admin/Services/VaultStorageQueries.cs
index 577055ac2..333088efd 100644
--- a/apps/server/AliasVault.Admin/Services/VaultStorageQueries.cs
+++ b/apps/server/AliasVault.Admin/Services/VaultStorageQueries.cs
@@ -8,6 +8,7 @@
namespace AliasVault.Admin.Services;
using AliasServerDb;
+using AliasServerDb.Retention;
using AliasVault.Admin.Main.Models;
using Microsoft.EntityFrameworkCore;
diff --git a/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs b/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs
index 80f4b798f..52c3bb368 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/AuthController.cs
@@ -13,6 +13,7 @@ using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using AliasServerDb;
+using AliasServerDb.Retention;
using AliasVault.Api.Headers;
using AliasVault.Api.Helpers;
using AliasVault.Auth;
diff --git a/apps/server/Databases/AliasServerDb/Legacy/LegacyVaultRollback.cs b/apps/server/Databases/AliasServerDb/Legacy/LegacyVaultRollback.cs
new file mode 100644
index 000000000..7086f70f0
--- /dev/null
+++ b/apps/server/Databases/AliasServerDb/Legacy/LegacyVaultRollback.cs
@@ -0,0 +1,78 @@
+//-----------------------------------------------------------------------
+// <copyright file="LegacyVaultRollback.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+namespace AliasServerDb.Legacy;
+
+using Microsoft.EntityFrameworkCore;
+
+/// <summary>
+/// Puts a migrated account back on the legacy sqlite-blob storage format.
+/// TODO: remove together with the rest of the legacy storage format once every account has migrated.
+/// </summary>
+public static class LegacyVaultRollback
+{
+ /// <summary>
+ /// Puts the account back on the legacy storage format by making one of its legacy revisions current again and
+ /// removing everything that only exists on the manifest format. Runs as a single transaction.
+ /// </summary>
+ /// <param name="dbContextFactory">DbContext factory, so the work can be retried as one unit.</param>
+ /// <param name="userId">The account being reverted.</param>
+ /// <param name="manifestId">The account's personal manifest.</param>
+ /// <param name="legacyRevisionNumber">The legacy revision to make current.</param>
+ /// <returns>The revision number the restored payload is now current at.</returns>
+ public static async Task<long> RevertAsync(IAliasServerDbContextFactory dbContextFactory, string userId, Guid manifestId, long legacyRevisionNumber)
+ {
+ await using var probeContext = await dbContextFactory.CreateDbContextAsync();
+ var strategy = probeContext.Database.CreateExecutionStrategy();
+
+ return await strategy.ExecuteAsync<long>(async () =>
+ {
+ await using var context = await dbContextFactory.CreateDbContextAsync();
+ await using var transaction = await context.Database.BeginTransactionAsync();
+
+ var manifest = await context.VaultManifests.FirstAsync(x => x.ManifestId == manifestId);
+ var legacyRevision = await context.VaultManifestsHistory
+ .FirstAsync(x => x.ManifestId == manifestId && x.RevisionNumber == legacyRevisionNumber && x.StorageFormat == VaultManifestBase.LegacyStorageFormat);
+
+ // The restored payload becomes current one revision above the previous current, so every client still
+ // sees the manifest move forward and pulls it, as it does after any other admin restore.
+ var newRevisionNumber = manifest.RevisionNumber + 1;
+ manifest.CopyPayloadFrom(legacyRevision);
+ manifest.RevisionNumber = newRevisionNumber;
+ context.VaultManifestsHistory.Remove(legacyRevision);
+ await context.SaveChangesAsync();
+
+ // Every manifest-v1 revision is sealed with the VEK removed below, so none of them can ever be opened
+ // again. They are dropped rather than left as restorable rows that would brick the account.
+ await context.VaultManifestsHistory.Where(x => x.ManifestId == manifestId && x.StorageFormat == VaultManifestBase.ManifestStorageFormat).ExecuteDeleteAsync();
+
+ // Blob objects only exist on the manifest format; the legacy format carries its attachments inside the
+ // vault blob itself. The stored bytes go with the references rather than waiting for the sweeper.
+ await context.VaultBlobReferences.Where(x => x.ManifestId == manifestId).ExecuteDeleteAsync();
+ await context.VaultBlobObjects.Where(x => x.OwnerUserId == userId).ExecuteDeleteAsync();
+
+ // Data buckets only exist on the manifest format; their superseded revisions cascade with them. Leaving
+ // them behind would also block the next migration, whose first push writes each bucket from revision 0.
+ await context.VaultDataBuckets.Where(x => x.ManifestId == manifestId).ExecuteDeleteAsync();
+
+ /*
+ * The account key hierarchy. Without it the v1 endpoints serve this account again, the SRP credentials
+ * come from the restored manifest revision, and the client falls back to the password-derived key,
+ * which is what the restored blob is encrypted with.
+ */
+ await context.VaultManifestAccessKeys.Where(x => x.UserId == userId).ExecuteDeleteAsync();
+ await context.UserGrantKeys.Where(x => x.UserId == userId).ExecuteDeleteAsync();
+ await context.UserUnlockKeysHistory.Where(x => x.UserId == userId).ExecuteDeleteAsync();
+ await context.UserUnlockKeys.Where(x => x.UserId == userId).ExecuteDeleteAsync();
+
+ // Both the login credentials and the session key change, so every device has to authenticate again.
+ await context.AliasVaultUserRefreshTokens.Where(x => x.UserId == userId).ExecuteDeleteAsync();
+
+ await transaction.CommitAsync();
+ return newRevisionNumber;
+ });
+ }
+}
diff --git a/apps/server/Databases/AliasServerDb/UnlockKeyHistoryPolicy.cs b/apps/server/Databases/AliasServerDb/Retention/UnlockKeyHistoryPolicy.cs
similarity index 98%
rename from apps/server/Databases/AliasServerDb/UnlockKeyHistoryPolicy.cs
rename to apps/server/Databases/AliasServerDb/Retention/UnlockKeyHistoryPolicy.cs
index fef75759f..72ca4718d 100644
--- a/apps/server/Databases/AliasServerDb/UnlockKeyHistoryPolicy.cs
+++ b/apps/server/Databases/AliasServerDb/Retention/UnlockKeyHistoryPolicy.cs
@@ -4,7 +4,7 @@
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasServerDb;
+namespace AliasServerDb.Retention;
using Microsoft.EntityFrameworkCore;
diff --git a/apps/server/Databases/AliasServerDb/VaultBlobRetentionPolicy.cs b/apps/server/Databases/AliasServerDb/Retention/VaultBlobRetentionPolicy.cs
similarity index 99%
rename from apps/server/Databases/AliasServerDb/VaultBlobRetentionPolicy.cs
rename to apps/server/Databases/AliasServerDb/Retention/VaultBlobRetentionPolicy.cs
index fcbcea3a0..4ea1b3a59 100644
--- a/apps/server/Databases/AliasServerDb/VaultBlobRetentionPolicy.cs
+++ b/apps/server/Databases/AliasServerDb/Retention/VaultBlobRetentionPolicy.cs
@@ -4,7 +4,7 @@
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasServerDb;
+namespace AliasServerDb.Retention;
using Microsoft.EntityFrameworkCore;
diff --git a/apps/server/Services/AliasVault.TaskRunner/Tasks/UnlockKeyHistoryCleanupTask.cs b/apps/server/Services/AliasVault.TaskRunner/Tasks/UnlockKeyHistoryCleanupTask.cs
index 59cb3fffb..cc8e29052 100644
--- a/apps/server/Services/AliasVault.TaskRunner/Tasks/UnlockKeyHistoryCleanupTask.cs
+++ b/apps/server/Services/AliasVault.TaskRunner/Tasks/UnlockKeyHistoryCleanupTask.cs
@@ -8,6 +8,7 @@
namespace AliasVault.TaskRunner.Tasks;
using AliasServerDb;
+using AliasServerDb.Retention;
using AliasVault.Shared.Server.Services;
/// <summary>
diff --git a/apps/server/Services/AliasVault.TaskRunner/Tasks/VaultBlobCleanupTask.cs b/apps/server/Services/AliasVault.TaskRunner/Tasks/VaultBlobCleanupTask.cs
index 00eb0a480..2fcbd95ab 100644
--- a/apps/server/Services/AliasVault.TaskRunner/Tasks/VaultBlobCleanupTask.cs
+++ b/apps/server/Services/AliasVault.TaskRunner/Tasks/VaultBlobCleanupTask.cs
@@ -8,6 +8,7 @@
namespace AliasVault.TaskRunner.Tasks;
using AliasServerDb;
+using AliasServerDb.Retention;
using AliasVault.Shared.Server.Services;
/// <summary>
diff --git a/apps/server/Tests/AliasVault.UnitTests/Vault/UnlockKeyHistoryPolicyTests.cs b/apps/server/Tests/AliasVault.UnitTests/Vault/UnlockKeyHistoryPolicyTests.cs
index 90fd97e3c..9c644978f 100644
--- a/apps/server/Tests/AliasVault.UnitTests/Vault/UnlockKeyHistoryPolicyTests.cs
+++ b/apps/server/Tests/AliasVault.UnitTests/Vault/UnlockKeyHistoryPolicyTests.cs
@@ -8,6 +8,7 @@
namespace AliasVault.UnitTests.Vault;
using AliasServerDb;
+using AliasServerDb.Retention;
using AliasVault.Shared.Models.Enums;
/// <summary>
diff --git a/apps/server/Tests/AliasVault.UnitTests/Vault/VaultBlobRetentionPolicyTests.cs b/apps/server/Tests/AliasVault.UnitTests/Vault/VaultBlobRetentionPolicyTests.cs
index c0c3fa0fb..9164f87d6 100644
--- a/apps/server/Tests/AliasVault.UnitTests/Vault/VaultBlobRetentionPolicyTests.cs
+++ b/apps/server/Tests/AliasVault.UnitTests/Vault/VaultBlobRetentionPolicyTests.cs
@@ -8,6 +8,7 @@
namespace AliasVault.UnitTests.Vault;
using AliasServerDb;
+using AliasServerDb.Retention;
/// <summary>
/// Tests for the rules that decide which encrypted vault blobs the garbage collector may delete.