Admin sign-in and password-reset attempts are counted across every instance that shares the ValKey server #74

Open
opened 2026-10-31 12:42:01 -04:00 by claude · 0 comments
Owner
  • Draft: INT-21
  • Priority: medium (with N nodes, password guessing gets N times the sign-in budget today)
  • Agent: coder
  • Depends on: #70
  • Decisions: Q-INT-3

Why

The sign-in limiter is the ASP.NET Core rate-limiting middleware with a fixed-window partition per client IPv4 address or IPv6 /64, for two policies, admin-login and password-reset (src/dotnet/src/Ninlil.Dns.Identity/Extension/ServiceCollectionAdminLoginRateLimitingExtension.cs:33, :42, :119-175). Its counters live in the process. It is what bounds password guessing, because a wrong password does not count toward Identity lockout (src/dotnet/src/Ninlil.Dns.Identity/Options/AdminLoginRateLimitOptions.cs:22-28). Behind a load balancer, each node grants its own PermitLimit (default 5 per 5 minutes, :53, :60).

What changes

Technical call: unlike the DNS limiter (#73), a synchronous ValKey round trip per attempt is acceptable here: sign-ins are rare and already slow (password hashing), so the shared count is checked on every attempt rather than in a background loop.

  • Ninlil.Common.Valkey: IValkeyWindowCounter with Task<long?> IncrementAsync(string key, TimeSpan window, CancellationToken stoppingToken). It runs INCR and, on the first increment, PEXPIRE with the window, in one transaction. It returns null when ValKey is not configured, disconnected, or slower than 250 ms.
  • Ninlil.Dns.Identity/RateLimiting/ (new folder):
    • IAdminLoginAttemptCounter with ValueTask<long?> CountAttemptAsync(string policyName, string partitionKey, CancellationToken stoppingToken). Identity does not reference ValKey.
    • A RateLimiter subclass that wraps the existing local fixed-window limiter. It acquires the local permit first (a local denial stays a 429 at once). Then it asks the counter. A shared count over PermitLimit is a denial, with RetryAfter metadata set to the end of the shared window.
    • Shared windows are aligned to whole multiples of Window since the Unix epoch, so every node uses the same key <prefix>:rl:login:<policy>:<partitionKey>:<windowIndex>.
    • GetPartition (ServiceCollectionAdminLoginRateLimitingExtension.cs:152-175) builds this limiter when a counter is registered, and the plain one otherwise. OnRejected prefers the shared window's remaining time for Retry-After.
  • src/dotnet/src/Ninlil.Web/Valkey/ValkeyAdminLoginAttemptCounter.cs (new): implements IAdminLoginAttemptCounter over IValkeyWindowCounter. It is registered only when ValkeyOptions.EnableSharedRateLimit is set and a connection string exists.
  • Failure behaviour: when the counter returns null, the local limiter alone decides. The local limit always holds, so the limiter still fails closed per node, as AdminLoginRateLimitOptions documents (:15-20). A Warning is logged at most once a minute while the shared count is unavailable.
  • Update the summaries of AdminLoginRateLimitOptions and ServiceCollectionAdminLoginRateLimitingExtension to describe the shared count.

Acceptance

  • Unit test (fails first) in src/dotnet/tests/Ninlil.Dns.Identity.Tests: with a fake counter that reports 6 for a partition and PermitLimit = 5, the first attempt on a fresh local limiter is rejected with a RetryAfter value.
  • Unit test: with the fake counter returning null, the sixth local attempt is rejected and the first five pass, as today.
  • Unit test: admin-login and password-reset use different keys, so a reset request does not use up sign-in attempts.
  • Container test (RequiresDocker) in src/dotnet/tests/Ninlil.Web.Tests: two hosts (factory.CreateSecondHost) on one ValKey with PermitLimit = 3; two failed sign-ins on host A and one on host B, then the next on host B gets 429 with Retry-After.
  • src/dotnet/tests/Ninlil.Common.Valkey.Tests passes.
  • No new option (EnableSharedRateLimit from #70 covers it); ENVIRONMENT.md description of that variable names both limiters.
  • README sign-in rate-limit text says the count is shared when ValKey is configured with EnableSharedRateLimit.

Source

Owner decision 2026-10-31 (Q-INT-3): "ValKey counters: DNS queries AND admin login limiter (NEW child issue)".

- **Draft:** INT-21 - **Priority:** medium (with N nodes, password guessing gets N times the sign-in budget today) - **Agent:** coder - **Depends on:** #70 - **Decisions:** Q-INT-3 ### Why The sign-in limiter is the ASP.NET Core rate-limiting middleware with a fixed-window partition per client IPv4 address or IPv6 /64, for two policies, `admin-login` and `password-reset` (`src/dotnet/src/Ninlil.Dns.Identity/Extension/ServiceCollectionAdminLoginRateLimitingExtension.cs:33`, `:42`, `:119-175`). Its counters live in the process. It is what bounds password guessing, because a wrong password does not count toward Identity lockout (`src/dotnet/src/Ninlil.Dns.Identity/Options/AdminLoginRateLimitOptions.cs:22-28`). Behind a load balancer, each node grants its own `PermitLimit` (default 5 per 5 minutes, `:53`, `:60`). ### What changes **Technical call:** unlike the DNS limiter (#73), a synchronous ValKey round trip per attempt is acceptable here: sign-ins are rare and already slow (password hashing), so the shared count is checked on every attempt rather than in a background loop. - `Ninlil.Common.Valkey`: `IValkeyWindowCounter` with `Task<long?> IncrementAsync(string key, TimeSpan window, CancellationToken stoppingToken)`. It runs `INCR` and, on the first increment, `PEXPIRE` with the window, in one transaction. It returns null when ValKey is not configured, disconnected, or slower than 250 ms. - `Ninlil.Dns.Identity/RateLimiting/` (new folder): - `IAdminLoginAttemptCounter` with `ValueTask<long?> CountAttemptAsync(string policyName, string partitionKey, CancellationToken stoppingToken)`. Identity does not reference ValKey. - A `RateLimiter` subclass that wraps the existing local fixed-window limiter. It acquires the local permit first (a local denial stays a 429 at once). Then it asks the counter. A shared count over `PermitLimit` is a denial, with `RetryAfter` metadata set to the end of the shared window. - Shared windows are aligned to whole multiples of `Window` since the Unix epoch, so every node uses the same key `<prefix>:rl:login:<policy>:<partitionKey>:<windowIndex>`. - `GetPartition` (`ServiceCollectionAdminLoginRateLimitingExtension.cs:152-175`) builds this limiter when a counter is registered, and the plain one otherwise. `OnRejected` prefers the shared window's remaining time for `Retry-After`. - `src/dotnet/src/Ninlil.Web/Valkey/ValkeyAdminLoginAttemptCounter.cs` (new): implements `IAdminLoginAttemptCounter` over `IValkeyWindowCounter`. It is registered only when `ValkeyOptions.EnableSharedRateLimit` is set and a connection string exists. - Failure behaviour: when the counter returns null, the local limiter alone decides. The local limit always holds, so the limiter still fails closed per node, as `AdminLoginRateLimitOptions` documents (`:15-20`). A Warning is logged at most once a minute while the shared count is unavailable. - Update the summaries of `AdminLoginRateLimitOptions` and `ServiceCollectionAdminLoginRateLimitingExtension` to describe the shared count. ### Acceptance - [ ] Unit test (fails first) in `src/dotnet/tests/Ninlil.Dns.Identity.Tests`: with a fake counter that reports 6 for a partition and `PermitLimit = 5`, the first attempt on a fresh local limiter is rejected with a `RetryAfter` value. - [ ] Unit test: with the fake counter returning null, the sixth local attempt is rejected and the first five pass, as today. - [ ] Unit test: `admin-login` and `password-reset` use different keys, so a reset request does not use up sign-in attempts. - [ ] Container test (RequiresDocker) in `src/dotnet/tests/Ninlil.Web.Tests`: two hosts (`factory.CreateSecondHost`) on one ValKey with `PermitLimit = 3`; two failed sign-ins on host A and one on host B, then the next on host B gets 429 with `Retry-After`. - [ ] `src/dotnet/tests/Ninlil.Common.Valkey.Tests` passes. - [ ] No new option (`EnableSharedRateLimit` from #70 covers it); `ENVIRONMENT.md` description of that variable names both limiters. - [ ] README sign-in rate-limit text says the count is shared when ValKey is configured with `EnableSharedRateLimit`. ### Source Owner decision 2026-10-31 (Q-INT-3): "ValKey counters: DNS queries AND admin login limiter (NEW child issue)".
claude added this to the Release: ValKey milestone 2026-10-31 12:42:01 -04:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
lantea-one/ninlil#74
No description provided.