Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add EmailPart separation for more efficient storage and client retrieval (#2159)
Leendert de Borste3329a7546d1
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx b/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsxindex 009a2da22..df23ae7d2 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx@@ -218,7 +218,20 @@ const EmailDetails: React.FC = (): React.ReactElement => {return;}- const bytes = await extractEmailAttachment(sourceBytes, index);+ let detachedBody: Uint8Array | undefined;+ if (attachment.detached && attachment.partIndex !== null) {+ if (!dbContext?.sqliteClient || !email) {+ setError('Database context or email not available');+ return;+ }++ const encryptedPart = await webApi.downloadBlob(`Email/${id}/parts/${attachment.partIndex}`);+ const encryptionKeys = dbContext.sqliteClient.encryptionKeys.getAll();++ detachedBody = await EncryptionUtility.decryptAttachment(encryptedPart, email, encryptionKeys);+ }++ const bytes = await extractEmailAttachment(sourceBytes, index, detachedBody);triggerAttachmentDownload(bytes, attachment.mimeType, attachment.filename);} catch (err) {console.error('handleDownloadParsedAttachment error', err);diff --git a/apps/browser-extension/src/utils/RustCore.ts b/apps/browser-extension/src/utils/RustCore.tsindex 1fc6db3ee..8594d084c 100644--- a/apps/browser-extension/src/utils/RustCore.ts+++ b/apps/browser-extension/src/utils/RustCore.ts@@ -179,6 +179,8 @@ export type ParsedEmailAttachment = {filename: string;mimeType: string;size: number;+ detached: boolean;+ partIndex: number | null;};/** Result of parsing a raw RFC 822 email source. Body fields are null when the message has no such part. */@@ -207,9 +209,9 @@ export async function decodeEmailSource(source: Uint8Array): Promise<Uint8Array>/*** Extract the decoded bytes of one attachment, identified by its index in the parsed attachment list.*/-export async function extractEmailAttachment(source: Uint8Array, index: number): Promise<Uint8Array> {+export async function extractEmailAttachment(source: Uint8Array, index: number, detachedBody?: Uint8Array): Promise<Uint8Array> {await initRustCore();- return core.extractEmailAttachment(source, index) as Uint8Array;+ return core.extractEmailAttachment(source, index, detachedBody) as Uint8Array;}/**diff --git a/apps/server/AliasVault.Api/Controllers/V2/Email/EmailController.cs b/apps/server/AliasVault.Api/Controllers/V2/Email/EmailController.csindex 8432ba1db..259d3a155 100644--- a/apps/server/AliasVault.Api/Controllers/V2/Email/EmailController.cs+++ b/apps/server/AliasVault.Api/Controllers/V2/Email/EmailController.cs@@ -138,6 +138,33 @@ public class EmailController(ILogger<EmailController> logger, IAliasServerDbContreturn File(attachment.Bytes, "application/octet-stream", attachment.Filename);}+ /// <summary>+ /// Get the bytes of an attachment body that was detached from the email's source at ingest.+ /// </summary>+ /// <param name="id">The email ID.</param>+ /// <param name="partIndex">The part index, as advertised by the X-AliasVault-Part header on the attachment in the message source.</param>+ /// <returns>Part bytes in encrypted form.</returns>+ [HttpGet(template: "{id}/parts/{partIndex}", Name = "GetEmailPart")]+ public async Task<IActionResult> GetEmailPart(int id, int partIndex)+ {+ await using var context = await dbContextFactory.CreateDbContextAsync();++ var (email, _, errorResult) = await AuthenticateAndRetrieveEmailAsync(id, context);+ if (errorResult != null)+ {+ return errorResult;+ }++ var part = await context.EmailParts.FirstOrDefaultAsync(x => x.EmailId == email!.Id && x.PartIndex == partIndex);+ if (part == null)+ {+ return NotFound("Email part not found.");+ }++ // Return the encrypted bytes as binary.+ return File(part.Bytes, "application/octet-stream");+ }+/// <summary>/// Authenticates the user and retrieves the requested email./// </summary>diff --git a/apps/server/Databases/AliasServerDb/AliasServerDbContext.cs b/apps/server/Databases/AliasServerDb/AliasServerDbContext.csindex d128f8c22..686fd10f9 100644--- a/apps/server/Databases/AliasServerDb/AliasServerDbContext.cs+++ b/apps/server/Databases/AliasServerDb/AliasServerDbContext.cs@@ -106,6 +106,11 @@ public class AliasServerDbContext : WorkerStatusDbContext, IDataProtectionKeyCon/// </summary>public DbSet<EmailKeyWrap> EmailKeyWraps { get; set; }+ /// <summary>+ /// Gets or sets the EmailParts DbSet.+ /// </summary>+ public DbSet<EmailPart> EmailParts { get; set; }+/// <summary>/// Gets or sets the Groups DbSet./// </summary>diff --git a/apps/server/Databases/AliasServerDb/Email.cs b/apps/server/Databases/AliasServerDb/Email.csindex a2e12eec0..4cbb41b1f 100644--- a/apps/server/Databases/AliasServerDb/Email.cs+++ b/apps/server/Databases/AliasServerDb/Email.cs@@ -122,4 +122,10 @@ public class Email/// Gets or sets the collection of email attachments./// </summary>public virtual List<EmailAttachment> Attachments { get; set; } = [];++ /// <summary>+ /// Gets or sets the attachment bodies that were detached from <see cref="MessageSourceBytes"/> at ingest.+ /// Clients can fetch these individually, only when the user actually opens an attachment.+ /// </summary>+ public virtual List<EmailPart> Parts { get; set; } = [];}diff --git a/apps/server/Databases/AliasServerDb/EmailPart.cs b/apps/server/Databases/AliasServerDb/EmailPart.csnew file mode 100644index 000000000..d0865d37d--- /dev/null+++ b/apps/server/Databases/AliasServerDb/EmailPart.cs@@ -0,0 +1,46 @@+//-----------------------------------------------------------------------+// <copyright file="EmailPart.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;++using Microsoft.EntityFrameworkCore;++/// <summary>+/// Represents an attachment body that was detached from an email's MIME source at ingest so that opening the+/// email does not require downloading its attachments. The source itself keeps the attachment's part headers,+/// which is where the filename and MIME type live.+/// </summary>+[Index(nameof(EmailId), nameof(PartIndex), IsUnique = true)]+public class EmailPart+{+ /// <summary>+ /// Gets or sets the ID of the part.+ /// </summary>+ public int Id { get; set; }++ /// <summary>+ /// Gets or sets the ID of the email that the part was detached from.+ /// </summary>+ public int EmailId { get; set; }++ /// <summary>+ /// Gets or sets the email that the part was detached from.+ /// </summary>+ public virtual Email Email { get; set; } = null!;++ /// <summary>+ /// Gets or sets the index that identifies this part within its email. The same value is stamped on the+ /// detached part in the message source as the X-AliasVault-Part header, which is how clients know which+ /// part to request.+ /// </summary>+ public int PartIndex { get; set; }++ /// <summary>+ /// Gets or sets the detached part body.+ /// </summary>+ public byte[] Bytes { get; set; } = null!;+}diff --git a/apps/server/Databases/AliasServerDb/Migrations/20260811195539_EmailDetachedParts.Designer.cs b/apps/server/Databases/AliasServerDb/Migrations/20260811195539_EmailDetachedParts.Designer.csnew file mode 100644index 000000000..082cf0d02--- /dev/null+++ b/apps/server/Databases/AliasServerDb/Migrations/20260811195539_EmailDetachedParts.Designer.cs@@ -0,0 +1,1665 @@+// <auto-generated />+using System;+using AliasServerDb;+using Microsoft.EntityFrameworkCore;+using Microsoft.EntityFrameworkCore.Infrastructure;+using Microsoft.EntityFrameworkCore.Migrations;+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;++#nullable disable++namespace AliasServerDb.Migrations+{+ [DbContext(typeof(AliasServerDbContext))]+ [Migration("20260811195539_EmailDetachedParts")]+ partial class EmailDetachedParts+ {+ /// <inheritdoc />+ protected override void BuildTargetModel(ModelBuilder modelBuilder)+ {+#pragma warning disable 612, 618+ modelBuilder+ .HasAnnotation("ProductVersion", "10.0.10")+ .HasAnnotation("Proxies:ChangeTracking", false)+ .HasAnnotation("Proxies:CheckEquality", false)+ .HasAnnotation("Proxies:LazyLoading", true)+ .HasAnnotation("Relational:MaxIdentifierLength", 63);++ NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);++ modelBuilder.Entity("AliasServerDb.AdminUser", b =>+ {+ b.Property<string>("Id")+ .HasColumnType("text");++ b.Property<int>("AccessFailedCount")+ .HasColumnType("integer");++ b.Property<string>("ConcurrencyStamp")+ .HasColumnType("text");++ b.Property<string>("Email")+ .HasColumnType("text");++ b.Property<bool>("EmailConfirmed")+ .HasColumnType("boolean");++ b.Property<DateTime?>("LastPasswordChanged")+ .HasColumnType("timestamp with time zone");++ b.Property<bool>("LockoutEnabled")+ .HasColumnType("boolean");++ b.Property<DateTimeOffset?>("LockoutEnd")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("NormalizedEmail")+ .HasColumnType("text");++ b.Property<string>("NormalizedUserName")+ .HasColumnType("text");++ b.Property<string>("PasswordHash")+ .HasColumnType("text");++ b.Property<string>("PhoneNumber")+ .HasColumnType("text");++ b.Property<bool>("PhoneNumberConfirmed")+ .HasColumnType("boolean");++ b.Property<string>("SecurityStamp")+ .HasColumnType("text");++ b.Property<bool>("TwoFactorEnabled")+ .HasColumnType("boolean");++ b.Property<string>("UserName")+ .HasColumnType("text");++ b.HasKey("Id");++ b.ToTable("AdminUsers");+ });++ modelBuilder.Entity("AliasServerDb.AliasVaultUser", b =>+ {+ b.Property<string>("Id")+ .HasColumnType("text");++ b.Property<int>("AccessFailedCount")+ .HasColumnType("integer");++ b.Property<bool>("Blocked")+ .HasColumnType("boolean");++ b.Property<DateTime?>("BlockedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("ConcurrencyStamp")+ .HasColumnType("text");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("Email")+ .HasColumnType("text");++ b.Property<bool>("EmailConfirmed")+ .HasColumnType("boolean");++ b.Property<DateTime?>("LastActivityDate")+ .HasColumnType("timestamp with time zone");++ b.Property<bool>("LockoutEnabled")+ .HasColumnType("boolean");++ b.Property<DateTimeOffset?>("LockoutEnd")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("NormalizedEmail")+ .HasColumnType("text");++ b.Property<string>("NormalizedUserName")+ .HasColumnType("text");++ b.Property<DateTime>("PasswordChangedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("PasswordHash")+ .HasColumnType("text");++ b.Property<Guid>("PersonalGroupId")+ .HasColumnType("uuid");++ b.Property<string>("PhoneNumber")+ .HasColumnType("text");++ b.Property<bool>("PhoneNumberConfirmed")+ .HasColumnType("boolean");++ b.Property<string>("SecurityStamp")+ .HasColumnType("text");++ b.Property<string>("SrpIdentity")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<bool>("TwoFactorEnabled")+ .HasColumnType("boolean");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("UserName")+ .HasColumnType("text");++ b.HasKey("Id");++ b.HasIndex("PersonalGroupId")+ .IsUnique()+ .HasDatabaseName("UX_AliasVaultUsers_PersonalGroupId");++ b.ToTable("AliasVaultUsers");+ });++ modelBuilder.Entity("AliasServerDb.AliasVaultUserRefreshToken", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("DeviceIdentifier")+ .IsRequired()+ .HasColumnType("text");++ b.Property<DateTime>("ExpireDate")+ .HasMaxLength(255)+ .HasColumnType("timestamp with time zone");++ b.Property<string>("IpAddress")+ .HasMaxLength(45)+ .HasColumnType("character varying(45)");++ b.Property<string>("PreviousTokenValue")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<string>("UserId")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<string>("Value")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("Id");++ b.HasIndex("UserId");++ b.ToTable("AliasVaultUserRefreshTokens");+ });++ modelBuilder.Entity("AliasServerDb.AuthLog", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<string>("AdditionalInfo")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<string>("Browser")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<string>("Client")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<string>("Country")+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<string>("DeviceType")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<int>("EventType")+ .HasColumnType("integer");++ b.Property<int?>("FailureReason")+ .HasColumnType("integer");++ b.Property<string>("IpAddress")+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<bool>("IsSuccess")+ .HasColumnType("boolean");++ b.Property<bool>("IsSuspiciousActivity")+ .HasColumnType("boolean");++ b.Property<string>("OperatingSystem")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<string>("RequestPath")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<DateTime>("Timestamp")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("Username")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("Id");++ b.HasIndex(new[] { "EventType" }, "IX_EventType");++ b.HasIndex(new[] { "IpAddress" }, "IX_IpAddress");++ b.HasIndex(new[] { "Timestamp" }, "IX_Timestamp");++ b.HasIndex(new[] { "Username", "IsSuccess", "Timestamp" }, "IX_Username_IsSuccess_Timestamp")+ .IsDescending(false, false, true);++ b.HasIndex(new[] { "Username", "Timestamp" }, "IX_Username_Timestamp")+ .IsDescending(false, true);++ b.ToTable("AuthLogs");+ });++ modelBuilder.Entity("AliasServerDb.BlockedIpRange", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<bool>("BlockLogin")+ .HasColumnType("boolean");++ b.Property<bool>("BlockRegistration")+ .HasColumnType("boolean");++ b.Property<bool>("BlockShadow")+ .HasColumnType("boolean");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("CreatedBy")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<bool>("Enabled")+ .HasColumnType("boolean");++ b.Property<string>("IpRange")+ .IsRequired()+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<string>("Reason")+ .HasMaxLength(500)+ .HasColumnType("character varying(500)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.HasKey("Id");++ b.HasIndex(new[] { "Enabled" }, "IX_BlockedIpRange_Enabled");++ b.HasIndex(new[] { "IpRange" }, "IX_BlockedIpRange_IpRange")+ .IsUnique();++ b.ToTable("BlockedIpRanges");+ });++ modelBuilder.Entity("AliasServerDb.Email", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<int>("AttachmentCount")+ .HasColumnType("integer");++ b.Property<DateTime>("Date")+ .HasColumnType("timestamp with time zone");++ b.Property<DateTime>("DateSystem")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("From")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("FromDomain")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("FromLocal")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("MessageHtml")+ .HasColumnType("text");++ b.Property<string>("MessagePlain")+ .HasColumnType("text");++ b.Property<string>("MessagePreview")+ .HasColumnType("text");++ b.Property<string>("MessageSource")+ .HasColumnType("text");++ b.Property<byte[]>("MessageSourceBytes")+ .HasColumnType("bytea");++ b.Property<bool>("PushNotificationSent")+ .HasColumnType("boolean");++ b.Property<string>("Subject")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("To")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("ToDomain")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("ToLocal")+ .IsRequired()+ .HasColumnType("text");++ b.Property<bool>("Visible")+ .HasColumnType("boolean");++ b.HasKey("Id");++ b.HasIndex("Date");++ b.HasIndex("DateSystem");++ b.HasIndex("PushNotificationSent");++ b.HasIndex("Visible");++ b.HasIndex("To", "DateSystem");++ b.ToTable("Emails");+ });++ modelBuilder.Entity("AliasServerDb.EmailAttachment", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<byte[]>("Bytes")+ .IsRequired()+ .HasColumnType("bytea");++ b.Property<DateTime>("Date")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("EmailId")+ .HasColumnType("integer");++ b.Property<string>("Filename")+ .IsRequired()+ .HasColumnType("text");++ b.Property<int>("Filesize")+ .HasColumnType("integer");++ b.Property<string>("MimeType")+ .IsRequired()+ .HasColumnType("text");++ b.HasKey("Id");++ b.HasIndex("EmailId");++ b.ToTable("EmailAttachments");+ });++ modelBuilder.Entity("AliasServerDb.EmailClaim", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<string>("Address")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<string>("AddressDomain")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<string>("AddressLocal")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<bool>("Disabled")+ .HasColumnType("boolean");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.HasKey("Id");++ b.HasIndex("Address")+ .IsUnique();++ b.ToTable("EmailClaims");+ });++ modelBuilder.Entity("AliasServerDb.EmailClaimLink", b =>+ {+ b.Property<Guid>("EmailClaimId")+ .HasColumnType("uuid");++ b.Property<Guid>("VaultManifestId")+ .HasColumnType("uuid");++ b.HasKey("EmailClaimId", "VaultManifestId");++ b.HasIndex("VaultManifestId", "EmailClaimId");++ b.ToTable("EmailClaimLinks");+ });++ modelBuilder.Entity("AliasServerDb.EmailKeyWrap", b =>+ {+ b.Property<int>("EmailId")+ .HasColumnType("integer");++ b.Property<Guid>("EncryptionKeyId")+ .HasColumnType("uuid");++ b.Property<string>("EncryptedSymmetricKey")+ .IsRequired()+ .HasColumnType("text");++ b.HasKey("EmailId", "EncryptionKeyId");++ b.HasIndex("EncryptionKeyId", "EmailId");++ b.ToTable("EmailKeyWraps");+ });++ modelBuilder.Entity("AliasServerDb.EmailPart", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<byte[]>("Bytes")+ .IsRequired()+ .HasColumnType("bytea");++ b.Property<int>("EmailId")+ .HasColumnType("integer");++ b.Property<int>("PartIndex")+ .HasColumnType("integer");++ b.HasKey("Id");++ b.HasIndex("EmailId", "PartIndex")+ .IsUnique();++ b.ToTable("EmailParts");+ });++ modelBuilder.Entity("AliasServerDb.Group", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("EmailsReceived")+ .HasColumnType("integer");++ b.Property<int>("MaxEmailAgeDays")+ .HasColumnType("integer");++ b.Property<int>("MaxEmails")+ .HasColumnType("integer");++ b.Property<string>("Name")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<bool>("ShadowBlocked")+ .HasColumnType("boolean");++ b.Property<DateTime?>("ShadowBlockedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("Type")+ .HasColumnType("integer");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.HasKey("Id");++ b.ToTable("Groups");+ });++ modelBuilder.Entity("AliasServerDb.GroupMember", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<Guid>("GroupId")+ .HasColumnType("uuid");++ b.Property<int>("Role")+ .HasColumnType("integer");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("UserId")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("Id");++ b.HasIndex("UserId");++ b.HasIndex("GroupId", "UserId")+ .IsUnique();++ b.ToTable("GroupMembers");+ });++ modelBuilder.Entity("AliasServerDb.Log", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<string>("Application")+ .IsRequired()+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<string>("Exception")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("Level")+ .IsRequired()+ .HasMaxLength(128)+ .HasColumnType("character varying(128)");++ b.Property<string>("LogEvent")+ .IsRequired()+ .HasColumnType("text")+ .HasColumnName("LogEvent");++ b.Property<string>("Message")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("MessageTemplate")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("Properties")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("SourceContext")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<DateTime>("TimeStamp")+ .HasColumnType("timestamp with time zone");++ b.HasKey("Id");++ b.HasIndex("Application");++ b.HasIndex("TimeStamp");++ b.ToTable("Logs", (string)null);+ });++ modelBuilder.Entity("AliasServerDb.MobileLoginRequest", b =>+ {+ b.Property<string>("Id")+ .HasColumnType("text");++ b.Property<DateTime?>("ClearedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("ClientIpAddress")+ .HasColumnType("text");++ b.Property<string>("ClientPublicKey")+ .IsRequired()+ .HasColumnType("text");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("EncryptedDecryptionKey")+ .HasColumnType("text");++ b.Property<DateTime?>("FulfilledAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("MobileIpAddress")+ .HasColumnType("text");++ b.Property<DateTime?>("RetrievedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("UserId")+ .HasColumnType("text");++ b.HasKey("Id");++ b.HasIndex(new[] { "ClientIpAddress" }, "IX_ClientIpAddress");++ b.HasIndex(new[] { "CreatedAt" }, "IX_CreatedAt");++ b.HasIndex(new[] { "MobileIpAddress" }, "IX_MobileIpAddress");++ b.HasIndex(new[] { "RetrievedAt", "ClearedAt", "FulfilledAt" }, "IX_RetrievedAt_ClearedAt_FulfilledAt");++ b.HasIndex(new[] { "UserId" }, "IX_UserId");++ b.ToTable("MobileLoginRequests");+ });++ modelBuilder.Entity("AliasServerDb.RateLimit", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<int?>("AppliesToAccountAgeMaxDays")+ .HasColumnType("integer");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("CreatedBy")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<DateTime?>("EffectiveFrom")+ .HasColumnType("timestamp with time zone");++ b.Property<DateTime?>("EffectiveUntil")+ .HasColumnType("timestamp with time zone");++ b.Property<bool>("Enabled")+ .HasColumnType("boolean");++ b.Property<Guid?>("GroupId")+ .HasColumnType("uuid");++ b.Property<int>("LimitType")+ .HasColumnType("integer");++ b.Property<int>("MaxCount")+ .HasColumnType("integer");++ b.Property<string>("Notes")+ .HasMaxLength(1000)+ .HasColumnType("character varying(1000)");++ b.Property<int?>("Tier")+ .HasColumnType("integer");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("WindowSeconds")+ .HasColumnType("integer");++ b.HasKey("Id");++ b.HasIndex("GroupId");++ b.HasIndex("Tier");++ b.HasIndex("LimitType", "Enabled");++ b.ToTable("RateLimits");+ });++ modelBuilder.Entity("AliasServerDb.ServerSetting", b =>+ {+ b.Property<string>("Key")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("Value")+ .HasColumnType("text");++ b.HasKey("Key");++ b.ToTable("ServerSettings");+ });++ modelBuilder.Entity("AliasServerDb.TaskRunnerJob", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<TimeOnly?>("EndTime")+ .HasColumnType("time without time zone");++ b.Property<string>("ErrorMessage")+ .HasColumnType("text");++ b.Property<bool>("IsOnDemand")+ .HasColumnType("boolean");++ b.Property<string>("Name")+ .IsRequired()+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<DateTime>("RunDate")+ .HasColumnType("timestamp with time zone");++ b.Property<TimeOnly>("StartTime")+ .HasColumnType("time without time zone");++ b.Property<int>("Status")+ .HasColumnType("integer");++ b.HasKey("Id");++ b.ToTable("TaskRunnerJobs");+ });++ modelBuilder.Entity("AliasServerDb.UserGrantKey", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<int>("AccountKeyVersion")+ .HasColumnType("integer");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("EncryptedPrivateKey")+ .IsRequired()+ .HasMaxLength(4000)+ .HasColumnType("character varying(4000)");++ b.Property<bool>("IsPrimary")+ .HasColumnType("boolean");++ b.Property<string>("PublicKey")+ .IsRequired()+ .HasMaxLength(2000)+ .HasColumnType("character varying(2000)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("UserId")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("Id");++ b.HasIndex("UserId")+ .IsUnique()+ .HasDatabaseName("UX_UserGrantKeys_User_Primary")+ .HasFilter("\"IsPrimary\"");++ b.ToTable("UserGrantKeys");+ });++ modelBuilder.Entity("AliasServerDb.UserUnlockKey", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<int>("AccountKeyVersion")+ .HasColumnType("integer");++ b.Property<string>("Algorithm")+ .IsRequired()+ .HasMaxLength(30)+ .HasColumnType("character varying(30)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("EncryptedAccountKey")+ .IsRequired()+ .HasColumnType("text");++ b.Property<string>("Label")+ .IsRequired()+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<DateTime?>("LastUsedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("Metadata")+ .HasColumnType("jsonb");++ b.Property<string>("Type")+ .IsRequired()+ .HasMaxLength(30)+ .HasColumnType("character varying(30)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("UserId")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("Id");++ b.HasIndex("UserId", "Type", "Label")+ .IsUnique()+ .HasDatabaseName("UX_UserUnlockKeys_UserId_Type_Label");++ b.ToTable("UserUnlockKeys");+ });++ modelBuilder.Entity("AliasServerDb.VaultBlobObject", b =>+ {+ b.Property<string>("Hash")+ .HasMaxLength(64)+ .HasColumnType("character varying(64)");++ b.Property<string>("OwnerUserId")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<string>("Category")+ .IsRequired()+ .HasMaxLength(20)+ .HasColumnType("character varying(20)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<byte[]>("EncryptedData")+ .IsRequired()+ .HasColumnType("bytea");++ b.Property<int>("KeyVersion")+ .HasColumnType("integer");++ b.Property<DateTime>("LastReferencedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("SizeBytes")+ .HasColumnType("integer");++ b.HasKey("Hash", "OwnerUserId");++ b.HasIndex("OwnerUserId", "Category");++ b.ToTable("VaultBlobObjects");+ });++ modelBuilder.Entity("AliasServerDb.VaultBlobReference", b =>+ {+ b.Property<Guid>("ManifestId")+ .HasColumnType("uuid");++ b.Property<long>("RevisionNumber")+ .HasColumnType("bigint");++ b.Property<string>("BlobHash")+ .HasMaxLength(64)+ .HasColumnType("character varying(64)");++ b.HasKey("ManifestId", "RevisionNumber", "BlobHash");++ b.ToTable("VaultBlobReferences");+ });++ modelBuilder.Entity("AliasServerDb.VaultDataBucket", b =>+ {+ b.Property<Guid>("ManifestId")+ .HasColumnType("uuid");++ b.Property<string>("Category")+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<string>("CiphertextHash")+ .HasMaxLength(64)+ .HasColumnType("character varying(64)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<byte[]>("EncryptedData")+ .IsRequired()+ .HasColumnType("bytea");++ b.Property<int>("KeyVersion")+ .HasColumnType("integer");++ b.Property<long>("RevisionNumber")+ .HasColumnType("bigint");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.HasKey("ManifestId", "Category");++ b.ToTable("VaultDataBuckets");+ });++ modelBuilder.Entity("AliasServerDb.VaultDataBucketsHistory", b =>+ {+ b.Property<Guid>("ManifestId")+ .HasColumnType("uuid");++ b.Property<string>("Category")+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<long>("RevisionNumber")+ .HasColumnType("bigint");++ b.Property<string>("CiphertextHash")+ .HasMaxLength(64)+ .HasColumnType("character varying(64)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<byte[]>("EncryptedData")+ .IsRequired()+ .HasColumnType("bytea");++ b.Property<int>("KeyVersion")+ .HasColumnType("integer");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.HasKey("ManifestId", "Category", "RevisionNumber");++ b.ToTable("VaultDataBucketsHistory");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifest", b =>+ {+ b.Property<Guid>("ManifestId")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<string>("Client")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("CredentialsCount")+ .HasColumnType("integer");++ b.Property<int>("EmailClaimsCount")+ .HasColumnType("integer");++ b.Property<string>("EncryptionSettings")+ .HasColumnType("text");++ b.Property<string>("EncryptionType")+ .HasColumnType("text");++ b.Property<int>("FileSize")+ .HasColumnType("integer");++ b.Property<int>("KeyVersion")+ .HasColumnType("integer");++ b.Property<byte[]>("ManifestBlob")+ .HasColumnType("bytea");++ b.Property<string>("ManifestCiphertextHash")+ .HasMaxLength(64)+ .HasColumnType("character varying(64)");++ b.Property<string>("Name")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<Guid>("OwnerGroupId")+ .HasColumnType("uuid");++ b.Property<long>("RevisionNumber")+ .HasColumnType("bigint");++ b.Property<string>("Salt")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<string>("StorageFormat")+ .IsRequired()+ .HasMaxLength(20)+ .HasColumnType("character varying(20)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("VaultBlob")+ .HasColumnType("text");++ b.Property<string>("Verifier")+ .HasMaxLength(1000)+ .HasColumnType("character varying(1000)");++ b.Property<string>("Version")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("ManifestId");++ b.HasIndex("OwnerGroupId");++ b.ToTable("VaultManifests");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestAccessKey", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<int?>("AccountKeyVersion")+ .HasColumnType("integer");++ b.Property<string>("Algorithm")+ .IsRequired()+ .HasMaxLength(30)+ .HasColumnType("character varying(30)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("EncryptedVek")+ .IsRequired()+ .HasColumnType("text");++ b.Property<int>("KeyVersion")+ .HasColumnType("integer");++ b.Property<DateTime?>("LastUsedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("Metadata")+ .HasColumnType("jsonb");++ b.Property<string>("Type")+ .IsRequired()+ .HasMaxLength(30)+ .HasColumnType("character varying(30)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<Guid?>("UserGrantKeyId")+ .HasColumnType("uuid");++ b.Property<string>("UserId")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<Guid>("VaultManifestId")+ .HasColumnType("uuid");++ b.HasKey("Id");++ b.HasIndex("UserGrantKeyId");++ b.HasIndex("VaultManifestId")+ .HasDatabaseName("IX_VaultManifestAccessKeys_VaultManifestId");++ b.HasIndex("UserId", "Type", "VaultManifestId", "KeyVersion")+ .IsUnique()+ .HasDatabaseName("UX_VaultManifestAccessKeys_UserId_Type_Manifest_Version");++ b.ToTable("VaultManifestAccessKeys");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestDeliveryKey", b =>+ {+ b.Property<Guid>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("uuid");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<bool>("IsPrimary")+ .HasColumnType("boolean");++ b.Property<string>("PublicKey")+ .IsRequired()+ .HasMaxLength(2000)+ .HasColumnType("character varying(2000)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<Guid>("VaultManifestId")+ .HasColumnType("uuid");++ b.HasKey("Id");++ b.HasIndex("VaultManifestId")+ .IsUnique()+ .HasDatabaseName("UX_VaultManifestDeliveryKeys_Manifest_Primary")+ .HasFilter("\"IsPrimary\"");++ b.HasIndex("VaultManifestId", "IsPrimary");++ b.ToTable("VaultManifestDeliveryKeys");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestsHistory", b =>+ {+ b.Property<Guid>("ManifestId")+ .HasColumnType("uuid");++ b.Property<long>("RevisionNumber")+ .HasColumnType("bigint");++ b.Property<string>("Client")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.Property<DateTime>("CreatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<int>("CredentialsCount")+ .HasColumnType("integer");++ b.Property<int>("EmailClaimsCount")+ .HasColumnType("integer");++ b.Property<string>("EncryptionSettings")+ .HasColumnType("text");++ b.Property<string>("EncryptionType")+ .HasColumnType("text");++ b.Property<int>("FileSize")+ .HasColumnType("integer");++ b.Property<int>("KeyVersion")+ .HasColumnType("integer");++ b.Property<byte[]>("ManifestBlob")+ .HasColumnType("bytea");++ b.Property<string>("ManifestCiphertextHash")+ .HasMaxLength(64)+ .HasColumnType("character varying(64)");++ b.Property<string>("Salt")+ .HasMaxLength(100)+ .HasColumnType("character varying(100)");++ b.Property<string>("StorageFormat")+ .IsRequired()+ .HasMaxLength(20)+ .HasColumnType("character varying(20)");++ b.Property<DateTime>("UpdatedAt")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("VaultBlob")+ .HasColumnType("text");++ b.Property<string>("Verifier")+ .HasMaxLength(1000)+ .HasColumnType("character varying(1000)");++ b.Property<string>("Version")+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("ManifestId", "RevisionNumber");++ b.ToTable("VaultManifestsHistory", (string)null);+ });++ modelBuilder.Entity("AliasVault.WorkerStatus.Database.WorkerServiceStatus", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<string>("CurrentStatus")+ .IsRequired()+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<string>("DesiredStatus")+ .IsRequired()+ .HasMaxLength(50)+ .HasColumnType("character varying(50)");++ b.Property<DateTime>("Heartbeat")+ .HasColumnType("timestamp with time zone");++ b.Property<string>("ServiceName")+ .IsRequired()+ .HasMaxLength(255)+ .HasColumnType("character varying(255)");++ b.HasKey("Id");++ b.ToTable("WorkerServiceStatuses");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.DataProtection.EntityFrameworkCore.DataProtectionKey", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<string>("FriendlyName")+ .HasColumnType("text");++ b.Property<string>("Xml")+ .HasColumnType("text");++ b.HasKey("Id");++ b.ToTable("DataProtectionKeys");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<string>("ClaimType")+ .HasColumnType("text");++ b.Property<string>("ClaimValue")+ .HasColumnType("text");++ b.Property<string>("UserId")+ .HasColumnType("text");++ b.HasKey("Id");++ b.ToTable("UserClaims", (string)null);+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>+ {+ b.Property<string>("UserId")+ .HasColumnType("text");++ b.Property<string>("LoginProvider")+ .HasColumnType("text");++ b.Property<string>("Name")+ .HasColumnType("text");++ b.Property<string>("Value")+ .HasColumnType("text");++ b.HasKey("UserId", "LoginProvider", "Name");++ b.ToTable("UserTokens", (string)null);+ });++ modelBuilder.Entity("AliasServerDb.AliasVaultUser", b =>+ {+ b.HasOne("AliasServerDb.Group", "PersonalGroup")+ .WithOne()+ .HasForeignKey("AliasServerDb.AliasVaultUser", "PersonalGroupId")+ .OnDelete(DeleteBehavior.Restrict)+ .IsRequired();++ b.Navigation("PersonalGroup");+ });++ modelBuilder.Entity("AliasServerDb.AliasVaultUserRefreshToken", b =>+ {+ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("User");+ });++ modelBuilder.Entity("AliasServerDb.EmailAttachment", b =>+ {+ b.HasOne("AliasServerDb.Email", "Email")+ .WithMany("Attachments")+ .HasForeignKey("EmailId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Email");+ });++ modelBuilder.Entity("AliasServerDb.EmailClaimLink", b =>+ {+ b.HasOne("AliasServerDb.EmailClaim", "EmailClaim")+ .WithMany("Links")+ .HasForeignKey("EmailClaimId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("AliasServerDb.VaultManifest", "VaultManifest")+ .WithMany()+ .HasForeignKey("VaultManifestId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("EmailClaim");++ b.Navigation("VaultManifest");+ });++ modelBuilder.Entity("AliasServerDb.EmailKeyWrap", b =>+ {+ b.HasOne("AliasServerDb.Email", "Email")+ .WithMany("Wraps")+ .HasForeignKey("EmailId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("AliasServerDb.VaultManifestDeliveryKey", "EncryptionKey")+ .WithMany("Wraps")+ .HasForeignKey("EncryptionKeyId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Email");++ b.Navigation("EncryptionKey");+ });++ modelBuilder.Entity("AliasServerDb.EmailPart", b =>+ {+ b.HasOne("AliasServerDb.Email", "Email")+ .WithMany("Parts")+ .HasForeignKey("EmailId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Email");+ });++ modelBuilder.Entity("AliasServerDb.GroupMember", b =>+ {+ b.HasOne("AliasServerDb.Group", "Group")+ .WithMany("Members")+ .HasForeignKey("GroupId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Group");++ b.Navigation("User");+ });++ modelBuilder.Entity("AliasServerDb.MobileLoginRequest", b =>+ {+ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade);++ b.Navigation("User");+ });++ modelBuilder.Entity("AliasServerDb.RateLimit", b =>+ {+ b.HasOne("AliasServerDb.Group", "Group")+ .WithMany()+ .HasForeignKey("GroupId")+ .OnDelete(DeleteBehavior.Cascade);++ b.Navigation("Group");+ });++ modelBuilder.Entity("AliasServerDb.UserGrantKey", b =>+ {+ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("User");+ });++ modelBuilder.Entity("AliasServerDb.UserUnlockKey", b =>+ {+ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("User");+ });++ modelBuilder.Entity("AliasServerDb.VaultBlobObject", b =>+ {+ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany()+ .HasForeignKey("OwnerUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("User");+ });++ modelBuilder.Entity("AliasServerDb.VaultBlobReference", b =>+ {+ b.HasOne("AliasServerDb.VaultManifest", "Manifest")+ .WithMany()+ .HasForeignKey("ManifestId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Manifest");+ });++ modelBuilder.Entity("AliasServerDb.VaultDataBucket", b =>+ {+ b.HasOne("AliasServerDb.VaultManifest", "Manifest")+ .WithMany()+ .HasForeignKey("ManifestId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Manifest");+ });++ modelBuilder.Entity("AliasServerDb.VaultDataBucketsHistory", b =>+ {+ b.HasOne("AliasServerDb.VaultDataBucket", "Bucket")+ .WithMany()+ .HasForeignKey("ManifestId", "Category")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Bucket");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifest", b =>+ {+ b.HasOne("AliasServerDb.Group", "OwnerGroup")+ .WithMany()+ .HasForeignKey("OwnerGroupId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("OwnerGroup");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestAccessKey", b =>+ {+ b.HasOne("AliasServerDb.UserGrantKey", "UserGrantKey")+ .WithMany()+ .HasForeignKey("UserGrantKeyId")+ .OnDelete(DeleteBehavior.SetNull);++ b.HasOne("AliasServerDb.AliasVaultUser", "User")+ .WithMany("VaultManifestAccessKeys")+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("User");++ b.Navigation("UserGrantKey");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestDeliveryKey", b =>+ {+ b.HasOne("AliasServerDb.VaultManifest", "VaultManifest")+ .WithMany()+ .HasForeignKey("VaultManifestId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("VaultManifest");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestsHistory", b =>+ {+ b.HasOne("AliasServerDb.VaultManifest", "Manifest")+ .WithMany()+ .HasForeignKey("ManifestId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Manifest");+ });++ modelBuilder.Entity("AliasServerDb.AliasVaultUser", b =>+ {+ b.Navigation("VaultManifestAccessKeys");+ });++ modelBuilder.Entity("AliasServerDb.Email", b =>+ {+ b.Navigation("Attachments");++ b.Navigation("Parts");++ b.Navigation("Wraps");+ });++ modelBuilder.Entity("AliasServerDb.EmailClaim", b =>+ {+ b.Navigation("Links");+ });++ modelBuilder.Entity("AliasServerDb.Group", b =>+ {+ b.Navigation("Members");+ });++ modelBuilder.Entity("AliasServerDb.VaultManifestDeliveryKey", b =>+ {+ b.Navigation("Wraps");+ });+#pragma warning restore 612, 618+ }+ }+}diff --git a/apps/server/Databases/AliasServerDb/Migrations/20260811195539_EmailDetachedParts.cs b/apps/server/Databases/AliasServerDb/Migrations/20260811195539_EmailDetachedParts.csnew file mode 100644index 000000000..c74b6c0e1--- /dev/null+++ b/apps/server/Databases/AliasServerDb/Migrations/20260811195539_EmailDetachedParts.cs@@ -0,0 +1,52 @@+using Microsoft.EntityFrameworkCore.Migrations;+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;++#nullable disable++namespace AliasServerDb.Migrations+{+ /// <inheritdoc />+ public partial class EmailDetachedParts : Migration+ {+ /// <inheritdoc />+ protected override void Up(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.CreateTable(+ name: "EmailParts",+ columns: table => new+ {+ Id = table.Column<int>(type: "integer", nullable: false)+ .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),+ EmailId = table.Column<int>(type: "integer", nullable: false),+ PartIndex = table.Column<int>(type: "integer", nullable: false),+ Bytes = table.Column<byte[]>(type: "bytea", nullable: false)+ },+ constraints: table =>+ {+ table.PrimaryKey("PK_EmailParts", x => x.Id);+ table.ForeignKey(+ name: "FK_EmailParts_Emails_EmailId",+ column: x => x.EmailId,+ principalTable: "Emails",+ principalColumn: "Id",+ onDelete: ReferentialAction.Cascade);+ });++ migrationBuilder.CreateIndex(+ name: "IX_EmailParts_EmailId_PartIndex",+ table: "EmailParts",+ columns: new[] { "EmailId", "PartIndex" },+ unique: true);++ // The part bodies are AES ciphertext which is incompressible, so skip TOAST compression.+ migrationBuilder.Sql(@"ALTER TABLE ""EmailParts"" ALTER COLUMN ""Bytes"" SET STORAGE EXTERNAL;");+ }++ /// <inheritdoc />+ protected override void Down(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.DropTable(+ name: "EmailParts");+ }+ }+}diff --git a/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.cs b/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.csindex 64a252662..f18c82e10 100644--- a/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.cs+++ b/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.cs@@ -518,6 +518,32 @@ namespace AliasServerDb.Migrationsb.ToTable("EmailKeyWraps");});+ modelBuilder.Entity("AliasServerDb.EmailPart", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("integer");++ NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));++ b.Property<byte[]>("Bytes")+ .IsRequired()+ .HasColumnType("bytea");++ b.Property<int>("EmailId")+ .HasColumnType("integer");++ b.Property<int>("PartIndex")+ .HasColumnType("integer");++ b.HasKey("Id");++ b.HasIndex("EmailId", "PartIndex")+ .IsUnique();++ b.ToTable("EmailParts");+ });+modelBuilder.Entity("AliasServerDb.Group", b =>{b.Property<Guid>("Id")@@ -1435,6 +1461,17 @@ namespace AliasServerDb.Migrationsb.Navigation("EncryptionKey");});+ modelBuilder.Entity("AliasServerDb.EmailPart", b =>+ {+ b.HasOne("AliasServerDb.Email", "Email")+ .WithMany("Parts")+ .HasForeignKey("EmailId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Email");+ });+modelBuilder.Entity("AliasServerDb.GroupMember", b =>{b.HasOne("AliasServerDb.Group", "Group")@@ -1600,6 +1637,8 @@ namespace AliasServerDb.Migrations{b.Navigation("Attachments");+ b.Navigation("Parts");+b.Navigation("Wraps");});diff --git a/apps/server/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.cs b/apps/server/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.csindex 92bca5295..79aa05981 100644--- a/apps/server/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.cs+++ b/apps/server/Services/AliasVault.SmtpService/Handlers/DatabaseMessageStore.cs@@ -8,6 +8,7 @@namespace AliasVault.SmtpService.Handlers;using System.Buffers;+using System.Globalization;using System.IO.Compression;using System.Net.Mail;using System.Text.RegularExpressions;@@ -15,6 +16,7 @@ using AliasServerDb;using AliasVault.Cryptography.Server;using Microsoft.EntityFrameworkCore;using MimeKit;+using MimeKit.IO;using NUglify;using SmtpServer;using SmtpServer.Mail;@@ -29,6 +31,24 @@ using SmtpServer.Storage;/// <param name="dbContextFactory">IDbContextFactory instance.</param>public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config config, IAliasServerDbContextFactory dbContextFactory) : MessageStore{+ /// <summary>+ /// Attachment bodies smaller than this are left inline in the message source. Detaching them would trade a+ /// download saving too small to notice for an extra round trip whenever the user opens the attachment.+ /// </summary>+ private const int DetachedPartMinimumSizeInBytes = 64 * 1024;++ /// <summary>+ /// Header stamped on a detached part carrying the index its body is stored and requested under. Clients+ /// read it from the part headers, so the index never depends on how a given MIME parser orders attachments.+ /// </summary>+ private const string DetachedPartIndexHeader = "X-AliasVault-Part";++ /// <summary>+ /// Header stamped on a detached part carrying the decoded size of the attachment, so clients can show the+ /// file size in the attachment list without downloading the body first.+ /// </summary>+ private const string DetachedPartLengthHeader = "X-AliasVault-Detached-Length";+/// <summary>/// Override the SaveAsync method to save the email into the database./// </summary>@@ -51,6 +71,10 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config cvar message = await LoadMessageFromBuffer(buffer, cancellationToken);+ // Detach the large attachments once, up front: this mutates the message into the skeleton that every+ // recipient's copy is serialized from, so it must not run again inside the per-recipient loop.+ var detachedParts = DetachLargeAttachments(message);+// Retrieve all addresses from the SMTP transaction which should contain all recipients for this mail instance.var allAddresses = transaction.To.Distinct()@@ -64,7 +88,7 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config cforeach (var toAddress in toAddresses){// Process the email for each recipient separately.- var process = await ProcessEmailForRecipient(message, toAddress);+ var process = await ProcessEmailForRecipient(message, detachedParts, toAddress);if (!process){toAddressesFailCount++;@@ -112,9 +136,10 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c/// Convert MimeMessage to Email database object./// </summary>/// <param name="message">MimeMessage object.</param>+ /// <param name="detachedParts">The attachment bodies detached from the message source at ingest.</param>/// <param name="toAddress">The recipient for this mail.</param>/// <returns>Email object.</returns>- private static Email ConvertMimeMessageToEmail(MimeMessage message, MailAddress toAddress)+ private static Email ConvertMimeMessageToEmail(MimeMessage message, IReadOnlyCollection<DetachedPart> detachedParts, MailAddress toAddress){var from = string.Empty;@@ -165,6 +190,12 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c// Extract a preview of the email message body to be used in the email listing preview in the UI.email.MessagePreview = ExtractMessagePreview(message.TextBody, message.HtmlBody);+ // Add the detached parts to the email.+ foreach (var part in detachedParts)+ {+ email.Parts.Add(new EmailPart { PartIndex = part.PartIndex, Bytes = part.Bytes });+ }+return email;}@@ -184,9 +215,69 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config creturn output.ToArray();}+ /// <summary>+ /// Gzip-compress a byte array.+ /// </summary>+ /// <param name="bytes">The bytes to compress.</param>+ /// <returns>Gzip-compressed bytes.</returns>+ private static byte[] Compress(byte[] bytes)+ {+ using var output = new MemoryStream();+ using (var gzip = new GZipStream(output, CompressionLevel.Optimal, leaveOpen: true))+ {+ gzip.Write(bytes, 0, bytes.Length);+ }++ return output.ToArray();+ }++ /// <summary>+ /// Move the body of every sizeable attachment out of the message and into a separately stored part, leaving+ /// the message a valid MIME skeleton: the attachment keeps its headers (and therefore its filename and MIME+ /// type, which stay encrypted along with the rest of the source) but carries an empty body plus the two+ /// X-AliasVault-* headers that tell clients where to fetch it and how large it is.+ ///+ /// The captured body is the transfer-encoded form exactly as it appeared in the source, so splicing it back in+ /// is a plain byte insert in case client wants to fully reconstruct the message source.+ /// </summary>+ /// <param name="message">MimeMessage to detach the attachments from. Mutated in place.</param>+ /// <returns>The detached parts, in the order they were stamped.</returns>+ private static List<DetachedPart> DetachLargeAttachments(MimeMessage message)+ {+ var detached = new List<DetachedPart>();+ foreach (var part in message.BodyParts.OfType<MimePart>())+ {+ if (!part.IsAttachment || part.Content is null)+ {+ continue;+ }++ using var encoded = new MemoryStream();+ part.Content.WriteTo(encoded);+ if (encoded.Length < DetachedPartMinimumSizeInBytes)+ {+ continue;+ }++ using var measuring = new MeasuringStream();+ part.Content.DecodeTo(measuring);++ var partIndex = detached.Count;+ part.Headers[DetachedPartIndexHeader] = partIndex.ToString(CultureInfo.InvariantCulture);+ part.Headers[DetachedPartLengthHeader] = measuring.Length.ToString(CultureInfo.InvariantCulture);++ var encoding = part.Content.Encoding;+ part.Content = new MimeContent(new MemoryStream([], false), encoding);++ detached.Add(new DetachedPart(partIndex, Compress(encoded.ToArray())));+ }++ return detached;+ }+/// <summary>/// Extracts a preview of the email message body to be used in the email listing preview in the UI.- /// This so the client does not need to load the full email body.+ /// This so the client does not need to load the full email body when rendering a list./// </summary>/// <param name="messagePlain">The parsed plain text body of the message, if any.</param>/// <param name="messageHtml">The parsed HTML body of the message, if any.</param>@@ -260,9 +351,10 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c/// Process email for recipient separately./// </summary>/// <param name="message">MimeMessage.</param>+ /// <param name="detachedParts">The attachment bodies detached from the message source at ingest.</param>/// <param name="toAddress">ToAddress.</param>/// <returns>True if success or silent skip, false if SmtpResponse.NoValidRecipientsGiven should be triggered.</returns>- private async Task<bool> ProcessEmailForRecipient(MimeMessage message, IMailbox? toAddress)+ private async Task<bool> ProcessEmailForRecipient(MimeMessage message, IReadOnlyCollection<DetachedPart> detachedParts, IMailbox? toAddress){// Check if toAddress domain is allowed.if (toAddress is null ||@@ -301,8 +393,7 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config cvar linkedManifestIds = await dbContext.EmailClaimLinks.Where(l => l.EmailClaimId == emailClaim.Id).Select(l => l.VaultManifestId).ToListAsync(CancellationToken.None);if (linkedManifestIds.Count == 0){- // The claim is orphaned: every manifest it was linked to no longer exists (owner deleted account),- // leaving the claim as a tombstone record that is designed to block re-use of the address. We cannot process this email.+ // The claim is orphaned: every manifest it was linked to no longer exists (owner deleted account).logger.LogInformation("Rejected email: email for {ToAddress} is claimed but its owning vault no longer exists. The owner has most likely deleted their account.",toAddress.User + "@" + toAddress.Host);@@ -319,8 +410,7 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config creturn false;}- // Resolve every linked manifest's primary delivery key. A manifest without one cannot receive this mail- // (its members simply get no wrap); reject only when no wrap at all is possible.+ // Resolve every linked manifest's primary delivery key.var deliveryKeys = await dbContext.VaultManifestDeliveryKeys.Where(x => linkedManifestIds.Contains(x.VaultManifestId) && x.IsPrimary).ToListAsync(CancellationToken.None);foreach (var keylessManifestId in linkedManifestIds.Except(deliveryKeys.Select(k => k.VaultManifestId))){@@ -340,7 +430,7 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config cvar wrappedManifestIds = deliveryKeys.Select(k => k.VaultManifestId).ToList();var recipientGroupIds = await dbContext.VaultManifests.Where(m => wrappedManifestIds.Contains(m.ManifestId)).Select(m => m.OwnerGroupId).Distinct().ToListAsync(CancellationToken.None);- var insertedId = await InsertEmailIntoDatabase(message, new MailAddress(toAddress.AsAddress()), deliveryKeys, recipientGroupIds);+ var insertedId = await InsertEmailIntoDatabase(message, detachedParts, new MailAddress(toAddress.AsAddress()), deliveryKeys, recipientGroupIds);logger.LogDebug("Email for {ToAddress} successfully saved into database with ID {InsertedId}.", toAddress.User + "@" + toAddress.Host, insertedId);return true;}@@ -349,14 +439,15 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config c/// Insert email into database./// </summary>/// <param name="message">MimeMessage to save into database.</param>+ /// <param name="detachedParts">The attachment bodies detached from the message source at ingest.</param>/// <param name="toAddress">The recipient for this mail.</param>/// <param name="deliveryKeys">The delivery keys of every manifest claiming this alias; each gets its own wrap of the email's symmetric key.</param>/// <param name="recipientGroupIds">The groups that own the manifests this alias is claimed by.</param>- private async Task<int> InsertEmailIntoDatabase(MimeMessage message, MailAddress toAddress, IReadOnlyCollection<VaultManifestDeliveryKey> deliveryKeys, IReadOnlyCollection<Guid> recipientGroupIds)+ private async Task<int> InsertEmailIntoDatabase(MimeMessage message, IReadOnlyCollection<DetachedPart> detachedParts, MailAddress toAddress, IReadOnlyCollection<VaultManifestDeliveryKey> deliveryKeys, IReadOnlyCollection<Guid> recipientGroupIds){await using var dbContext = await dbContextFactory.CreateDbContextAsync();- var newEmail = ConvertMimeMessageToEmail(message, toAddress);+ var newEmail = ConvertMimeMessageToEmail(message, detachedParts, toAddress);newEmail = EmailEncryption.EncryptEmail(newEmail, deliveryKeys);// Insert the email into the database.@@ -373,4 +464,12 @@ public class DatabaseMessageStore(ILogger<DatabaseMessageStore> logger, Config creturn newEmail.Id;}++ /// <summary>+ /// An attachment body lifted out of the message source at ingest, ready to be stored as an EmailPart row+ /// for every recipient of the message.+ /// </summary>+ /// <param name="PartIndex">The index stamped on the part in the source as the X-AliasVault-Part header.</param>+ /// <param name="Bytes">The transfer-encoded part body, gzip-compressed but not yet encrypted.</param>+ private sealed record DetachedPart(int PartIndex, byte[] Bytes);}diff --git a/apps/server/Services/AliasVault.SmtpService/Scripts/sendEmailCLI.sh b/apps/server/Services/AliasVault.SmtpService/Scripts/sendEmailCLI.shindex ba6462ab0..c529c57bb 100755--- a/apps/server/Services/AliasVault.SmtpService/Scripts/sendEmailCLI.sh+++ b/apps/server/Services/AliasVault.SmtpService/Scripts/sendEmailCLI.sh@@ -261,6 +261,8 @@ send_email() {local boundary="boundary-$(generate_random_string 16)"local attachment_content="This is a test attachment content - $(generate_random_string 32)"local attachment_name="test_attachment_$(generate_random_string 8).txt"+ local large_attachment_name="test_large_attachment_$(generate_random_string 8).bin"s+ local large_attachment_size=$((80 * 1024)) # >64KB to test multipart/mixed attachment storage path{generate_headers "$recipient" "$subject" "" "$boundary"@@ -279,7 +281,7 @@ send_email() {printf "\r\n"- # Attachment part+ # Attachment part 1: small text attachment (stored inline)printf -- "--%s\r\n" "$boundary"printf "Content-Type: application/octet-stream\r\n"printf "Content-Transfer-Encoding: base64\r\n"@@ -287,6 +289,15 @@ send_email() {printf "\r\n"echo "$attachment_content" | base64printf "\r\n"++ # Attachment part 2: large random binary attachment (stored out-of-bounds)+ printf -- "--%s\r\n" "$boundary"+ printf "Content-Type: application/octet-stream\r\n"+ printf "Content-Transfer-Encoding: base64\r\n"+ printf "Content-Disposition: attachment; filename=\"%s\"\r\n" "$large_attachment_name"+ printf "\r\n"+ head -c "$large_attachment_size" /dev/urandom | base64 | fold -w 76+ printf "\r\n"printf -- "--%s--\r\n" "$boundary"} | curl --url "smtp://localhost:$smtp_port" \--mail-from "[email protected]" \diff --git a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.csindex f53b4d5ad..52e2aee24 100644--- a/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs+++ b/apps/server/Tests/AliasVault.IntegrationTests/SmtpServer/SmtpServerTests.cs@@ -505,6 +505,173 @@ public class SmtpServerTests});}+ /// <summary>+ /// Tests that a sizeable attachment is detached from the message source at ingest.+ /// </summary>+ /// <returns>Task.</returns>+ [Test]+ public async Task LargeAttachmentIsDetachedFromSource()+ {+ var message = new MimeMessage();+ message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));+ message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));+ message.Subject = "Test Email with large attachment";++ // Random bytes well over the 64 KB detach floor, so the payload cannot compress away either.+ var attachmentData = new byte[128 * 1024];+ Random.Shared.NextBytes(attachmentData);++ var bodyBuilder = new BodyBuilder { TextBody = "This is a test email with a large attachment." };+ bodyBuilder.Attachments.Add("large.bin", attachmentData, ContentType.Parse("application/octet-stream"));+ message.Body = bodyBuilder.ToMessageBody();++ await SendMessageToSmtpServer(message);++ var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Parts).Include(x => x.Wraps).FirstAsync();+ var storedPart = processedEmail.Parts.Single();+ Assert.Multiple(() =>+ {+ Assert.That(processedEmail.AttachmentCount, Is.EqualTo(1));+ Assert.That(storedPart.PartIndex, Is.Zero);+ Assert.That(storedPart.Bytes, Is.Not.Empty);+ });++ // The whole point of detaching: the source a client downloads to read the mail no longer carries the payload.+ Assert.That(processedEmail.MessageSourceBytes!.Length, Is.LessThan(attachmentData.Length / 4), "The stored source should no longer carry the attachment payload.");++ processedEmail = EmailEncryption.DecryptEmail(processedEmail, PrivateKey);+ var sourceBytes = Gunzip(processedEmail.MessageSourceBytes!);+ var messageSource = Encoding.UTF8.GetString(sourceBytes);+ Assert.Multiple(() =>+ {+ Assert.That(messageSource, Does.Contain("X-AliasVault-Part: 0"), "The detached attachment should advertise the index its body is stored under.");+ Assert.That(messageSource, Does.Contain($"X-AliasVault-Detached-Length: {attachmentData.Length}"), "The detached attachment should advertise its decoded size.");+ Assert.That(messageSource, Does.Contain("filename=large.bin"), "The attachment headers must stay in the source so clients can still list it.");+ });++ // The skeleton has to remain a parseable MIME message with the attachment present but empty.+ using var sourceStream = new MemoryStream(sourceBytes);+ var parsedMessage = await MimeMessage.LoadAsync(sourceStream);+ var parsedAttachment = parsedMessage.Attachments.OfType<MimePart>().Single();+ using var emptyBody = new MemoryStream();+ await parsedAttachment.Content!.DecodeToAsync(emptyBody);+ Assert.Multiple(() =>+ {+ Assert.That(parsedAttachment.FileName, Is.EqualTo("large.bin"));+ Assert.That(emptyBody.ToArray(), Is.Empty, "The detached attachment should have an empty body in the source.");+ });++ // Splicing the stored part back where its body was must reproduce the original file byte for byte.+ var detachedBody = Gunzip(processedEmail.Parts.Single().Bytes);+ var spliced = SpliceDetachedPart(sourceBytes, detachedBody);+ using var splicedStream = new MemoryStream(spliced);+ var splicedMessage = await MimeMessage.LoadAsync(splicedStream);+ using var decodedAttachment = new MemoryStream();+ await splicedMessage.Attachments.OfType<MimePart>().Single().Content!.DecodeToAsync(decodedAttachment);+ Assert.That(decodedAttachment.ToArray(), Is.EqualTo(attachmentData), "The spliced attachment should decode to the original bytes.");+ }++ /// <summary>+ /// Tests that every recipient of a multi-recipient email gets a usable copy of a detached attachment.+ /// Detaching lifts the body out of the shared MimeMessage, so doing it per recipient rather than once up+ /// front would leave every copy after the first with an empty part.+ /// </summary>+ /// <returns>Task.</returns>+ [Test]+ public async Task DetachedAttachmentIsStoredForEveryRecipient()+ {+ var message = new MimeMessage();+ message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));+ message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));+ message.Cc.Add(new MailboxAddress("Test Recipient 2", "[email protected]"));+ message.Subject = "Test Email with large attachment for multiple recipients";++ var attachmentData = new byte[128 * 1024];+ Random.Shared.NextBytes(attachmentData);++ var bodyBuilder = new BodyBuilder { TextBody = "This is a test email with a large attachment." };+ bodyBuilder.Attachments.Add("large.bin", attachmentData, ContentType.Parse("application/octet-stream"));+ message.Body = bodyBuilder.ToMessageBody();++ await SendMessageToSmtpServer(message);++ var emails = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Parts).Include(x => x.Wraps).ToListAsync();+ Assert.That(emails, Has.Count.EqualTo(2));++ foreach (var email in emails)+ {+ var decrypted = EmailEncryption.DecryptEmail(email, PrivateKey);+ var detachedBody = Gunzip(decrypted.Parts.Single().Bytes);+ var spliced = SpliceDetachedPart(Gunzip(decrypted.MessageSourceBytes!), detachedBody);++ using var splicedStream = new MemoryStream(spliced);+ var splicedMessage = await MimeMessage.LoadAsync(splicedStream);+ using var decodedAttachment = new MemoryStream();+ await splicedMessage.Attachments.OfType<MimePart>().Single().Content!.DecodeToAsync(decodedAttachment);++ Assert.That(decodedAttachment.ToArray(), Is.EqualTo(attachmentData), $"Recipient {email.To} did not get a usable copy of the detached attachment.");+ }+ }++ /// <summary>+ /// Tests that an attachment below the detach floor is left inline: detaching it would cost an extra round+ /// trip on download for a saving too small to matter.+ /// </summary>+ /// <returns>Task.</returns>+ [Test]+ public async Task SmallAttachmentStaysInsideSource()+ {+ var message = new MimeMessage();+ message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));+ message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));+ message.Subject = "Test Email with small attachment";++ var bodyBuilder = new BodyBuilder { TextBody = "This is a test email with a small attachment." };+ bodyBuilder.Attachments.Add("small.txt", Encoding.UTF8.GetBytes("This is a small attachment."), ContentType.Parse("text/plain"));+ message.Body = bodyBuilder.ToMessageBody();++ await SendMessageToSmtpServer(message);++ var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Parts).FirstAsync();+ Assert.Multiple(() =>+ {+ Assert.That(processedEmail.Parts, Is.Empty, "An attachment below the detach floor should stay inside the source.");+ Assert.That(processedEmail.AttachmentCount, Is.EqualTo(1));+ });+ }++ /// <summary>+ /// Splices a detached part body back into the empty body of the attachment it was lifted out of, mirroring+ /// what the Rust core does client-side.+ /// </summary>+ /// <param name="skeleton">The raw MIME source with the attachment body removed.</param>+ /// <param name="body">The transfer-encoded attachment body to splice back in.</param>+ /// <returns>The reassembled raw MIME source.</returns>+ private static byte[] SpliceDetachedPart(byte[] skeleton, byte[] body)+ {+ // The detached part's body sits between the blank line that ends its headers and the next boundary.+ var text = Encoding.ASCII.GetString(skeleton);+ var headerIndex = text.IndexOf("X-AliasVault-Detached-Length:", StringComparison.Ordinal);+ Assert.That(headerIndex, Is.GreaterThan(0), "Skeleton does not contain a detached part.");++ var separator = text.IndexOf("\r\n\r\n", headerIndex, StringComparison.Ordinal);+ var separatorLength = 4;+ if (separator == -1)+ {+ separator = text.IndexOf("\n\n", headerIndex, StringComparison.Ordinal);+ separatorLength = 2;+ }++ Assert.That(separator, Is.GreaterThan(0), "Detached part has no header/body separator.");+ var bodyStart = separator + separatorLength;++ var spliced = new byte[skeleton.Length + body.Length];+ Array.Copy(skeleton, 0, spliced, 0, bodyStart);+ Array.Copy(body, 0, spliced, bodyStart, body.Length);+ Array.Copy(skeleton, bodyStart, spliced, bodyStart + body.Length, skeleton.Length - bodyStart);+ return spliced;+ }+/// <summary>/// Sends a message to the SMTP server with retry logic for connection./// </summary>diff --git a/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Server/EmailEncryption.cs b/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Server/EmailEncryption.csindex 3fc75d856..24a3779e1 100644--- a/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Server/EmailEncryption.cs+++ b/apps/server/Utilities/Cryptography/AliasVault.Cryptography.Server/EmailEncryption.cs@@ -62,6 +62,12 @@ public static class EmailEncryptionattachment.Bytes = Encryption.SymmetricEncrypt(attachment.Bytes, symmetricKey);}+ // Encrypt the attachment bodies that were detached from the source with the same symmetric key.+ foreach (var part in email.Parts)+ {+ part.Bytes = Encryption.SymmetricEncrypt(part.Bytes, symmetricKey);+ }+// Wrap the same symmetric key once per recipient manifest's delivery key.foreach (var deliveryKey in deliveryKeys){@@ -132,6 +138,12 @@ public static class EmailEncryptionemail.FromLocal = Encryption.SymmetricDecrypt(email.FromLocal, symmetricKey);email.FromDomain = Encryption.SymmetricDecrypt(email.FromDomain, symmetricKey);+ // Decrypt the attachment bodies that were detached from the source.+ foreach (var part in email.Parts)+ {+ part.Bytes = Encryption.SymmetricDecrypt(part.Bytes, symmetricKey);+ }+return email;}}diff --git a/core/rust/src/email_parser/mod.rs b/core/rust/src/email_parser/mod.rsindex 60f7ce21f..3551e59b6 100644--- a/core/rust/src/email_parser/mod.rs+++ b/core/rust/src/email_parser/mod.rs@@ -1,10 +1,19 @@//! Email MIME parsing for source-only stored emails.-use mail_parser::{MessageParser, MimeHeaders};+use mail_parser::{GetHeader, Message, MessageParser, MessagePart, MimeHeaders};use serde::Serialize;use std::io::Read;use crate::error::{VaultError, VaultResult};+/// Header the server stamps on an attachment whose body it detached from the source at ingest, carrying the+/// index that body is stored and requested under. Reading the index from the message itself keeps it+/// independent of how any given MIME parser happens to order a message's attachments.+const DETACHED_PART_INDEX_HEADER: &str = "X-AliasVault-Part";++/// Header the server stamps on a detached attachment carrying its decoded size, so the size can be shown in+/// the attachment list without downloading the body.+const DETACHED_PART_LENGTH_HEADER: &str = "X-AliasVault-Detached-Length";+/// A single attachment of a parsed email message. Metadata only: fetch the bytes with/// [`extract_email_attachment`] using this attachment's index in [`ParsedEmail::attachments`].#[derive(Debug, Clone, Serialize)]@@ -14,6 +23,12 @@ pub struct ParsedEmailAttachment {pub mime_type: String,/// The decoded attachment size in bytes.pub size: u64,+ /// Whether this attachment's body lives outside the message source and has to be fetched separately+ /// before [`extract_email_attachment`] can return its bytes.+ pub detached: bool,+ /// The index the detached body is stored under, to request it by. `None` for an attachment whose body+ /// is still inline in the source.+ pub part_index: Option<u32>,}/// The result of parsing a raw RFC 822 email source.@@ -42,16 +57,22 @@ pub fn parse_email_source(source: &[u8]) -> VaultResult<ParsedEmail> {let attachments = message.attachments()- .map(|part| ParsedEmailAttachment {- filename: part.attachment_name().unwrap_or_default().to_string(),- mime_type: part- .content_type()- .map(|content_type| match content_type.subtype() {- Some(subtype) => format!("{}/{}", content_type.ctype(), subtype),- None => content_type.ctype().to_string(),- })- .unwrap_or_else(|| "application/octet-stream".to_string()),- size: part.contents().len() as u64,+ .map(|part| {+ let part_index = detached_part_index(part);+ ParsedEmailAttachment {+ filename: part.attachment_name().unwrap_or_default().to_string(),+ mime_type: part+ .content_type()+ .map(|content_type| match content_type.subtype() {+ Some(subtype) => format!("{}/{}", content_type.ctype(), subtype),+ None => content_type.ctype().to_string(),+ })+ .unwrap_or_else(|| "application/octet-stream".to_string()),+ // The real size of a detached attachment comes from the header the server stamped on it.+ size: header_u64(part, DETACHED_PART_LENGTH_HEADER).unwrap_or_else(|| part.contents().len() as u64),+ detached: part_index.is_some(),+ part_index,+ }}).collect();@@ -68,19 +89,38 @@ pub fn decode_email_source(source: &[u8]) -> VaultResult<Vec<u8>> {}/// Extract the decoded bytes of a single attachment, identified by its index in-/// [`ParsedEmail::attachments`]. Only that attachment's bytes are returned, so downloading one-/// attachment never marshals the whole message.-pub fn extract_email_attachment(source: &[u8], index: usize) -> VaultResult<Vec<u8>> {+/// [`ParsedEmail::attachments`].+pub fn extract_email_attachment(source: &[u8], index: usize, detached_body: Option<&[u8]>) -> VaultResult<Vec<u8>> {let raw = decode_email_source(source)?;- let message = MessageParser::default()- .parse(raw.as_slice())- .ok_or_else(|| VaultError::General("Failed to parse email source as a MIME message".to_string()))?;+ // Resolve what the attachment needs before splicing: the parsed message borrows `raw`, so nothing that+ // borrows it may outlive this block.+ let body_range = {+ let message = parse_message(&raw)?;+ let part = attachment_at(&message, index)?;++ match (detached_part_index(part).is_some(), detached_body) {+ (false, _) => return Ok(part.contents().to_vec()),+ (true, None) => {+ return Err(VaultError::General(format!(+ "Attachment at index {} was detached from the source at ingest; its body has to be fetched separately",+ index+ )))+ }+ (true, Some(_)) => part.offset_body as usize..part.offset_end as usize,+ }+ };++ let body = decode_detached_part(detached_body.unwrap_or_default())?;+ let mut spliced = Vec::with_capacity(raw.len() + body.len());+ spliced.extend_from_slice(&raw[..body_range.start]);+ spliced.extend_from_slice(&body);+ spliced.extend_from_slice(&raw[body_range.end..]);- // Bind before returning: the attachments iterator borrows `message`, which owns the parsed source.- let extracted = message.attachments().nth(index).map(|part| part.contents().to_vec());+ let message = parse_message(&spliced)?;+ let extracted = attachment_at(&message, index)?.contents().to_vec();- extracted.ok_or_else(|| VaultError::General(format!("Email has no attachment at index {}", index)))+ Ok(extracted)}/// Parse a raw RFC 822 email source and return the result as a JSON string (for uniffi/ffi callers).@@ -89,6 +129,42 @@ pub fn parse_email_source_json(source: &[u8]) -> VaultResult<String> {serde_json::to_string(&parsed).map_err(VaultError::from)}+/// Parse raw RFC 822 bytes into a message, turning the parser's `None` into a proper error.+fn parse_message(raw: &[u8]) -> VaultResult<Message<'_>> {+ MessageParser::default()+ .parse(raw)+ .ok_or_else(|| VaultError::General("Failed to parse email source as a MIME message".to_string()))+}++/// Look up an attachment by its index in the message's attachment list.+fn attachment_at<'x>(message: &'x Message<'x>, index: usize) -> VaultResult<&'x MessagePart<'x>> {+ message+ .attachments()+ .nth(index)+ .ok_or_else(|| VaultError::General(format!("Email has no attachment at index {}", index)))+}++/// Read the index a detached attachment's body is stored under, or `None` when the body is still inline.+fn detached_part_index(part: &MessagePart<'_>) -> Option<u32> {+ header_u64(part, DETACHED_PART_INDEX_HEADER).and_then(|index| u32::try_from(index).ok())+}++/// Read a header of a part as an unsigned number, ignoring it when absent or not numeric.+fn header_u64(part: &MessagePart<'_>, name: &str) -> Option<u64> {+ part.headers+ .header(name)+ .and_then(|header| header.value().as_text())+ .and_then(|value| value.trim().parse::<u64>().ok())+}++/// Decompress a stored detached attachment body. Unlike the message source there is no legacy encoding to+/// repair: detached parts only exist in the source-only storage format.+fn decode_detached_part(body: &[u8]) -> VaultResult<Vec<u8>> {+ Ok(match decompress_if_gzip(body)? {+ MaybeDecompressed::Decompressed(bytes) | MaybeDecompressed::AsIs(bytes) => bytes,+ })+}+/// The outcome of the gzip check, which also tells the caller which storage format the source came from.enum MaybeDecompressed {Decompressed(Vec<u8>),diff --git a/core/rust/src/email_parser/tests.rs b/core/rust/src/email_parser/tests.rsindex 2eee08c8b..1d4c54ab4 100644--- a/core/rust/src/email_parser/tests.rs+++ b/core/rust/src/email_parser/tests.rs@@ -77,7 +77,7 @@ fn parses_multipart_email_with_attachment() {assert_eq!(attachment.filename, "report.pdf");assert_eq!(attachment.mime_type, "application/pdf");assert_eq!(attachment.size, 10);- assert_eq!(extract_email_attachment(MULTIPART_EMAIL.as_bytes(), 0).unwrap(), b"%PDF-hello");+ assert_eq!(extract_email_attachment(MULTIPART_EMAIL.as_bytes(), 0, None).unwrap(), b"%PDF-hello");}#[test]@@ -137,7 +137,7 @@ Prijs: 10€\r\n\let parsed = parse_email_source(&legacy_double_encode(email.as_bytes())).unwrap();assert_eq!(parsed.attachments.len(), 1);- let content = extract_email_attachment(&legacy_double_encode(email.as_bytes()), 0).unwrap();+ let content = extract_email_attachment(&legacy_double_encode(email.as_bytes()), 0, None).unwrap();assert_eq!(String::from_utf8(content).unwrap().trim_end(), "Prijs: 10€");}@@ -204,16 +204,16 @@ c2Vjb25k\r\n\let parsed = parse_email_source(email.as_bytes()).unwrap();assert_eq!(parsed.attachments.iter().map(|a| a.filename.as_str()).collect::<Vec<_>>(), ["first.bin", "second.bin"]);- assert_eq!(extract_email_attachment(email.as_bytes(), 0).unwrap(), b"first");- assert_eq!(extract_email_attachment(email.as_bytes(), 1).unwrap(), b"second");- assert!(extract_email_attachment(email.as_bytes(), 2).is_err(), "an out of range index must error, not return empty bytes");+ assert_eq!(extract_email_attachment(email.as_bytes(), 0, None).unwrap(), b"first");+ assert_eq!(extract_email_attachment(email.as_bytes(), 1, None).unwrap(), b"second");+ assert!(extract_email_attachment(email.as_bytes(), 2, None).is_err(), "an out of range index must error, not return empty bytes");}#[test]fn extracts_attachments_from_compressed_source() {let compressed = gzip(MULTIPART_EMAIL.as_bytes());- assert_eq!(extract_email_attachment(&compressed, 0).unwrap(), b"%PDF-hello");+ assert_eq!(extract_email_attachment(&compressed, 0, None).unwrap(), b"%PDF-hello");}#[test]diff --git a/core/rust/src/uniffi_api.rs b/core/rust/src/uniffi_api.rsindex 352573e60..4181f4540 100644--- a/core/rust/src/uniffi_api.rs+++ b/core/rust/src/uniffi_api.rs@@ -190,7 +190,7 @@ pub fn get_identity_languages() -> Vec<String> {}/// Parse a raw RFC 822 email source into its html/plain bodies and attachment metadata, returned as-/// a JSON string (`{htmlBody, textBody, attachments: [{filename, mimeType, size}]}`). Input that+/// a JSON string (`{htmlBody, textBody, attachments: [{filename, mimeType, size, detached, partIndex}]}`). Input that/// starts with the gzip magic bytes (0x1f 0x8b) is gunzipped, so the decrypted/// `MessageSource` of both legacy and source-only emails can be passed as-is.#[uniffi::export]@@ -206,8 +206,8 @@ pub fn decode_email_source(source: Vec<u8>) -> Result<Vec<u8>, VaultError> {/// Extract the decoded bytes of one attachment, identified by its index in the parsed attachment list.#[uniffi::export]-pub fn extract_email_attachment(source: Vec<u8>, index: u32) -> Result<Vec<u8>, VaultError> {- crate::email_parser::extract_email_attachment(&source, index as usize)+pub fn extract_email_attachment(source: Vec<u8>, index: u32, detached_body: Option<Vec<u8>>) -> Result<Vec<u8>, VaultError> {+ crate::email_parser::extract_email_attachment(&source, index as usize, detached_body.as_deref())}/// Get the list of age range option values ("random" plus 5-year ranges).