988927c8ac
- Reserve an in-flight slot per client before authenticating (TryBegin / Complete / Abandon under one lock); refuse once recorded failures + in-flight attempts reach MaxFailedAttempts, so a parallel burst cannot pass the check before its first failure is recorded. - An expired lockout resets the count (LockoutSeconds < FailureWindowSeconds relocked on every single failure after the first lockout). - Validate OpcUa:CredentialLockout at host start: disabled, or both durations > 0. - Log the first refusal of a lockout episode at Information, the rest at Debug. - Document that on None endpoints the key is effectively IP-only (NAT neighbours share a UserName count) and a null peer shares one key.
209 lines
9.7 KiB
C#
209 lines
9.7 KiB
C#
using System.Net;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests;
|
|
|
|
/// <summary>lmxopcua#833 — the UserName-only lockout that replaces the SDK's all-token lockout.</summary>
|
|
public sealed class FailedCredentialLockoutTests
|
|
{
|
|
private static readonly string Client = FailedCredentialLockout.KeyFor("THUMB", IPAddress.Parse("10.0.0.5"));
|
|
|
|
[Fact]
|
|
public void Locks_at_the_threshold_and_releases_after_the_lockout()
|
|
{
|
|
var clock = new ManualClock();
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions(), clock);
|
|
|
|
for (var i = 0; i < 4; i++) lockout.RecordFailure(Client).ShouldBeFalse();
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
lockout.RecordFailure(Client).ShouldBeTrue("the fifth failure locks");
|
|
lockout.IsLockedOut(Client, out var remaining).ShouldBeTrue();
|
|
remaining.ShouldBe(TimeSpan.FromMinutes(5));
|
|
|
|
clock.Advance(TimeSpan.FromSeconds(299));
|
|
lockout.IsLockedOut(Client, out _).ShouldBeTrue();
|
|
clock.Advance(TimeSpan.FromSeconds(1));
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
lockout.RecordFailure(Client).ShouldBeFalse("a lockout that ran out starts a fresh count");
|
|
}
|
|
|
|
[Fact]
|
|
public void A_refusal_during_the_lockout_does_not_extend_it()
|
|
{
|
|
var clock = new ManualClock();
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 1 }, clock);
|
|
lockout.RecordFailure(Client).ShouldBeTrue();
|
|
clock.Advance(TimeSpan.FromSeconds(200));
|
|
lockout.RecordFailure(Client).ShouldBeFalse();
|
|
clock.Advance(TimeSpan.FromSeconds(100));
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void Failures_spaced_wider_than_the_window_never_accumulate()
|
|
{
|
|
var clock = new ManualClock();
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions(), clock);
|
|
for (var i = 0; i < 10; i++)
|
|
{
|
|
lockout.RecordFailure(Client).ShouldBeFalse();
|
|
clock.Advance(TimeSpan.FromSeconds(61));
|
|
}
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void A_success_clears_the_count()
|
|
{
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions(), new ManualClock());
|
|
for (var i = 0; i < 4; i++) lockout.RecordFailure(Client);
|
|
lockout.RecordSuccess(Client);
|
|
for (var i = 0; i < 4; i++) lockout.RecordFailure(Client).ShouldBeFalse();
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
}
|
|
|
|
[Fact]
|
|
public void The_key_needs_both_certificate_and_address_so_one_client_cannot_lock_out_another()
|
|
{
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 1 }, new ManualClock());
|
|
var attacker = FailedCredentialLockout.KeyFor("VICTIMTHUMB", IPAddress.Parse("10.0.0.99"));
|
|
var victim = FailedCredentialLockout.KeyFor("VICTIMTHUMB", IPAddress.Parse("10.0.0.5"));
|
|
var natNeighbour = FailedCredentialLockout.KeyFor("OTHERTHUMB", IPAddress.Parse("10.0.0.99"));
|
|
|
|
lockout.RecordFailure(attacker).ShouldBeTrue();
|
|
lockout.IsLockedOut(victim, out _).ShouldBeFalse("same certificate, different address");
|
|
lockout.IsLockedOut(natNeighbour, out _).ShouldBeFalse("same address, different certificate");
|
|
}
|
|
|
|
[Fact]
|
|
public void Zero_attempts_disables_it()
|
|
{
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 0 }, new ManualClock());
|
|
for (var i = 0; i < 50; i++) lockout.RecordFailure(Client).ShouldBeFalse();
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
lockout.TrackedCount.ShouldBe(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void Tracking_is_bounded_and_prunes_expired_clients_first()
|
|
{
|
|
var clock = new ManualClock();
|
|
var lockout = new FailedCredentialLockout(new OpcUaCredentialLockoutOptions(), clock);
|
|
for (var i = 0; i < FailedCredentialLockout.MaxTrackedClients; i++) lockout.RecordFailure($"c{i}");
|
|
lockout.TrackedCount.ShouldBe(FailedCredentialLockout.MaxTrackedClients);
|
|
|
|
lockout.RecordFailure("overflow");
|
|
lockout.TrackedCount.ShouldBe(FailedCredentialLockout.MaxTrackedClients, "full and nothing expired ⇒ not tracked");
|
|
|
|
clock.Advance(TimeSpan.FromSeconds(61));
|
|
lockout.RecordFailure("overflow");
|
|
lockout.TrackedCount.ShouldBe(1, "expired entries are pruned to make room");
|
|
}
|
|
|
|
/// <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.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task A_parallel_burst_is_held_to_the_threshold_before_any_failure_is_recorded()
|
|
{
|
|
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 attempts = Enumerable.Range(0, burst).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();
|
|
|
|
allChecked.Wait(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken).ShouldBeTrue();
|
|
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");
|
|
}
|
|
|
|
[Fact]
|
|
public void In_flight_attempts_are_refused_as_TooManyInFlight_and_released_slots_free_up()
|
|
{
|
|
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();
|
|
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// #833 review — with LockoutSeconds shorter than FailureWindowSeconds, the count survived the expired lockout
|
|
/// (the last failure was still inside the window), so the very next failure relocked the client.
|
|
/// </summary>
|
|
[Fact]
|
|
public void An_expired_lockout_starts_a_fresh_count_even_inside_the_failure_window()
|
|
{
|
|
var clock = new ManualClock();
|
|
var lockout = new FailedCredentialLockout(
|
|
new OpcUaCredentialLockoutOptions { MaxFailedAttempts = 2, FailureWindowSeconds = 600, LockoutSeconds = 10 }, clock);
|
|
lockout.RecordFailure(Client).ShouldBeFalse();
|
|
lockout.RecordFailure(Client).ShouldBeTrue();
|
|
|
|
clock.Advance(TimeSpan.FromSeconds(11));
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
lockout.RecordFailure(Client).ShouldBeFalse("one failure after the lockout is the first of a new run");
|
|
lockout.IsLockedOut(Client, out _).ShouldBeFalse();
|
|
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()
|
|
{
|
|
var config = new Opc.Ua.ServerConfiguration();
|
|
config.MaxFailedAuthenticationAttempts.ShouldBe(5, "the SDK default this guards against");
|
|
OpcUaApplicationHost.DisableSdkAuthenticationLockout(config);
|
|
config.MaxFailedAuthenticationAttempts.ShouldBe(0);
|
|
}
|
|
|
|
private sealed class ManualClock : TimeProvider
|
|
{
|
|
private DateTimeOffset _now = new(2026, 11, 11, 0, 0, 0, TimeSpan.Zero);
|
|
public override DateTimeOffset GetUtcNow() => _now;
|
|
public void Advance(TimeSpan by) => _now += by;
|
|
}
|
|
}
|