Skip to content

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

Refactor (#2159)

Leendert de Borst6dc60272ebed
diff --git a/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs
index ff73eab03..2a1d01607 100644
--- a/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V1/VaultController.cs
@@ -417,7 +417,7 @@ public class VaultController(ILogger<VaultController> logger, IAliasServerDbCont
// Resolve the alias creation limits for the caller's personal group: v1 only files claims in the root manifest,
// which that group owns, so it is the only quota subject a v1 push can charge.
var personalGroup = await context.Groups.FirstAsync(g => g.Id == user.PersonalGroupId);
- var rateLimits = await rateLimitService.ResolveAsync(personalGroup, RateLimitType.AliasCreation);
+ var rateLimits = await rateLimitService.GetLimitsAsync(personalGroup, RateLimitType.AliasCreation);
// Calculate the current usage baseline per limit. addedThisSync is then added to each in the loop.
var limitUsages = new List<(int MaxCount, int BaseCount)>();
diff --git a/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs b/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs
index c55ac8249..c3578ce01 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/StatusController.cs
@@ -51,7 +51,7 @@ public class StatusController(IAliasServerDbContextFactory dbContextFactory, Use
await using var context = await dbContextFactory.CreateDbContextAsync();
- // Manifest revisions (own manifests plus those shared with this user) are built via the shared helper.
+ // Manifest revisions for every manifest this user can access, built via the shared helper.
var manifestRevisions = await VaultStatusHelper.GetManifestRevisionsAsync(context, user.Id);
// Latest revision per bucket kind.
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
index 134798431..b9c4e9735 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
@@ -90,13 +90,12 @@ public class VaultController(
var emailRouting = await BuildEmailRoutingAsync(context, user);
var ownedGroupIds = await GroupHelper.GetOwnedGroupIdsAsync(context, user.Id);
- // Current revision per logical manifest.
- var latestManifests = await context.VaultManifests
- .Where(x => ownedGroupIds.Contains(x.OwnerGroupId) && x.StorageFormat == ManifestFormat)
- .Select(x => new { x.ManifestId, x.IsRoot, x.Name, x.ManifestBlob, x.ManifestCiphertextHash, x.RevisionNumber })
+ // Every manifest the caller can open.
+ var latestManifests = await AccessibleManifests(context, user.Id)
+ .Select(x => new { x.ManifestId, x.IsRoot, x.Name, x.ManifestBlob, x.ManifestCiphertextHash, x.RevisionNumber, x.OwnerGroupId })
.ToListAsync();
- if (latestManifests.Count == 0)
+ if (!latestManifests.Any(m => m.IsRoot && ownedGroupIds.Contains(m.OwnerGroupId)))
{
// User hasn't migrated to manifest-v1 yet, return the latest legacy SQLite blob.
var legacy = await context.VaultManifests
diff --git a/apps/server/AliasVault.Api/Helpers/GroupHelper.cs b/apps/server/AliasVault.Api/Helpers/GroupHelper.cs
index e568832a9..b18c29221 100644
--- a/apps/server/AliasVault.Api/Helpers/GroupHelper.cs
+++ b/apps/server/AliasVault.Api/Helpers/GroupHelper.cs
@@ -26,6 +26,17 @@ public static class GroupHelper
return await context.GroupMembers.Where(m => m.UserId == userId && m.Role == GroupRole.Owner).Select(m => m.GroupId).ToListAsync();
}
+ /// <summary>
+ /// Get the ids of every group the user may administer.
+ /// </summary>
+ /// <param name="context">Database context.</param>
+ /// <param name="userId">The user.</param>
+ /// <returns>The administered group ids.</returns>
+ public static async Task<List<Guid>> GetAdministeredGroupIdsAsync(AliasServerDbContext context, string userId)
+ {
+ return await context.GroupMembers.Where(m => m.UserId == userId && (m.Role == GroupRole.Owner || m.Role == GroupRole.Admin)).Select(m => m.GroupId).ToListAsync();
+ }
+
/// <summary>
/// Create a new user's personal group in memory.
/// </summary>
@@ -113,9 +124,9 @@ public static class GroupHelper
/// both the rate-limit rules and the usage they are measured against are scoped to it.
/// </summary>
/// <param name="context">Database context.</param>
- /// <param name="manifestIds">The manifests to resolve the owning group of.</param>
+ /// <param name="manifestIds">The manifests to get the owning group of.</param>
/// <returns>Manifest id to owning group id.</returns>
- public static async Task<Dictionary<Guid, Guid>> ResolveOwnerGroupsAsync(AliasServerDbContext context, IEnumerable<Guid> manifestIds)
+ public static async Task<Dictionary<Guid, Guid>> GetOwnerGroupsAsync(AliasServerDbContext context, IEnumerable<Guid> manifestIds)
{
var ids = manifestIds.Distinct().ToList();
if (ids.Count == 0)
diff --git a/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.cs b/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.cs
new file mode 100644
index 000000000..f60393fbf
--- /dev/null
+++ b/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.cs
@@ -0,0 +1,29 @@
+//-----------------------------------------------------------------------
+// <copyright file="ManifestAccessHelper.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 AliasVault.Api.Helpers;
+
+using AliasServerDb;
+
+/// <summary>
+/// Helper for resolving which vault manifests a user can access.
+/// </summary>
+public static class ManifestAccessHelper
+{
+ /// <summary>
+ /// Every manifest the user can access.
+ /// </summary>
+ /// <param name="context">Database context.</param>
+ /// <param name="userId">The calling user.</param>
+ /// <returns>Query over the manifests the user can access, in any storage format.</returns>
+ public static IQueryable<VaultManifest> AccessibleManifests(AliasServerDbContext context, string userId)
+ {
+ return context.VaultManifests
+ .Where(m => context.GroupMembers.Any(gm => gm.GroupId == m.OwnerGroupId && gm.UserId == userId && gm.Role == GroupRole.Owner)
+ || context.VaultManifestAccessKeys.Any(k => k.UserId == userId && k.Type == ManifestKeyType.GrantKey && k.VaultManifestId == m.ManifestId));
+ }
+}
diff --git a/apps/server/AliasVault.Api/Helpers/VaultStatusHelper.cs b/apps/server/AliasVault.Api/Helpers/VaultStatusHelper.cs
index e539b97d6..b494ab2a1 100644
--- a/apps/server/AliasVault.Api/Helpers/VaultStatusHelper.cs
+++ b/apps/server/AliasVault.Api/Helpers/VaultStatusHelper.cs
@@ -16,68 +16,16 @@ using Microsoft.EntityFrameworkCore;
/// </summary>
public static class VaultStatusHelper
{
- private const string ManifestFormat = "manifest-v1";
-
/// <summary>
- /// The full manifest-revision list a status endpoint reports: the user's own manifests (all storage formats)
- /// plus every manifest shared with them.
+ /// The full manifest-revision list a status endpoint reports: every manifest the user can access.
/// </summary>
/// <param name="context">Database context.</param>
/// <param name="userId">The id of the user to build manifest revisions for.</param>
- /// <returns>The combined owned + shared-with-me manifest revision list.</returns>
+ /// <returns>The accessible manifest revision list.</returns>
public static async Task<List<ManifestRevision>> GetManifestRevisionsAsync(AliasServerDbContext context, string userId)
{
- var revisions = await GetOwnedManifestRevisionsAsync(context, userId);
- revisions.AddRange(await GetGrantedManifestRevisionsAsync(context, userId));
- return revisions;
- }
-
- /// <summary>
- /// The revision entries for every manifest owned groups that the user is owner of>.
- /// </summary>
- /// <param name="context">Database context.</param>
- /// <param name="userId">The id of the user to build owned manifest revisions for.</param>
- /// <returns>The list of owned manifest revisions.</returns>
- private static async Task<List<ManifestRevision>> GetOwnedManifestRevisionsAsync(AliasServerDbContext context, string userId)
- {
- return await context.VaultManifests
- .Where(x => context.GroupMembers.Any(m => m.GroupId == x.OwnerGroupId && m.UserId == userId && m.Role == GroupRole.Owner))
+ return await ManifestAccessHelper.AccessibleManifests(context, userId)
.Select(x => new ManifestRevision { ManifestId = x.ManifestId, IsRoot = x.IsRoot, Revision = x.RevisionNumber })
.ToListAsync();
}
-
- /// <summary>
- /// The revision entries for every manifest another user has shared with <paramref name="userId"/>.
- /// </summary>
- /// <param name="context">Database context.</param>
- /// <param name="userId">The id of the user to build granted manifest revisions for.</param>
- /// <returns>The list of shared-with-me manifest revisions (empty when the user has no grants).</returns>
- private static async Task<List<ManifestRevision>> GetGrantedManifestRevisionsAsync(AliasServerDbContext context, string userId)
- {
- var grantedManifestIds = await GetGrantedManifestIdsAsync(context, userId);
- if (grantedManifestIds.Count == 0)
- {
- return [];
- }
-
- return await context.VaultManifests
- .Where(m => grantedManifestIds.Contains(m.ManifestId) && m.StorageFormat == ManifestFormat)
- .Select(m => new ManifestRevision { ManifestId = m.ManifestId, IsRoot = false, Revision = m.RevisionNumber })
- .ToListAsync();
- }
-
- /// <summary>
- /// The ids of manifests <b>other</b> users have granted to <paramref name="userId"/>.
- /// </summary>
- /// <param name="context">Database context.</param>
- /// <param name="userId">The id of the user to resolve granted manifest ids for.</param>
- /// <returns>The list of granted manifest ids.</returns>
- private static async Task<List<Guid>> GetGrantedManifestIdsAsync(AliasServerDbContext context, string userId)
- {
- return await context.VaultManifestAccessKeys
- .Where(k => k.UserId == userId && k.Type == ManifestKeyType.GrantKey
- && context.VaultManifests.Any(m => m.ManifestId == k.VaultManifestId && !context.GroupMembers.Any(gm => gm.GroupId == m.OwnerGroupId && gm.UserId == userId && gm.Role == GroupRole.Owner)))
- .Select(k => k.VaultManifestId)
- .ToListAsync();
- }
}
diff --git a/apps/server/AliasVault.Api/Services/RateLimitService.cs b/apps/server/AliasVault.Api/Services/RateLimitService.cs
index adb3f9a64..d7b5d0a9a 100644
--- a/apps/server/AliasVault.Api/Services/RateLimitService.cs
+++ b/apps/server/AliasVault.Api/Services/RateLimitService.cs
@@ -27,12 +27,12 @@ public class RateLimitService(IAliasServerDbContextFactory dbContextFactory, IMe
private const string EnabledRulesCacheKey = "RateLimits_EnabledRules";
/// <summary>
- /// Resolves the limits that apply to the given group for the given limit type.
+ /// Gets the limits that apply to the given group for the given limit type.
/// </summary>
- /// <param name="group">The group to resolve limits for.</param>
- /// <param name="limitType">The limit type to resolve.</param>
+ /// <param name="group">The group to get limits for.</param>
+ /// <param name="limitType">The limit type to get limits for.</param>
/// <returns>The limits that must all be satisfied. Empty means no limit applies.</returns>
- public async Task<IReadOnlyList<EffectiveRateLimit>> ResolveAsync(Group group, RateLimitType limitType)
+ public async Task<IReadOnlyList<EffectiveRateLimit>> GetLimitsAsync(Group group, RateLimitType limitType)
{
var rules = await GetEnabledRulesAsync();
return RateLimitResolver.Resolve(rules, group, limitType, timeProvider.UtcNow);
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Manifest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Manifest.cs
index a049a3cef..c6c8f26d7 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Manifest.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/Manifest.cs
@@ -39,13 +39,23 @@ public class Manifest
/// <summary>Gets or sets the plaintext display name of a shared manifest. Null for the root manifest.</summary>
public string? Name { get; set; }
- /// <summary>Gets or sets the username of the manifest owner. Set only on manifests granted to the caller by another user.</summary>
+ /// <summary>
+ /// Gets or sets the username of the manifest owner.
+ /// <see cref="CanAdminister"/>).
+ /// </summary>
public string? OwnerUsername { get; set; }
/// <summary>
- /// Gets or sets the manifest VEK encrypted with the caller's public key. Set only on manifests granted to the
- /// caller by another user; the caller decrypts it with their private key. Null on manifests the caller owns
- /// (the owner keeps their own copy of the folder VEK inside their root vault).
+ /// Gets or sets a value indicating whether the caller may administer this manifest's sharing: grant and revoke
+ /// access and publish its email delivery key. True when the caller is an owner or admin of the group that owns
+ /// the manifest.
+ /// </summary>
+ public bool CanAdminister { get; set; }
+
+ /// <summary>
+ /// Gets or sets the manifest VEK encrypted with the caller's public key, which they decrypt with their private
+ /// key. Set on every non-root manifest the caller can open, whether they own it (their self-grant) or another
+ /// user granted it to them. Null on the root manifest, which is unlocked through the account key hierarchy.
/// </summary>
public string? EncryptedVek { get; set; }