Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor manifest grant access checks to use central helper (#2159)
Leendert de Borst3ecfb25bfe53
diff --git a/apps/server/AliasVault.Api/Controllers/V2/GroupsController.cs b/apps/server/AliasVault.Api/Controllers/V2/GroupsController.csindex fd614b912..497cac563 100644--- a/apps/server/AliasVault.Api/Controllers/V2/GroupsController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/GroupsController.cs@@ -252,7 +252,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use* admin of the group no longer implies access to each one of them, and an admin who was left out of a manifest* cannot pass on what they cannot open.*/- if (!await context.VaultManifestAccessKeys.AnyAsync(k => k.VaultManifestId == manifestId && k.UserId == me.Id && k.Type == ManifestKeyType.GrantKey))+ if (!await ManifestAccessHelper.HoldsGrantAsync(context, me.Id, manifestId)){return NotFound(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.SHARED_MANIFEST_NOT_FOUND, 404));}@@ -275,7 +275,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Usereturn NotFound(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.RECIPIENT_KEY_NOT_FOUND, 404));}- if (await context.VaultManifestAccessKeys.AnyAsync(k => k.VaultManifestId == manifestId && k.UserId == model.UserId && k.Type == ManifestKeyType.GrantKey))+ if (await ManifestAccessHelper.HoldsGrantAsync(context, model.UserId, manifestId)){return BadRequest(ApiErrorCodeHelper.CreateValidationErrorResponse(ApiErrorCode.ACCESS_ALREADY_GRANTED, 400));}@@ -761,7 +761,7 @@ public class GroupsController(IAliasServerDbContextFactory dbContextFactory, Use}var manifestId = invitation.VaultManifestId.Value;- if (await context.VaultManifestAccessKeys.AnyAsync(k => k.VaultManifestId == manifestId && k.UserId == userId && k.Type == ManifestKeyType.GrantKey))+ if (await ManifestAccessHelper.HoldsGrantAsync(context, userId, manifestId)){return true;}diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.csindex 3321e90d0..4f3eaabc4 100644--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs@@ -728,10 +728,11 @@ public class VaultController(return [];}+ var grants = ManifestAccessHelper.Grants(context, userId);var administered = await GroupHelper.SharedManifests(context).Where(m => ids.Contains(m.ManifestId)&& context.GroupMembers.Any(gm => gm.GroupId == m.OwnerGroupId && gm.UserId == userId && (gm.Role == GroupRole.Admin || gm.Role == GroupRole.Owner))- && context.VaultManifestAccessKeys.Any(k => k.VaultManifestId == m.ManifestId && k.UserId == userId && k.Type == ManifestKeyType.GrantKey))+ && grants.Any(k => k.VaultManifestId == m.ManifestId)).Select(m => m.ManifestId).ToListAsync();diff --git a/apps/server/AliasVault.Api/Helpers/EmailAccessHelper.cs b/apps/server/AliasVault.Api/Helpers/EmailAccessHelper.csindex d2484a5ad..fec6d8672 100644--- a/apps/server/AliasVault.Api/Helpers/EmailAccessHelper.cs+++ b/apps/server/AliasVault.Api/Helpers/EmailAccessHelper.cs@@ -22,27 +22,11 @@ public static class EmailAccessHelper/// <param name="context">Database context.</param>/// <param name="claim">The email claim to check access for.</param>/// <param name="userId">The user requesting access.</param>- /// <returns>True when the user holds an access key on the alias's manifest.</returns>+ /// <returns>True when the alias is linked to a manifest the user can access.</returns>public static async Task<bool> CanReadClaimAsync(AliasServerDbContext context, EmailClaim claim, string userId){- // Only manifests that carry the alias grant access.- var linkedManifestIds = await context.EmailClaimLinks.Where(l => l.EmailClaimId == claim.Id && l.State != EmailClaimLinkState.Removed).Select(l => l.VaultManifestId).ToListAsync();- if (linkedManifestIds.Count == 0)- {- return false;- }-- // Holding any access key on a linked manifest is proof of access.- var hasAccessKey = await context.VaultManifestAccessKeys.AnyAsync(k => k.UserId == userId && linkedManifestIds.Contains(k.VaultManifestId));- if (hasAccessKey)- {- return true;- }-- // TODO: legacy fallback: pre-KEK/VEK accounts have no AccountKey row on their personal manifest yet, so the manifest being linked- // to their personal group is their only proof of access. Personal groups only: a shared manifest is grants-only, so a group owner- // whose grant was revoked must not read its mail. Delete once all clients have migrated.- return await context.VaultManifests.AnyAsync(m => linkedManifestIds.Contains(m.ManifestId) && context.AliasVaultUsers.Any(u => u.Id == userId && u.PersonalGroupId == m.OwnerGroupId));+ var accessible = await AccessibleManifestsAsync(context, userId);+ return await context.EmailClaimLinks.AnyAsync(l => l.EmailClaimId == claim.Id && l.State != EmailClaimLinkState.Removed && accessible.Any(m => m.ManifestId == l.VaultManifestId));}/// <summary>@@ -59,27 +43,12 @@ public static class EmailAccessHelperreturn [];}- // Get the claim links that the user may read through.- var claims = await context.EmailClaimLinks.Where(l => addresses.Contains(l.EmailClaim.Address) && l.State != EmailClaimLinkState.Removed).Select(l => new { l.EmailClaim.Address, ManifestId = l.VaultManifestId, l.VaultManifest.OwnerGroupId }).ToListAsync();- if (claims.Count == 0)- {- return [];- }-- // Holding any access key on the manifest is proof of access: AccountKey on a personal manifest, GrantKey on a shared manifest- // (owner self-grant and recipient alike).- var manifestIds = claims.Select(c => c.ManifestId).Distinct().ToList();- var keyedManifestIds = (await context.VaultManifestAccessKeys- .Where(k => k.UserId == userId && manifestIds.Contains(k.VaultManifestId))- .Select(k => k.VaultManifestId)- .ToListAsync()).ToHashSet();-- // Legacy fallback: pre-KEK/VEK accounts have no AccountKey row on their personal manifest yet, so the manifest being filed under- // their personal group is their only proof of access. Personal group only: a shared manifest is grants-only, so a group owner whose- // grant was revoked must not read its mail. TODO: delete once all clients have migrated.- var personalGroupId = await GroupHelper.GetPersonalGroupIdAsync(context, userId);-- return claims.Where(c => keyedManifestIds.Contains(c.ManifestId) || c.OwnerGroupId == personalGroupId).Select(c => c.Address).Distinct().ToList();+ var accessible = await AccessibleManifestsAsync(context, userId);+ return await context.EmailClaimLinks+ .Where(l => addresses.Contains(l.EmailClaim.Address) && l.State != EmailClaimLinkState.Removed && accessible.Any(m => m.ManifestId == l.VaultManifestId))+ .Select(l => l.EmailClaim.Address)+ .Distinct()+ .ToListAsync();}/// <summary>@@ -91,30 +60,16 @@ public static class EmailAccessHelper/// <returns>The ids of the encryption keys the user can decrypt with.</returns>public static async Task<List<Guid>> ResolveDecryptableKeyIdsAsync(AliasServerDbContext context, string userId){- // Get the ids of the user's personal keys.- var personalKeyIds = await context.VaultManifestDeliveryKeys- .Where(k => context.VaultManifests.Any(m => m.ManifestId == k.VaultManifestId && context.AliasVaultUsers.Any(u => u.Id == userId && u.PersonalGroupId == m.OwnerGroupId)))- .Select(k => k.Id)- .ToListAsync();-- // Get the ids of the encryption keys the user can decrypt with.- var accessibleManifestIds = await context.VaultManifestAccessKeys- .Where(k => k.UserId == userId && k.Type == ManifestKeyType.GrantKey)- .Select(k => k.VaultManifestId)- .Distinct()- .ToListAsync();-- if (accessibleManifestIds.Count == 0)- {- return personalKeyIds;- }-- // Get the ids of the encryption keys the user can decrypt with.- var folderKeyIds = await context.VaultManifestDeliveryKeys- .Where(k => accessibleManifestIds.Contains(k.VaultManifestId))- .Select(k => k.Id)- .ToListAsync();+ var accessible = await AccessibleManifestsAsync(context, userId);+ return await context.VaultManifestDeliveryKeys.Where(k => accessible.Any(m => m.ManifestId == k.VaultManifestId)).Select(k => k.Id).ToListAsync();+ }- return [.. personalKeyIds, .. folderKeyIds];+ /// <summary>+ /// Every manifest the user can access, per <see cref="ManifestAccessHelper.AccessibleManifests"/>.+ /// </summary>+ private static async Task<IQueryable<VaultManifest>> AccessibleManifestsAsync(AliasServerDbContext context, string userId)+ {+ var scope = await ManifestAccessHelper.ResolveScopeAsync(context, userId);+ return ManifestAccessHelper.AccessibleManifests(context, scope);}}diff --git a/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.cs b/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.csindex 4a147da76..e4452edcc 100644--- a/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.cs+++ b/apps/server/AliasVault.Api/Helpers/ManifestAccessHelper.cs@@ -27,11 +27,7 @@ public static class ManifestAccessHelperpublic static async Task<ManifestAccessScope> ResolveScopeAsync(AliasServerDbContext context, string userId, Guid? personalGroupId = null){var groupId = personalGroupId ?? await context.AliasVaultUsers.Where(u => u.Id == userId).Select(u => u.PersonalGroupId).FirstOrDefaultAsync();- var grantedManifestIds = await context.VaultManifestAccessKeys- .Where(k => k.UserId == userId && k.Type == ManifestKeyType.GrantKey)- .Select(k => k.VaultManifestId)- .Distinct()- .ToListAsync();+ var grantedManifestIds = await Grants(context, userId).Select(k => k.VaultManifestId).Distinct().ToListAsync();return new ManifestAccessScope(groupId, grantedManifestIds);}@@ -49,4 +45,27 @@ public static class ManifestAccessHelperreturn context.VaultManifests.Where(m => m.OwnerGroupId == personalGroupId || grantedManifestIds.Contains(m.ManifestId));}++ /// <summary>+ /// The grant keys the user holds.+ /// </summary>+ /// <param name="context">Database context.</param>+ /// <param name="userId">The user.</param>+ /// <returns>Query over the user's grant keys.</returns>+ public static IQueryable<VaultManifestAccessKey> Grants(AliasServerDbContext context, string userId)+ {+ return context.VaultManifestAccessKeys.Where(k => k.UserId == userId && k.Type == ManifestKeyType.GrantKey);+ }++ /// <summary>+ /// Whether the user holds a grant on the manifest.+ /// </summary>+ /// <param name="context">Database context.</param>+ /// <param name="userId">The user.</param>+ /// <param name="manifestId">The manifest.</param>+ /// <returns>True when the user holds a grant key on the manifest.</returns>+ public static Task<bool> HoldsGrantAsync(AliasServerDbContext context, string userId, Guid manifestId)+ {+ return Grants(context, userId).AnyAsync(k => k.VaultManifestId == manifestId);+ }}