Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add block timestamps (#2131)
Leendert de Borstb6a1880769c3
diff --git a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razorindex e7a9317ca..1fd0b24a4 100644--- a/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor+++ b/apps/server/AliasVault.Admin/Main/Pages/Users/View/Index.razor@@ -163,6 +163,10 @@ else<td class="px-4 py-3"><div class="flex items-center space-x-2"><StatusPill Enabled="@(!User.Blocked)" TextTrue="Active" TextFalse="Blocked" />+ @if (User.Blocked && User.BlockedAt is not null)+ {+ <span class="text-sm text-gray-500 dark:text-gray-400">since @User.BlockedAt.Value.ToString("yyyy-MM-dd HH:mm") UTC</span>+ }<Button Color="@(User.Blocked ? "success" : "danger")" OnClick="ToggleBlockStatus">@(User.Blocked ? "Unblock User" : "Block User")</Button>@@ -546,6 +550,16 @@ Do you want to proceed with the restoration?")) {{User.Blocked = !User.Blocked;+ // Record when the block was activated; clear the trace again when the user is unblocked.+ if (User.Blocked)+ {+ User.BlockedAt = DateTime.UtcNow;+ }+ else+ {+ User.BlockedAt = null;+ }+// If user is unblocked by the admin, also reset any lockout status, which can be// automatically triggered by the system when user has entered an incorrect password too many times.if (!User.Blocked) {diff --git a/apps/server/Databases/AliasServerDb/AliasVaultUser.cs b/apps/server/Databases/AliasServerDb/AliasVaultUser.csindex be0a825a9..f98c0fbdb 100644--- a/apps/server/Databases/AliasServerDb/AliasVaultUser.cs+++ b/apps/server/Databases/AliasServerDb/AliasVaultUser.cs@@ -36,11 +36,24 @@ public class AliasVaultUser : IdentityUser/// </summary>public bool Blocked { get; set; }+ /// <summary>+ /// Gets or sets the UTC timestamp when the user was last blocked. Null when the user has never been blocked.+ /// Kept as a small trace of when the block was activated.+ /// </summary>+ public DateTime? BlockedAt { get; set; }+/// <summary>/// Gets or sets a value indicating whether the user is marked as shadow-blocked./// </summary>public bool ShadowBlocked { get; set; }+ /// <summary>+ /// Gets or sets the UTC timestamp when the user was shadow-blocked. Used to only hide emails received after the+ /// block occurred. Null when the user has never been shadow-blocked (in which case all emails are hidden while+ /// ShadowBlocked is true, as a conservative fallback).+ /// </summary>+ public DateTime? ShadowBlockedAt { get; set; }+/// <summary>/// Gets or sets updated timestamp./// </summary>diff --git a/apps/server/Databases/AliasServerDb/Migrations/20260604174903_AddIpBlocklistAndShadowBlock.Designer.cs b/apps/server/Databases/AliasServerDb/Migrations/20260604183059_AddIpBlocklistAndShadowBlock.Designer.cssimilarity index 99%rename from apps/server/Databases/AliasServerDb/Migrations/20260604174903_AddIpBlocklistAndShadowBlock.Designer.csrename to apps/server/Databases/AliasServerDb/Migrations/20260604183059_AddIpBlocklistAndShadowBlock.Designer.csindex c2510302c..25c3ffd3c 100644--- a/apps/server/Databases/AliasServerDb/Migrations/20260604174903_AddIpBlocklistAndShadowBlock.Designer.cs+++ b/apps/server/Databases/AliasServerDb/Migrations/20260604183059_AddIpBlocklistAndShadowBlock.Designer.cs@@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;namespace AliasServerDb.Migrations{[DbContext(typeof(AliasServerDbContext))]- [Migration("20260604174903_AddIpBlocklistAndShadowBlock")]+ [Migration("20260604183059_AddIpBlocklistAndShadowBlock")]partial class AddIpBlocklistAndShadowBlock{/// <inheritdoc />@@ -132,6 +132,9 @@ namespace AliasServerDb.Migrationsb.Property<bool>("Blocked").HasColumnType("boolean");+ b.Property<DateTime?>("BlockedAt")+ .HasColumnType("timestamp with time zone");+b.Property<string>("ConcurrencyStamp").HasColumnType("text");@@ -186,6 +189,9 @@ namespace AliasServerDb.Migrationsb.Property<bool>("ShadowBlocked").HasColumnType("boolean");+ b.Property<DateTime?>("ShadowBlockedAt")+ .HasColumnType("timestamp with time zone");+b.Property<string>("SrpIdentity").HasMaxLength(255).HasColumnType("character varying(255)");diff --git a/apps/server/Databases/AliasServerDb/Migrations/20260604174903_AddIpBlocklistAndShadowBlock.cs b/apps/server/Databases/AliasServerDb/Migrations/20260604183059_AddIpBlocklistAndShadowBlock.cssimilarity index 80%rename from apps/server/Databases/AliasServerDb/Migrations/20260604174903_AddIpBlocklistAndShadowBlock.csrename to apps/server/Databases/AliasServerDb/Migrations/20260604183059_AddIpBlocklistAndShadowBlock.csindex 83b4a6559..dd5d9fa79 100644--- a/apps/server/Databases/AliasServerDb/Migrations/20260604174903_AddIpBlocklistAndShadowBlock.cs+++ b/apps/server/Databases/AliasServerDb/Migrations/20260604183059_AddIpBlocklistAndShadowBlock.cs@@ -12,6 +12,12 @@ namespace AliasServerDb.Migrations/// <inheritdoc />protected override void Up(MigrationBuilder migrationBuilder){+ migrationBuilder.AddColumn<DateTime>(+ name: "BlockedAt",+ table: "AliasVaultUsers",+ type: "timestamp with time zone",+ nullable: true);+migrationBuilder.AddColumn<bool>(name: "ShadowBlocked",table: "AliasVaultUsers",@@ -19,6 +25,12 @@ namespace AliasServerDb.Migrationsnullable: false,defaultValue: false);+ migrationBuilder.AddColumn<DateTime>(+ name: "ShadowBlockedAt",+ table: "AliasVaultUsers",+ type: "timestamp with time zone",+ nullable: true);+migrationBuilder.CreateTable(name: "BlockedIpRanges",columns: table => new@@ -58,9 +70,17 @@ namespace AliasServerDb.MigrationsmigrationBuilder.DropTable(name: "BlockedIpRanges");+ migrationBuilder.DropColumn(+ name: "BlockedAt",+ table: "AliasVaultUsers");+migrationBuilder.DropColumn(name: "ShadowBlocked",table: "AliasVaultUsers");++ migrationBuilder.DropColumn(+ name: "ShadowBlockedAt",+ table: "AliasVaultUsers");}}}diff --git a/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.cs b/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.csindex 450046991..2518f4688 100644--- a/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.cs+++ b/apps/server/Databases/AliasServerDb/Migrations/AliasServerDbContextModelSnapshot.cs@@ -129,6 +129,9 @@ namespace AliasServerDb.Migrationsb.Property<bool>("Blocked").HasColumnType("boolean");+ b.Property<DateTime?>("BlockedAt")+ .HasColumnType("timestamp with time zone");+b.Property<string>("ConcurrencyStamp").HasColumnType("text");@@ -183,6 +186,9 @@ namespace AliasServerDb.Migrationsb.Property<bool>("ShadowBlocked").HasColumnType("boolean");+ b.Property<DateTime?>("ShadowBlockedAt")+ .HasColumnType("timestamp with time zone");+b.Property<string>("SrpIdentity").HasMaxLength(255).HasColumnType("character varying(255)");diff --git a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockEvaluator.cs b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockEvaluator.csindex 83565677b..43ef60c62 100644--- a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockEvaluator.cs+++ b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockEvaluator.cs@@ -49,6 +49,41 @@ public static class IpBlockEvaluatorreturn false;}+ /// <summary>+ /// Returns the earliest blocked created at among all ranges that both cover the given action+ /// and contain the given address. Used for time-windowed shadow-blocking.+ /// </summary>+ /// <param name="ranges">The candidate blocklist ranges.</param>+ /// <param name="address">The IP address to evaluate.</param>+ /// <param name="action">The action to evaluate.</param>+ /// <returns>The earliest matching block creation time, or null when no range matches.</returns>+ public static DateTime? GetEarliestMatchingBlockTime(IEnumerable<BlockedIpRange> ranges, IPAddress? address, IpBlockAction action)+ {+ if (address is null)+ {+ return null;+ }++ DateTime? earliest = null;+ foreach (var range in ranges)+ {+ if (!HasFlag(range, action))+ {+ continue;+ }++ if (IpRangeUtility.TryParse(range.IpRange, out var network) && IpRangeUtility.Contains(network, address))+ {+ if (earliest is null || range.CreatedAt < earliest)+ {+ earliest = range.CreatedAt;+ }+ }+ }++ return earliest;+ }+/// <summary>/// Returns whether the range covers the given action./// </summary>diff --git a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.csindex b8ec214be..345a763ff 100644--- a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs+++ b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs@@ -36,13 +36,31 @@ public class IpBlockListService(IAliasServerDbContextFactory dbContextFactory)=> IsBlockedAsync(ipAddress, IpBlockAction.Login);/// <summary>- /// Determines whether the given IP address is shadow-blocked for email retrieval. When true, callers should- /// return an empty result rather than an explicit error.+ /// Returns the earliest timestamp when the given IP address was shadow-blocked./// </summary>+ /// <param name="user">The authenticated user.</param>/// <param name="ipAddress">The IP address to evaluate.</param>- /// <returns>True if the IP is shadow-blocked for email retrieval, false otherwise.</returns>- public Task<bool> IsBlockedForEmailsAsync(IPAddress? ipAddress)- => IsBlockedAsync(ipAddress, IpBlockAction.Shadow);+ /// <returns>The earliest shadow-block timestamp, or null when not shadow-blocked.</returns>+ public async Task<DateTime?> GetEmailShadowBlockCutoffAsync(AliasVaultUser user, IPAddress? ipAddress)+ {+ // Account-level shadow-block. When the timestamp is unknown, return min timestamp.+ DateTime? cutoff = user.ShadowBlocked ? (user.ShadowBlockedAt ?? DateTime.UnixEpoch) : null;++ // IP-range shadow-block: the earliest matching block determines the cutoff (the most that should be hidden).+ if (ipAddress is not null)+ {+ await using var dbContext = await dbContextFactory.CreateDbContextAsync();+ var enabledRanges = await dbContext.BlockedIpRanges.Where(x => x.Enabled).ToListAsync();+ var ipCutoff = IpBlockEvaluator.GetEarliestMatchingBlockTime(enabledRanges, ipAddress, IpBlockAction.Shadow);++ if (ipCutoff is not null && (cutoff is null || ipCutoff < cutoff))+ {+ cutoff = ipCutoff;+ }+ }++ return cutoff;+ }/// <summary>/// Loads all enabled ranges and evaluates whether the IP is blocked for the given action.