fix(opcua): parallel UserName activations wait for a slot instead of being refused (#833)

A legitimate consumer opening several keyed sessions at once from one key
(e.g. a ScadaBridge site node behind one NAT IP on a None endpoint) must
never see BadUserAccessDenied for parallelism. BeginAsync now hands out at
most MaxFailedAttempts - recordedFailures verification slots per key;
further activations queue FIFO and are woken as slots free. A waiter is
refused only when the key locks out (never reaching the authenticator) or,
uncounted, after OpcUa:CredentialLockout:SlotWaitSeconds (default 10,
BadTimeout). Cancelled waiters are dequeued; pruning accounts for waiters.
The security property is unchanged: no more than MaxFailedAttempts
unverified guesses in progress-or-failed per window.
This commit is contained in:
Joseph Doherty
2026-11-11 02:43:38 -05:00
parent 61b9adeb9c
commit 2314a53842
9 changed files with 397 additions and 159 deletions
@@ -275,31 +275,35 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
var lockoutKey = tokenType == UserTokenType.UserName
? FailedCredentialLockout.KeyFor(context.ChannelCertificate?.Thumbprint, peer)
: null;
if (lockoutKey is not null
&& !host._credentialLockout.TryBegin(lockoutKey, out var refusal, out var remaining, out var firstRefusal))
if (lockoutKey is not null)
{
// Logged once per lockout episode at Information; every further refusal at Debug, so a client hammering
// a lockout cannot flood the log (#833 review).
var level = firstRefusal ? LogLevel.Information : LogLevel.Debug;
if (refusal == CredentialLockoutRefusal.LockedOut)
// Waits (bounded) for one of this client's verification slots — parallel activations queue, they are
// never refused for parallelism. Refused only when locked out, or (uncounted) when no slot freed in time.
var decision = await host._credentialLockout.BeginAsync(lockoutKey, ct).ConfigureAwait(false);
if (decision.Verdict == CredentialLockoutVerdict.LockedOut)
{
var seconds = (long)Math.Ceiling(remaining.TotalSeconds);
host._logger.Log(level,
// Logged once per lockout episode at Information; every further refusal at Debug, so a client
// hammering a lockout cannot flood the log (#833 review).
var seconds = (long)Math.Ceiling(decision.Remaining.TotalSeconds);
host._logger.Log(decision.FirstRefusal ? LogLevel.Information : LogLevel.Debug,
"OpcUaApplicationHost: UserName activation refused — client {Client} is locked out for {Seconds}s more",
lockoutKey, seconds);
return AuthenticationResult.Reject(ServiceResult.Create(StatusCodes.BadUserAccessDenied,
$"Too many failed authentication attempts. Try again in {seconds} seconds."));
}
host._logger.Log(level,
"OpcUaApplicationHost: UserName activation refused — client {Client} already has as many failed plus "
+ "in-progress attempts as OpcUa:CredentialLockout allows",
lockoutKey);
return AuthenticationResult.Reject(ServiceResult.Create(StatusCodes.BadUserAccessDenied,
"Too many authentication attempts in progress. Try again shortly."));
if (decision.Verdict == CredentialLockoutVerdict.SlotWaitTimedOut)
{
host._logger.LogWarning(
"OpcUaApplicationHost: UserName activation for client {Client} timed out waiting for a "
+ "verification slot (OpcUa:CredentialLockout:SlotWaitSeconds); not counted as a failure",
lockoutKey);
return AuthenticationResult.Reject(ServiceResult.Create(StatusCodes.BadTimeout,
"Timed out waiting to verify the credential. Try again."));
}
}
// The slot TryBegin reserved is released on every path — completed with the outcome, or abandoned
// The slot BeginAsync granted is released on every path — completed with the outcome, or abandoned
// (uncounted) when the activation was cancelled or threw.
var completed = false;
try