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:
+13
@@ -79,4 +79,17 @@ public sealed class OpcUaApplicationHostOptionsValidatorTests
|
||||
result.Succeeded.ShouldBe(valid);
|
||||
if (!valid) result.Failures!.ShouldContain(f => f.Contains("OpcUa:CredentialLockout:LockoutSeconds"));
|
||||
}
|
||||
|
||||
/// <summary>#833 review: the slot wait must be positive while the lockout is enabled.</summary>
|
||||
[Fact]
|
||||
public void Credential_lockout_slot_wait_must_be_positive_while_enabled()
|
||||
{
|
||||
var result = Sut.Validate(null, new OpcUaApplicationHostOptions
|
||||
{
|
||||
CredentialLockout = new ZB.MOM.WW.OtOpcUa.OpcUaServer.Security.OpcUaCredentialLockoutOptions { SlotWaitSeconds = 0 },
|
||||
});
|
||||
|
||||
result.Failed.ShouldBeTrue();
|
||||
result.Failures!.ShouldContain(f => f.Contains("OpcUa:CredentialLockout:SlotWaitSeconds"));
|
||||
}
|
||||
}
|
||||
|
||||
+23
@@ -59,6 +59,29 @@ public sealed class CredentialLockoutWireTests
|
||||
auth.UserNameCalls.ShouldBe(callsBefore, "a locked-out client's credential is refused without being checked");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #833 review — one client opening more keyed sessions at once than MaxFailedAttempts (a ScadaBridge site
|
||||
/// node behind one NAT IP) is never refused for parallelism: the extra activations wait for a slot.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Parallel_valid_activations_beyond_the_threshold_all_succeed()
|
||||
{
|
||||
var ct = TestContext.Current.CancellationToken;
|
||||
var auth = new CountingAuthenticator();
|
||||
await using var rig = await Rig.StartAsync(auth, new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 2 }, ct);
|
||||
|
||||
var sessions = await Task.WhenAll(Enumerable.Range(0, 8).Select(_ => rig.ConnectAsync(GoodKey, ct)));
|
||||
try
|
||||
{
|
||||
sessions.ShouldAllBe(s => s.Connected);
|
||||
auth.UserNameCalls.ShouldBe(8);
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var s in sessions) s.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_disabled_lockout_admits_a_valid_key_after_any_number_of_failures()
|
||||
{
|
||||
|
||||
@@ -102,52 +102,166 @@ public sealed class FailedCredentialLockoutTests
|
||||
lockout.TrackedCount.ShouldBe(1, "expired entries are pruned to make room");
|
||||
}
|
||||
|
||||
private static CancellationToken Ct => TestContext.Current.CancellationToken;
|
||||
|
||||
/// <summary>
|
||||
/// #833 review — the authenticate call is awaited between the check and the outcome. A burst of parallel
|
||||
/// activations that all check before any failure is recorded must still be held to the threshold: the check
|
||||
/// reserves an in-flight slot, so only MaxFailedAttempts may be in progress at once.
|
||||
/// #833 review — a legitimate client opening many keyed sessions at once (a ScadaBridge site node behind one
|
||||
/// NAT IP) must never be refused for parallelism: at most MaxFailedAttempts verify concurrently, the rest
|
||||
/// WAIT for a slot, and all 20 succeed.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task A_parallel_burst_is_held_to_the_threshold_before_any_failure_is_recorded()
|
||||
public async Task An_all_valid_parallel_burst_queues_for_slots_and_every_activation_succeeds()
|
||||
{
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 3 }, new ManualClock());
|
||||
const int burst = 20;
|
||||
using var allChecked = new CountdownEvent(burst);
|
||||
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var admitted = 0;
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 5 }, new ManualClock());
|
||||
var concurrent = 0;
|
||||
var maxConcurrent = 0;
|
||||
var proceeded = 0;
|
||||
|
||||
var attempts = Enumerable.Range(0, burst).Select(attempt => Task.Run(async () =>
|
||||
await Task.WhenAll(Enumerable.Range(0, 20).Select(attempt => Task.Run(async () =>
|
||||
{
|
||||
var ok = lockout.TryBegin(Client, out _, out _, out _);
|
||||
if (ok) Interlocked.Increment(ref admitted);
|
||||
allChecked.Signal();
|
||||
await release.Task; // every attempt checks before any outcome lands — the race window
|
||||
if (ok) lockout.Complete(Client, succeeded: false);
|
||||
}, TestContext.Current.CancellationToken)).ToArray();
|
||||
var decision = await lockout.BeginAsync(Client, Ct);
|
||||
decision.Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
Interlocked.Increment(ref proceeded);
|
||||
var now = Interlocked.Increment(ref concurrent);
|
||||
InterlockedMax(ref maxConcurrent, now);
|
||||
await Task.Delay(20, Ct); // the authenticate call
|
||||
Interlocked.Decrement(ref concurrent);
|
||||
lockout.Complete(Client, succeeded: true);
|
||||
}, Ct)));
|
||||
|
||||
allChecked.Wait(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken).ShouldBeTrue();
|
||||
proceeded.ShouldBe(20);
|
||||
maxConcurrent.ShouldBe(5, "exactly MaxFailedAttempts verify at once — no more, and the cap is actually reached");
|
||||
lockout.TrackedCount.ShouldBe(0, "nothing left behind once the burst drains");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #833 review — the security half: a parallel burst of guesses that all arrive before any failure is
|
||||
/// recorded still reaches the authenticator only MaxFailedAttempts times; once those fail, every waiter is
|
||||
/// refused as locked out without being authenticated.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task An_all_bad_parallel_burst_authenticates_only_max_attempts_and_refuses_the_waiters_as_locked_out()
|
||||
{
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 5 }, new ManualClock());
|
||||
var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var authenticated = 0;
|
||||
var lockedOut = 0;
|
||||
|
||||
var attempts = Enumerable.Range(0, 20).Select(attempt => Task.Run(async () =>
|
||||
{
|
||||
var decision = await lockout.BeginAsync(Client, Ct);
|
||||
if (decision.Verdict == CredentialLockoutVerdict.LockedOut)
|
||||
{
|
||||
Interlocked.Increment(ref lockedOut);
|
||||
return;
|
||||
}
|
||||
|
||||
decision.Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
Interlocked.Increment(ref authenticated);
|
||||
await release.Task; // hold the slots until all 15 others are queued behind them
|
||||
lockout.Complete(Client, succeeded: false);
|
||||
}, Ct)).ToArray();
|
||||
|
||||
await WaitUntilAsync(() => lockout.WaitingCount(Client) == 15);
|
||||
authenticated.ShouldBe(5);
|
||||
release.SetResult();
|
||||
await Task.WhenAll(attempts);
|
||||
|
||||
admitted.ShouldBe(3, "only MaxFailedAttempts activations may be checked at once");
|
||||
lockout.IsLockedOut(Client, out _).ShouldBeTrue("the three failures lock the client");
|
||||
authenticated.ShouldBe(5, "no waiter may reach the authenticator once the key locks out");
|
||||
lockedOut.ShouldBe(15);
|
||||
lockout.IsLockedOut(Client, out _).ShouldBeTrue();
|
||||
lockout.WaitingCount(Client).ShouldBe(0);
|
||||
}
|
||||
|
||||
/// <summary>After a recorded failure the client has one slot fewer: capacity is MaxFailedAttempts − failures.</summary>
|
||||
[Fact]
|
||||
public async Task Recorded_failures_shrink_the_number_of_concurrent_verifications()
|
||||
{
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 3 }, new ManualClock());
|
||||
lockout.RecordFailure(Client);
|
||||
lockout.RecordFailure(Client);
|
||||
|
||||
(await lockout.BeginAsync(Client, Ct)).Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
var second = lockout.BeginAsync(Client, Ct).AsTask();
|
||||
await WaitUntilAsync(() => lockout.WaitingCount(Client) == 1);
|
||||
second.IsCompleted.ShouldBeFalse("one failure short of the threshold leaves exactly one slot");
|
||||
|
||||
lockout.Complete(Client, succeeded: true); // a success clears the count and hands the slot on
|
||||
(await second).Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
lockout.Complete(Client, succeeded: true);
|
||||
lockout.TrackedCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
/// <summary>A cancelled waiter is dequeued and leaves nothing behind (no leaked slot, entry prunable).</summary>
|
||||
[Fact]
|
||||
public async Task A_cancelled_waiter_is_removed_and_leaks_nothing()
|
||||
{
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 1 }, new ManualClock());
|
||||
(await lockout.BeginAsync(Client, Ct)).Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
|
||||
using var cts = CancellationTokenSource.CreateLinkedTokenSource(Ct);
|
||||
var waiting = lockout.BeginAsync(Client, cts.Token).AsTask();
|
||||
await WaitUntilAsync(() => lockout.WaitingCount(Client) == 1);
|
||||
cts.Cancel();
|
||||
await Should.ThrowAsync<OperationCanceledException>(waiting);
|
||||
lockout.WaitingCount(Client).ShouldBe(0);
|
||||
|
||||
lockout.Complete(Client, succeeded: true);
|
||||
lockout.TrackedCount.ShouldBe(0, "the held slot and the cancelled waiter are both gone");
|
||||
(await lockout.BeginAsync(Client, Ct)).Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
lockout.Abandon(Client);
|
||||
lockout.TrackedCount.ShouldBe(0);
|
||||
}
|
||||
|
||||
/// <summary>A waiter that never gets a slot times out, uncounted — the client is not one failure closer.</summary>
|
||||
[Fact]
|
||||
public async Task A_slot_wait_timeout_fails_that_attempt_without_counting_it()
|
||||
{
|
||||
var lockout = new FailedCredentialLockout(
|
||||
new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 1, SlotWaitSeconds = 1 }, new ManualClock());
|
||||
(await lockout.BeginAsync(Client, Ct)).Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
|
||||
(await lockout.BeginAsync(Client, Ct)).Verdict.ShouldBe(CredentialLockoutVerdict.SlotWaitTimedOut);
|
||||
lockout.WaitingCount(Client).ShouldBe(0);
|
||||
|
||||
lockout.Complete(Client, succeeded: true);
|
||||
lockout.TrackedCount.ShouldBe(0, "the timed-out attempt recorded no failure");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void In_flight_attempts_are_refused_as_TooManyInFlight_and_released_slots_free_up()
|
||||
public async Task Only_the_first_refusal_of_a_lockout_episode_is_flagged_for_Information_logging()
|
||||
{
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 2 }, new ManualClock());
|
||||
lockout.TryBegin(Client, out _, out _, out _).ShouldBeTrue();
|
||||
lockout.TryBegin(Client, out _, out _, out _).ShouldBeTrue();
|
||||
lockout.TryBegin(Client, out var refusal, out _, out var first).ShouldBeFalse();
|
||||
refusal.ShouldBe(CredentialLockoutRefusal.TooManyInFlight);
|
||||
first.ShouldBeTrue();
|
||||
var clock = new ManualClock();
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 1, LockoutSeconds = 10 }, clock);
|
||||
lockout.RecordFailure(Client).ShouldBeTrue();
|
||||
|
||||
lockout.Abandon(Client); // a cancelled activation frees its slot, uncounted
|
||||
lockout.Complete(Client, succeeded: true); // a success clears the count
|
||||
lockout.TryBegin(Client, out _, out _, out _).ShouldBeTrue();
|
||||
lockout.Complete(Client, succeeded: true);
|
||||
lockout.TrackedCount.ShouldBe(0);
|
||||
var r1 = await lockout.BeginAsync(Client, Ct);
|
||||
r1.Verdict.ShouldBe(CredentialLockoutVerdict.LockedOut);
|
||||
r1.FirstRefusal.ShouldBeTrue();
|
||||
(await lockout.BeginAsync(Client, Ct)).FirstRefusal.ShouldBeFalse();
|
||||
|
||||
clock.Advance(TimeSpan.FromSeconds(11));
|
||||
(await lockout.BeginAsync(Client, Ct)).Verdict.ShouldBe(CredentialLockoutVerdict.Proceed);
|
||||
lockout.Complete(Client, succeeded: false).ShouldBeTrue("a new episode");
|
||||
(await lockout.BeginAsync(Client, Ct)).FirstRefusal.ShouldBeTrue("each lockout episode logs its first refusal once");
|
||||
}
|
||||
|
||||
private static void InterlockedMax(ref int target, int value)
|
||||
{
|
||||
int seen;
|
||||
while ((seen = Volatile.Read(ref target)) < value && Interlocked.CompareExchange(ref target, value, seen) != seen)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task WaitUntilAsync(Func<bool> condition)
|
||||
{
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(10);
|
||||
while (!condition())
|
||||
{
|
||||
DateTime.UtcNow.ShouldBeLessThan(deadline, "condition not reached in time");
|
||||
await Task.Delay(5, Ct);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -170,26 +284,6 @@ public sealed class FailedCredentialLockoutTests
|
||||
lockout.RecordFailure(Client).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Only_the_first_refusal_of_a_lockout_episode_is_flagged_for_Information_logging()
|
||||
{
|
||||
var clock = new ManualClock();
|
||||
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 1, LockoutSeconds = 10 }, clock);
|
||||
lockout.RecordFailure(Client).ShouldBeTrue();
|
||||
|
||||
lockout.TryBegin(Client, out var r1, out _, out var first1).ShouldBeFalse();
|
||||
r1.ShouldBe(CredentialLockoutRefusal.LockedOut);
|
||||
first1.ShouldBeTrue();
|
||||
lockout.TryBegin(Client, out _, out _, out var first2).ShouldBeFalse();
|
||||
first2.ShouldBeFalse();
|
||||
|
||||
clock.Advance(TimeSpan.FromSeconds(11));
|
||||
lockout.TryBegin(Client, out _, out _, out _).ShouldBeTrue();
|
||||
lockout.Complete(Client, succeeded: false).ShouldBeTrue("a new episode");
|
||||
lockout.TryBegin(Client, out _, out _, out var first3).ShouldBeFalse();
|
||||
first3.ShouldBeTrue("each lockout episode logs its first refusal once");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_host_switches_the_sdk_lockout_off()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user