Skip to content

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

Updated claimed email alias routing (#2159)

Leendert de Borstf2fd9ad9c3ce
diff --git a/apps/browser-extension/src/utils/EmailRouting.ts b/apps/browser-extension/src/utils/EmailRouting.ts
index 0baffe853..463c76b32 100644
--- a/apps/browser-extension/src/utils/EmailRouting.ts
+++ b/apps/browser-extension/src/utils/EmailRouting.ts
@@ -7,22 +7,22 @@ import type { EmailRoutingPush } from '@/utils/types/EmailRoutingPush';
*
* Which manifest an alias belongs to decides how its mail is encrypted. Every row has already been
* routed into the manifest that owns it by the codec, so an address found in a shared manifest is a
- * shared alias and everything left in the root manifest is personal.
+ * shared alias and everything in the user's own manifest is personal.
*
- * @param rootManifest - the canonicalized root manifest
- * @param sharedManifests - the shared manifests produced by the same canonicalize run
+ * @param manifests - every manifest one canonicalize run produced, the user's own included
+ * @param rootManifestId - the id of the user's own manifest; its addresses stay personal
* @param privateEmailDomains - domains the server hosts mail for; addresses outside them are not claimed
- * @returns The addresses to claim, split into personal and shared-manifest ones
+ * @returns The addresses to claim, each against the manifest that owns it
*/
-export function buildEmailRouting(rootManifest: CodecManifest, sharedManifests: CodecManifest[], privateEmailDomains: string[]): EmailRoutingPush {
- const manifestIdByAddress = new Map<string, string | null>();
+export function buildEmailRouting(manifests: CodecManifest[], rootManifestId: string, privateEmailDomains: string[]): EmailRoutingPush {
+ const manifestIdByAddress = new Map<string, string>();
/**
* Record every claimable address one manifest carries against that manifest.
* @param manifest - the manifest to read the items and their login-email fields from
- * @param manifestId - the manifest's id, or null for the root manifest (its addresses stay personal)
+ * @param manifestId - the manifest's id
*/
- const collect = (manifest: CodecManifest, manifestId: string | null): void => {
+ const collect = (manifest: CodecManifest, manifestId: string): void => {
const liveItemIds = new Set(
rowsOf(manifest, 'Items').filter(row => !row.IsDeleted && row.DeletedAt == null).map(row => String(row.Id))
);
@@ -41,27 +41,29 @@ export function buildEmailRouting(rootManifest: CodecManifest, sharedManifests:
continue;
}
- if (manifestId !== null || !manifestIdByAddress.has(address)) {
+ // A shared manifest always wins the address; the root manifest only claims one nothing else has.
+ if (manifestId !== rootManifestId || !manifestIdByAddress.has(address)) {
manifestIdByAddress.set(address, manifestId);
}
}
};
- collect(rootManifest, null);
- for (const manifest of sharedManifests) {
- collect(manifest, manifest.manifestId);
- }
+ /**
+ * Whether a manifest is the user's own.
+ * @param manifest - the manifest to test
+ * @returns True when it is the manifest this vault is written from
+ */
+ const isOwn = (manifest: CodecManifest): boolean => manifest.manifestId === rootManifestId;
- const routing: EmailRoutingPush = { emailAddressList: [], sharedEmailAddressList: [] };
- for (const [address, manifestId] of manifestIdByAddress) {
- if (manifestId) {
- routing.sharedEmailAddressList.push({ address, manifestId });
- } else {
- routing.emailAddressList.push(address);
- }
+ /*
+ * The user's own manifest first: an address it also carries stays personal only while no shared manifest
+ * claims it, and `collect` lets a later shared claim override that (but never the reverse).
+ */
+ for (const manifest of [...manifests].sort((a, b) => Number(isOwn(b)) - Number(isOwn(a)))) {
+ collect(manifest, manifest.manifestId);
}
- return routing;
+ return { emailAddressList: [...manifestIdByAddress].map(([address, manifestId]) => ({ address, manifestId })) };
}
/**
diff --git a/apps/browser-extension/src/utils/FaviconService.ts b/apps/browser-extension/src/utils/FaviconService.ts
index 80ab368d2..e752b5ad8 100644
--- a/apps/browser-extension/src/utils/FaviconService.ts
+++ b/apps/browser-extension/src/utils/FaviconService.ts
@@ -1,6 +1,6 @@
import { Buffer } from 'buffer';
-import type { Item } from '@/utils/dist/core/models/vault';
+import type { DraftItem } from '@/utils/db/ItemRef';
import type { SqliteClient } from '@/utils/SqliteClient';
import type { WebApiService } from '@/utils/WebApiService';
@@ -171,11 +171,11 @@ export class FaviconService {
* @returns The updated item with Logo attached (if favicon was fetched), cleared (if URL is empty), or the original item
*/
public static async fetchAndAttachFavicon(
- item: Item,
+ item: DraftItem,
urlFieldValue: string | string[] | undefined | null,
sqliteClient: SqliteClient,
webApi: WebApiService
- ): Promise<Item> {
+ ): Promise<DraftItem> {
const urlString = FaviconService.extractFirstValidUrl(urlFieldValue);
// If URL is empty or invalid, explicitly clear logo to signal that any existing logo should be removed
diff --git a/apps/browser-extension/src/utils/types/EmailRoutingPush.ts b/apps/browser-extension/src/utils/types/EmailRoutingPush.ts
new file mode 100644
index 000000000..b5fd86c57
--- /dev/null
+++ b/apps/browser-extension/src/utils/types/EmailRoutingPush.ts
@@ -0,0 +1,7 @@
+/**
+ * The email-routing set sent on a vault push: every alias the server should route, each carrying the manifest
+ * that owns it.
+ */
+export type EmailRoutingPush = {
+ emailAddressList: Array<{ address: string; manifestId: string }>;
+};
diff --git a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
index c5d684630..0cfc6187e 100644
--- a/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
+++ b/apps/server/AliasVault.Api/Controllers/V2/VaultController.cs
@@ -436,13 +436,15 @@ public class VaultController(
row.Client = clientHeader;
row.UpdatedAt = timeProvider.UtcNow;
+ // Every manifest counts the aliases the push filed against it, shared manifests included.
+ if (model.EmailRouting != null)
+ {
+ row.EmailClaimsCount = model.EmailRouting.EmailAddressList.Count(x => x.ManifestId == row.ManifestId);
+ }
+
if (row.IsRoot)
{
row.CreatedAt = timeProvider.UtcNow;
- if (model.EmailRouting != null)
- {
- row.EmailClaimsCount = model.EmailRouting.EmailAddressList.Count;
- }
// Create the account-key hierarchy atomically with this write on the migration (first push after
// the client re-encrypted the vault under a fresh VEK). Move the SRP credentials off the manifest row.
@@ -553,7 +555,7 @@ public class VaultController(
await context.SaveChangesAsync();
}
- if (model.EmailRouting != null && (model.EmailRouting.EmailAddressList.Count > 0 || model.EmailRouting.SharedEmailAddressList.Count > 0))
+ if (model.EmailRouting is { EmailAddressList.Count: > 0 })
{
await UpdateEmailClaimsAsync(context, user, model.EmailRouting);
}
@@ -709,7 +711,9 @@ public class VaultController(
}
/// <summary>
- /// Gets the (shared) manifestIds that user has access to and may claim new aliases for.
+ /// Gets the manifestIds that user has access to and may claim aliases for. A push files personal aliases
+ /// against the caller's own root manifest, which they reach by owning its group rather than by holding a
+ /// grant on it, so both arms of <see cref="ManifestAccessHelper"/> count here.
/// </summary>
/// <param name="context">Database context.</param>
/// <param name="userId">The calling user.</param>
@@ -723,12 +727,12 @@ public class VaultController(
return [];
}
- var granted = await context.VaultManifestAccessKeys
- .Where(k => k.UserId == userId && k.Type == ManifestKeyType.GrantKey && ids.Contains(k.VaultManifestId))
- .Select(k => k.VaultManifestId)
+ var accessible = await ManifestAccessHelper.AccessibleManifests(context, userId)
+ .Where(m => ids.Contains(m.ManifestId))
+ .Select(m => m.ManifestId)
.ToListAsync();
- return [.. granted];
+ return [.. accessible];
}
/// <summary>
@@ -1035,12 +1039,13 @@ public class VaultController(
/// </summary>
/// <param name="context">Database context.</param>
/// <param name="user">The calling user.</param>
- /// <param name="routing">The pushed routing data: personal addresses plus shared addresses with their manifest.</param>
- private async Task UpdateEmailClaimsAsync(AliasServerDbContext context, AliasVaultUser user, EmailRouting routing)
+ /// <param name="routing">The pushed routing data: every claimed address with the manifest it is filed under.</param>
+ private async Task UpdateEmailClaimsAsync(AliasServerDbContext context, AliasVaultUser user, EmailRoutingPush routing)
{
- var accessibleManifests = await GetEmailClaimableManifestIdsAsync(context, user.Id, routing.SharedEmailAddressList.Select(x => x.ManifestId));
+ var accessibleManifests = await GetEmailClaimableManifestIdsAsync(context, user.Id, routing.EmailAddressList.Select(x => x.ManifestId));
var ownerGroupByManifest = await GroupHelper.GetOwnerGroupsAsync(context, accessibleManifests);
+ // Resolved server-side and never read off the push: this is what stops a client filing an alias under a manifest it merely named.
var rootManifestId = await context.VaultManifests.Where(m => m.IsRoot && m.OwnerGroupId == user.PersonalGroupId).Select(m => (Guid?)m.ManifestId).FirstOrDefaultAsync();
if (rootManifestId is null)
{
@@ -1055,25 +1060,24 @@ public class VaultController(
.ToListAsync()).ToHashSet();
var manifestByAddress = new Dictionary<string, Guid>();
- foreach (var shared in routing.SharedEmailAddressList)
+ foreach (var claimed in routing.EmailAddressList)
{
- var sanitizedShared = EmailHelper.SanitizeEmail(shared.Address);
- if (!accessibleManifests.Contains(shared.ManifestId))
+ var sanitizedClaimed = EmailHelper.SanitizeEmail(claimed.Address);
+ if (!accessibleManifests.Contains(claimed.ManifestId))
{
- logger.LogWarning("{User} claimed shared alias {Email} for manifest {Manifest} they cannot access; treating it as a personal alias.", user.UserName, sanitizedShared, shared.ManifestId);
+ logger.LogWarning("{User} claimed alias {Email} for manifest {Manifest} they cannot access; filing it under their own root manifest instead.", user.UserName, sanitizedClaimed, claimed.ManifestId);
continue;
}
- manifestByAddress[sanitizedShared] = shared.ManifestId;
- if (!manifestsWithDeliveryKey.Contains(shared.ManifestId))
+ manifestByAddress[sanitizedClaimed] = claimed.ManifestId;
+ if (claimed.ManifestId != rootManifestId.Value && !manifestsWithDeliveryKey.Contains(claimed.ManifestId))
{
- logger.LogWarning("{User} claimed shared alias {Email} for manifest {Manifest} with no published delivery key; its mail stays readable by the routing owner alone until a delivery key is published.", user.UserName, sanitizedShared, shared.ManifestId);
+ logger.LogWarning("{User} claimed shared alias {Email} for manifest {Manifest} with no published delivery key; its mail stays readable by the routing owner alone until a delivery key is published.", user.UserName, sanitizedClaimed, claimed.ManifestId);
}
}
var newEmailAddresses = routing.EmailAddressList
- .Concat(routing.SharedEmailAddressList.Select(x => x.Address))
- .Select(EmailHelper.SanitizeEmail)
+ .Select(x => EmailHelper.SanitizeEmail(x.Address))
.Distinct()
.ToList();
@@ -1107,12 +1111,11 @@ public class VaultController(
continue;
}
- // Check which manifest the alias is filed against.
- var sharedManifestId = manifestByAddress.TryGetValue(sanitized, out var mappedManifestId) ? mappedManifestId : (Guid?)null;
- var resolvedManifestId = sharedManifestId ?? rootManifestId.Value;
+ // Which manifest the alias is filed against: the one the push named when the caller may claim for it, their own root otherwise.
+ var resolvedManifestId = manifestByAddress.TryGetValue(sanitized, out var claimedManifestId) ? claimedManifestId : rootManifestId.Value;
// The quota subject is the group owning that manifest: the shared manifest's group, or the caller's personal group.
- var quotaGroupId = sharedManifestId is not null && ownerGroupByManifest.TryGetValue(sharedManifestId.Value, out var sharedGroupId) ? sharedGroupId : user.PersonalGroupId;
+ var quotaGroupId = ownerGroupByManifest.TryGetValue(resolvedManifestId, out var ownerGroupId) ? ownerGroupId : user.PersonalGroupId;
var existing = userOwnedEmailClaims.FirstOrDefault(x => x.Address == sanitized);
if (existing != null)
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/SharedEmailAddress.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ClaimedEmailAddress.cs
similarity index 54%
rename from apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/SharedEmailAddress.cs
rename to apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ClaimedEmailAddress.cs
index f71e4c74d..42bdba560 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/SharedEmailAddress.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/ClaimedEmailAddress.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="SharedEmailAddress.cs" company="aliasvault">
+// <copyright file="ClaimedEmailAddress.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>
@@ -8,15 +8,15 @@
namespace AliasVault.Shared.Models.WebApi.V2.Vault;
/// <summary>
-/// An email alias whose item lives in a shared manifest: mail for it is encrypted with the manifest's
-/// published keypair rather than the routing owner's personal key, so every member of the folder can
-/// read it.
+/// An email alias the client claims, together with the manifest holding the item it belongs to. The manifest
+/// decides how the alias's mail is encrypted: a shared manifest's published keypair (readable by every member)
+/// or, for the caller's root manifest, their own primary personal key.
/// </summary>
-public class SharedEmailAddress
+public class ClaimedEmailAddress
{
/// <summary>Gets or sets the full email address.</summary>
public required string Address { get; set; }
- /// <summary>Gets or sets the shared manifest whose key encrypts mail for this address.</summary>
+ /// <summary>Gets or sets the manifest whose key encrypts mail for this address.</summary>
public required Guid ManifestId { get; set; }
}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRouting.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRouting.cs
index ec4f44b61..d127d431e 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRouting.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRouting.cs
@@ -8,19 +8,14 @@
namespace AliasVault.Shared.Models.WebApi.V2.Vault;
/// <summary>
-/// Email-routing plaintext data.
+/// Email-routing plaintext data returned on a vault read. The push direction has its own model,
+/// <see cref="EmailRoutingPush"/>, which carries the owning manifest of every address.
/// </summary>
public class EmailRouting
{
/// <summary>Gets or sets the user's claimed email addresses (forwarded inbound).</summary>
public List<string> EmailAddressList { get; set; } = [];
- /// <summary>
- /// Gets or sets the claimed addresses whose items live in a shared manifest, each carrying the manifest
- /// whose published keypair encrypts its mail.
- /// </summary>
- public List<SharedEmailAddress> SharedEmailAddressList { get; set; } = [];
-
/// <summary>Gets or sets the private email domains available to this user.</summary>
public List<string> PrivateEmailDomainList { get; set; } = [];
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRoutingPush.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRoutingPush.cs
new file mode 100644
index 000000000..3982b14ab
--- /dev/null
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/EmailRoutingPush.cs
@@ -0,0 +1,21 @@
+//-----------------------------------------------------------------------
+// <copyright file="EmailRoutingPush.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.Shared.Models.WebApi.V2.Vault;
+
+/// <summary>
+/// The email-routing set a vault push uploads: every alias the client wants routed, each carrying the manifest
+/// that owns it. Aliases missing from a push are disabled, so a push always carries the complete set.
+/// </summary>
+public class EmailRoutingPush
+{
+ /// <summary>
+ /// Gets or sets the claimed addresses, each with the manifest whose key encrypts its mail. The manifest ids
+ /// are a request, not a grant: the server validates each against what the caller may claim for.
+ /// </summary>
+ public List<ClaimedEmailAddress> EmailAddressList { get; set; } = [];
+}
diff --git a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs
index bd2cf5c81..a24c4772e 100644
--- a/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs
+++ b/apps/server/Shared/AliasVault.Shared/Models/WebApi/V2/Vault/VaultWriteRequest.cs
@@ -25,7 +25,7 @@ public class VaultWriteRequest
public List<Blob> NewBlobs { get; set; } = [];
/// <summary>Gets or sets the email routing data to update server-side.</summary>
- public EmailRouting? EmailRouting { get; set; }
+ public EmailRoutingPush? EmailRouting { get; set; }
/// <summary>
/// Gets or sets the user's public encryption key.