Skip to content

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

Add blocklist cache (#2131)

Leendert de Borst99b0eef3fa15
diff --git a/apps/server/AliasVault.Api/Controllers/Email/EmailController.cs b/apps/server/AliasVault.Api/Controllers/Email/EmailController.cs
index b61f6faee..81543937f 100644
--- a/apps/server/AliasVault.Api/Controllers/Email/EmailController.cs
+++ b/apps/server/AliasVault.Api/Controllers/Email/EmailController.cs
@@ -154,7 +154,7 @@ public class EmailController(ILogger<VaultController> logger, IAliasServerDbCont
}
// Shadow-block: when active, emails received after the block took effect behave as if they do not exist.
- var shadowCutoff = await ipBlockListService.GetEmailShadowBlockCutoffAsync(user, IpAddressUtility.GetRawIpAddressFromContext(HttpContext));
+ var shadowCutoff = await ipBlockListService.GetShadowBlockCutoffAsync(user, IpAddressUtility.GetRawIpAddressFromContext(HttpContext));
// Retrieve email from database.
var email = await context.Emails
diff --git a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs
index 0c90cf47d..30d098e33 100644
--- a/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs
+++ b/apps/server/Utilities/AliasVault.Auth/IpAddress/IpBlockListService.cs
@@ -12,13 +12,24 @@ using System.Threading.Tasks;
using AliasServerDb;
using AliasVault.Auth.IpAddress.Models;
using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Caching.Memory;
/// <summary>
-/// Service for checking whether an IP address is on the IP blocklist.
+/// Service for checking whether an IP address is on the IP blocklist. The blocklist is cached
+/// for a short period so that high-frequency callers (e.g. email retrieval) do not query
+/// the database on every request. Blocklist changes can therefore take a short time to take effect.
/// </summary>
/// <param name="dbContextFactory">IDbContextFactory instance.</param>
-public class IpBlockListService(IAliasServerDbContextFactory dbContextFactory)
+/// <param name="cache">IMemoryCache instance used to cache the enabled blocklist ranges.</param>
+public class IpBlockListService(IAliasServerDbContextFactory dbContextFactory, IMemoryCache cache)
{
+ /// <summary>
+ /// Number of seconds the enabled blocklist ranges are cached in memory.
+ /// </summary>
+ private const int CacheDurationSeconds = 60;
+
+ private const string EnabledRangesCacheKey = "IpBlockList_EnabledRanges";
+
/// <summary>
/// Determines whether the given IP address is blocked from registering a new account.
/// </summary>
@@ -49,8 +60,7 @@ public class IpBlockListService(IAliasServerDbContextFactory dbContextFactory)
// IP-range shadow-block: the earliest matching block determines the cutoff time.
if (ipAddress is not null)
{
- await using var dbContext = await dbContextFactory.CreateDbContextAsync();
- var enabledRanges = await dbContext.BlockedIpRanges.Where(x => x.Enabled).ToListAsync();
+ var enabledRanges = await GetEnabledRangesAsync();
var ipCutoff = IpBlockEvaluator.GetEarliestMatchingBlockTime(enabledRanges, ipAddress, IpBlockAction.Shadow);
if (ipCutoff is not null && (cutoff is null || ipCutoff < cutoff))
@@ -63,7 +73,7 @@ public class IpBlockListService(IAliasServerDbContextFactory dbContextFactory)
}
/// <summary>
- /// Loads all enabled ranges and evaluates whether the IP is blocked for the given action.
+ /// Loads the enabled ranges (from cache) and evaluates whether the IP is blocked for the given action.
/// </summary>
/// <param name="ipAddress">The IP address to evaluate.</param>
/// <param name="action">The action to evaluate.</param>
@@ -75,12 +85,29 @@ public class IpBlockListService(IAliasServerDbContextFactory dbContextFactory)
return false;
}
- await using var dbContext = await dbContextFactory.CreateDbContextAsync();
+ var enabledRanges = await GetEnabledRangesAsync();
+ return IpBlockEvaluator.IsBlocked(enabledRanges, ipAddress, action);
+ }
+ /// <summary>
+ /// Returns the set of enabled blocklist ranges, served from an in-memory cache that is refreshed at most once
+ /// every <see cref="CacheDurationSeconds"/> seconds.
+ /// </summary>
+ /// <returns>The list of enabled blocklist ranges.</returns>
+ private async Task<List<BlockedIpRange>> GetEnabledRangesAsync()
+ {
+ if (cache.TryGetValue(EnabledRangesCacheKey, out List<BlockedIpRange>? cached) && cached is not null)
+ {
+ return cached;
+ }
+
+ await using var dbContext = await dbContextFactory.CreateDbContextAsync();
var enabledRanges = await dbContext.BlockedIpRanges
+ .AsNoTracking()
.Where(x => x.Enabled)
.ToListAsync();
- return IpBlockEvaluator.IsBlocked(enabledRanges, ipAddress, action);
+ cache.Set(EnabledRangesCacheKey, enabledRanges, TimeSpan.FromSeconds(CacheDurationSeconds));
+ return enabledRanges;
}
}