Concept vote rate limits: cast and mint budgets per account or network prefix
One FixedWindowBudget (injectable clock) owned by ConceptVoteRateLimits; the vote page 429s with Retry-After before any row or cookie. Prefix lengths and budgets are validated in every environment. Pins the empty Origin branch of CastRequestGuard. Unverified; not yet built. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
68214ced30
commit
fbf6e43955
13 changed files with 794 additions and 69 deletions
78
src/Bifrost.Web/Features/Shared/FixedWindowBudget.cs
Normal file
78
src/Bifrost.Web/Features/Shared/FixedWindowBudget.cs
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
namespace Bifrost.Web.Features.Shared;
|
||||
|
||||
// The one rate-limit mechanism: a fixed-window permit budget per caller-chosen partition key, read against an injected
|
||||
// clock. Keys are opaque and are never stored beyond their window: an entry whose window has ended is dropped by the
|
||||
// next sweep, so the table is bounded by the distinct keys seen in one window. System.Threading.RateLimiting is not
|
||||
// used because its limiters time themselves on Stopwatch and timers, which a test cannot move.
|
||||
public sealed class FixedWindowBudget
|
||||
{
|
||||
private readonly int permits;
|
||||
private readonly TimeSpan window;
|
||||
private readonly TimeProvider clock;
|
||||
private readonly Dictionary<string, Entry> entries = new(StringComparer.Ordinal);
|
||||
private readonly object gate = new();
|
||||
private DateTimeOffset nextSweep = DateTimeOffset.MinValue;
|
||||
|
||||
public FixedWindowBudget(int permits, TimeSpan window, TimeProvider clock)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(permits, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(window, TimeSpan.Zero);
|
||||
this.permits = permits;
|
||||
this.window = window;
|
||||
this.clock = clock;
|
||||
}
|
||||
|
||||
// How many keys are held now: bounded by the distinct keys seen in one window, not by every key ever seen.
|
||||
public int TrackedKeys
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (gate)
|
||||
{
|
||||
return entries.Count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Takes one permit for the key. When none is left, returns false and how long until its window ends.
|
||||
public bool TryAcquire(string key, out TimeSpan retryAfter)
|
||||
{
|
||||
var now = clock.GetUtcNow();
|
||||
lock (gate)
|
||||
{
|
||||
if (now >= nextSweep)
|
||||
{
|
||||
foreach (var expired in entries.Where(x => x.Value.Start + window <= now).Select(x => x.Key).ToList())
|
||||
{
|
||||
entries.Remove(expired);
|
||||
}
|
||||
|
||||
nextSweep = now + window;
|
||||
}
|
||||
|
||||
if (!entries.TryGetValue(key, out var entry) || entry.Start + window <= now)
|
||||
{
|
||||
entries[key] = new Entry(now);
|
||||
retryAfter = TimeSpan.Zero;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (entry.Count < permits)
|
||||
{
|
||||
entry.Count++;
|
||||
retryAfter = TimeSpan.Zero;
|
||||
return true;
|
||||
}
|
||||
|
||||
retryAfter = entry.Start + window - now;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class Entry(DateTimeOffset start)
|
||||
{
|
||||
public DateTimeOffset Start { get; } = start;
|
||||
|
||||
public int Count { get; set; } = 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -16,12 +16,35 @@ public sealed class StartupConfigurationValidator(
|
|||
{
|
||||
public void Validate()
|
||||
{
|
||||
if (hostEnvironment.IsDevelopment() || hostEnvironment.IsEnvironment("Testing"))
|
||||
var failures = new List<string>();
|
||||
|
||||
// Checked in every environment: a bad value here would otherwise pass startup and throw on every cast.
|
||||
var conceptVotes = conceptVoteOptions.Value;
|
||||
if (!NetworkPrefix.IsValidLength(conceptVotes.IPv4PrefixLength, NetworkPrefix.IPv4Bits))
|
||||
{
|
||||
return;
|
||||
failures.Add($"ConceptVotes:IPv4PrefixLength must be between 0 and {NetworkPrefix.IPv4Bits}.");
|
||||
}
|
||||
|
||||
var failures = new List<string>();
|
||||
if (!NetworkPrefix.IsValidLength(conceptVotes.IPv6PrefixLength, NetworkPrefix.IPv6Bits))
|
||||
{
|
||||
failures.Add($"ConceptVotes:IPv6PrefixLength must be between 0 and {NetworkPrefix.IPv6Bits}.");
|
||||
}
|
||||
|
||||
if (conceptVotes.CastPermits < 1 || conceptVotes.CastWindow <= TimeSpan.Zero)
|
||||
{
|
||||
failures.Add("ConceptVotes:CastPermits must be at least 1 and ConceptVotes:CastWindow must be positive.");
|
||||
}
|
||||
|
||||
if (conceptVotes.MintPermits < 1 || conceptVotes.MintWindow <= TimeSpan.Zero)
|
||||
{
|
||||
failures.Add("ConceptVotes:MintPermits must be at least 1 and ConceptVotes:MintWindow must be positive.");
|
||||
}
|
||||
|
||||
if (hostEnvironment.IsDevelopment() || hostEnvironment.IsEnvironment("Testing"))
|
||||
{
|
||||
Refuse(failures);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gitHubOAuthOptions.Value.IsConfigured)
|
||||
{
|
||||
|
|
@ -73,7 +96,6 @@ public sealed class StartupConfigurationValidator(
|
|||
failures.Add("DataProtection:KeysPath must be configured for production.");
|
||||
}
|
||||
|
||||
var conceptVotes = conceptVoteOptions.Value;
|
||||
if (string.IsNullOrWhiteSpace(conceptVotes.NetworkTagSecret) ||
|
||||
conceptVotes.NetworkTagSecret.Length < ConceptVoteOptions.MinNetworkTagSecretLength)
|
||||
{
|
||||
|
|
@ -81,21 +103,16 @@ public sealed class StartupConfigurationValidator(
|
|||
$"ConceptVotes:NetworkTagSecret must be configured with at least {ConceptVoteOptions.MinNetworkTagSecretLength} characters for production.");
|
||||
}
|
||||
|
||||
if (!NetworkPrefix.IsValidLength(conceptVotes.IPv4PrefixLength, NetworkPrefix.IPv4Bits))
|
||||
{
|
||||
failures.Add($"ConceptVotes:IPv4PrefixLength must be between 0 and {NetworkPrefix.IPv4Bits}.");
|
||||
}
|
||||
|
||||
if (!NetworkPrefix.IsValidLength(conceptVotes.IPv6PrefixLength, NetworkPrefix.IPv6Bits))
|
||||
{
|
||||
failures.Add($"ConceptVotes:IPv6PrefixLength must be between 0 and {NetworkPrefix.IPv6Bits}.");
|
||||
}
|
||||
|
||||
if (!conceptVotes.AllowedOrigins.All(IsExactHttpsOrigin))
|
||||
{
|
||||
failures.Add("ConceptVotes:AllowedOrigins entries must each be exactly https://host with no path, wildcard or trailing slash.");
|
||||
}
|
||||
|
||||
Refuse(failures);
|
||||
}
|
||||
|
||||
private static void Refuse(List<string> failures)
|
||||
{
|
||||
if (failures.Count > 0)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
|
|
|
|||
Loading…
Reference in a new issue