fix(opcua): credential lockout counts per credential identity, ignores transient denials (#844, #845)

#844: a success cleared the whole per-client count, so a client holding a valid
key could make four guesses at another key id, sign in with its own, and repeat
forever. Failures are now charged to the (client, credential identity) pair --
IOpcUaUserAuthenticator.DescribeCredential, key:<keyId> for API keys -- and to
the client as a whole. A success clears only its own identity's count; the
per-identity threshold locks that identity; a new per-client cap
(OpcUa:CredentialLockout:MaxFailedAttemptsPerClient, default 2 x
MaxFailedAttempts) that a success never resets bounds spraying across key ids
and locks every credential. Slots are bounded on both counts; a waiter whose
identity has room is not held behind one whose identity has none.

#845: (1) store timeouts, miss-budget refusals, store/authenticator exceptions,
an unset pepper and cancellation are reported Transient
(OpcUaUserAuthResult.Unavailable) and released uncounted, so a ConfigDb stall
can no longer lock every keyed client out; the client still sees the same
opaque denial. (2) a full 4096-entry table defers its next prune until the
earliest entry could age out instead of rescanning under the lock per failure.
(3) ApplicationUri deliberately NOT added to the key: on a None channel it is a
free string, so it would let an attacker reset the count per guess --
documented. (4) wire tests: distinct client certificates on a Sign endpoint are
locked out separately; the ApplicationConfigPath branch keeps the SDK lockout
off (falsified by removing the loaded-branch disable).
This commit is contained in:
Joseph Doherty
2026-11-11 03:47:10 -05:00
parent 481985e20b
commit ffba75bb93
13 changed files with 1150 additions and 208 deletions
@@ -154,6 +154,42 @@ public sealed class ApiKeyAuthenticationTests
result.Success.ShouldBeFalse();
result.Error.ShouldBe(ApiKeyOpcUaUserAuthenticator.DenyMessage);
result.Transient.ShouldBeTrue("#845: a store outage / timeout / miss-budget refusal is not a bad credential");
}
/// <summary>
/// #845 — only a definite verdict on the credential is non-transient: a wrong secret, an unknown key id, a
/// malformed credential and the wrong username all count toward the failed-credential lockout.
/// </summary>
[Fact]
public async Task Definite_credential_failures_are_not_transient()
{
var (presented, store) = SetUp();
var auth = Auth(store);
var ct = TestContext.Current.CancellationToken;
OpcUaUserAuthResult[] results =
[
await auth.AuthenticateUserNameAsync("apikey", $"otua_{store.KeyId}_wrong", ct),
await auth.AuthenticateUserNameAsync("apikey", "otua_ffffffff_whatever", ct),
await auth.AuthenticateUserNameAsync("apikey", "not-a-key", ct),
await auth.AuthenticateUserNameAsync("alice", presented, ct),
];
results.ShouldAllBe(r => !r.Success && !r.Transient);
}
/// <summary>
/// #844 — the lockout counts per credential identity: the key id (public — the lookup index, never the
/// secret), else the username. The secret must never appear in it (it is logged).
/// </summary>
[Fact]
public void The_credential_identity_is_the_key_id_and_never_the_secret()
{
var (presented, store) = SetUp();
var auth = Auth(store);
var identity = auth.DescribeCredential("apikey", presented);
identity.ShouldBe($"key:{store.KeyId}");
identity.ShouldNotContain(presented.Split('_')[^1]);
auth.DescribeCredential("apikey", "not-a-key").ShouldBe("user:apikey");
}
[Fact]
@@ -165,7 +201,9 @@ public sealed class ApiKeyAuthenticationTests
Options.Create(new ApiKeyOptions { Username = "apikey", Pepper = "" }),
NullLogger<ApiKeyOpcUaUserAuthenticator>.Instance);
(await auth.AuthenticateUserNameAsync("apikey", presented, TestContext.Current.CancellationToken)).Success.ShouldBeFalse();
var result = await auth.AuthenticateUserNameAsync("apikey", presented, TestContext.Current.CancellationToken);
result.Success.ShouldBeFalse();
result.Transient.ShouldBeTrue("#845: a deployment error, not a bad credential");
}
// ---------- anonymous as a normal principal (D3) ----------