feat(security): per-client pre-auth API-key miss budget; widen IOpcUaUserAuthenticator with the remote endpoint (lmxopcua#751)

The #733 miss budget (MissBurst / MissRefillPerSecond) was process-wide, because
the impersonation seam did not carry the client's address, so one noisy client
could drain it for everybody while the known-id set was unavailable.

- IOpcUaUserAuthenticator gains AuthenticateUserNameAsync(username, password,
  IPEndPoint? remoteEndpoint, ct) (default: ignore it and delegate); the server
  calls it. IApiKeyStore gains the matching FindAsync(..., IPAddress?, ...).
- The SDK (1.5.378) exposes no client address publicly. SecureChannelRemoteEndpoint
  reads it from the TCP listener's private channel map by reflection (through the
  protected TransportListeners, exposed by OtOpcUaSdkServer), keyed by the
  session's secure channel id. Fail-soft: null => process-wide accounting only.
- BoundedApiKeyStore charges a miss to the client's own bucket (keyed by IP
  address) and then to the process-wide one, which stays as the backstop. A
  refusal by either takes from neither; a hit refunds both. New options
  Security:ApiKeys:MissBurstPerEndpoint (10) / MissRefillPerSecondPerEndpoint (2),
  0 disables; telemetry outcome endpoint_miss_budget. At most 4096 addresses are
  tracked; idle buckets are pruned and an address beyond the cap is charged
  process-wide only.

Tests: BoundedApiKeyStorePerEndpointTests (noisy client vs quiet one, process-wide
backstop, refund + refill, off switch, authenticator forwards the address) and
over the wire ApiKeyMissBudgetPerEndpointTests (activation hands the authenticator
the loopback endpoint; real authenticator + bounded store refuse a client's 3rd
and 4th unknown key without a store read). Each new behaviour test fails with its
fix mutated out. Host.Tests 224/0, OpcUaServer.Tests 1019/1 (the one failure is the
wall-clock NodeManagerLockTimeBenchmarkTests under suite load; passes alone).
Docs: security.md.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 13:46:24 -05:00
parent f54922f94c
commit d59e76dd9d
11 changed files with 698 additions and 35 deletions
@@ -303,7 +303,21 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
}
private void OnImpersonateUser(ISession session, ImpersonateEventArgs args) =>
HandleImpersonation(_userAuthenticator, args, _logger, _options.AllowAnonymous);
HandleImpersonation(_userAuthenticator, args, _logger, _options.AllowAnonymous,
// Only a UserName activation consults the endpoint (the per-client API-key miss budget, #751), so
// the reflection walk is skipped for anonymous / X509.
args.NewIdentity is UserNameIdentityToken ? ResolveRemoteEndpoint(session) : null);
/// <summary>The remote endpoint of the client activating <paramref name="session"/>, or null when unknown.</summary>
/// <param name="session">The session being activated.</param>
/// <returns>The client's IP endpoint, or null.</returns>
/// <remarks>
/// Read from the session's secure channel — the SDK raises impersonation without the request's channel
/// context (<c>SecureChannelContext.Current</c> is obsolete in 1.5.378). A client that re-activates over
/// a NEW channel after its old one closed resolves to null and is accounted process-wide only.
/// </remarks>
private System.Net.IPEndPoint? ResolveRemoteEndpoint(ISession? session) =>
_server is OtOpcUaSdkServer sdk ? sdk.TryResolveRemoteEndpoint(session?.SecureChannelId) : null;
/// <summary>
/// Pure(-ish) impersonation handler: extracted so unit tests can drive it without booting
@@ -329,11 +343,14 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
/// <param name="args">The impersonation event arguments to process.</param>
/// <param name="logger">The logger for diagnostic output.</param>
/// <param name="allowAnonymous">Whether anonymous sessions may connect.</param>
/// <param name="remoteEndpoint">The activating client's IP endpoint when known (lmxopcua#751), handed to
/// the authenticator for per-client accounting; null when it could not be resolved.</param>
internal static void HandleImpersonation(
IOpcUaUserAuthenticator authenticator,
ImpersonateEventArgs args,
ILogger logger,
bool allowAnonymous = true)
bool allowAnonymous = true,
System.Net.IPEndPoint? remoteEndpoint = null)
{
if (args.NewIdentity is AnonymousIdentityToken anonymousToken)
{
@@ -367,7 +384,7 @@ public sealed class OpcUaApplicationHost : IAsyncDisposable
try
{
result = authenticator
.AuthenticateUserNameAsync(token.UserName ?? string.Empty, password, CancellationToken.None)
.AuthenticateUserNameAsync(token.UserName ?? string.Empty, password, remoteEndpoint, CancellationToken.None)
.GetAwaiter().GetResult();
}
catch (Exception ex)
@@ -24,6 +24,16 @@ public sealed class OtOpcUaSdkServer : StandardServer
/// <c>null</c> leaves it on a no-op logger.</param>
public OtOpcUaSdkServer(ILogger<OtOpcUaNodeManager>? nodeManagerLogger = null) => _nodeManagerLogger = nodeManagerLogger;
/// <summary>
/// The client's remote IP endpoint for the secure channel <paramref name="secureChannelId"/>, or null when it
/// cannot be resolved (lmxopcua#751). Best effort — see <see cref="SecureChannelRemoteEndpoint"/>; the SDK
/// exposes the listeners only to a subclass, which is why this lives here.
/// </summary>
/// <param name="secureChannelId">The global secure channel id a request arrived on.</param>
/// <returns>The remote endpoint, or null.</returns>
public System.Net.IPEndPoint? TryResolveRemoteEndpoint(string? secureChannelId) =>
SecureChannelRemoteEndpoint.Resolve(TransportListeners, secureChannelId);
/// <summary>The custom node manager once <c>StartAsync</c> has called
/// <see cref="CreateMasterNodeManager"/>. Null until the SDK has bootstrapped.</summary>
public OtOpcUaNodeManager? NodeManager => _otOpcUaNodeManager;
@@ -25,6 +25,26 @@ public interface IOpcUaUserAuthenticator
/// <returns>An OpcUaUserAuthResult indicating success or failure.</returns>
Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(string username, string password, CancellationToken ct);
/// <summary>
/// <see cref="AuthenticateUserNameAsync(string, string, CancellationToken)"/> with the activating client's
/// remote endpoint (lmxopcua#751), so an implementation can account pre-authentication failures per
/// client rather than process-wide. This is the overload the server calls; the default ignores the
/// endpoint and delegates, so an implementation that does not need it is unchanged.
/// </summary>
/// <remarks>
/// The endpoint is best effort (<see cref="SecureChannelRemoteEndpoint"/>) and null when unknown — an
/// implementation must treat null as "no per-client information", never as a distinct client. Same
/// must-not-throw contract as the three-argument overload.
/// </remarks>
/// <param name="username">The username to authenticate.</param>
/// <param name="password">The cleartext password to authenticate.</param>
/// <param name="remoteEndpoint">The client's IP endpoint, or null when it could not be resolved.</param>
/// <param name="ct">The cancellation token.</param>
/// <returns>An OpcUaUserAuthResult indicating success or failure.</returns>
Task<OpcUaUserAuthResult> AuthenticateUserNameAsync(
string username, string password, System.Net.IPEndPoint? remoteEndpoint, CancellationToken ct) =>
AuthenticateUserNameAsync(username, password, ct);
/// <summary>
/// Resolves what an <b>anonymous</b> session is allowed to do. Anonymous is a normal
/// principal — <see cref="OpcUaPrincipals.Anonymous"/> — not a special case, so it can be granted
@@ -0,0 +1,106 @@
using System.Collections;
using System.Net;
using System.Reflection;
using Opc.Ua;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
/// <summary>
/// Resolves the client's remote IP endpoint for a secure channel, so session activation can account
/// pre-authentication API-key misses per client (lmxopcua#751).
/// </summary>
/// <remarks>
/// <para>
/// ⚠️ <b>The OPC UA SDK (1.5.378) does not expose the client address on any public surface</b> — not
/// on <c>ISession</c>, <c>SecureChannelContext</c>, <c>ImpersonateEventArgs</c> nor the transport
/// listener. The TCP listener holds it privately: <c>TcpTransportListener.m_channels</c> (channel id
/// → channel) → the channel's non-public <c>Socket</c> → the public
/// <c>IMessageSocket.RemoteEndpoint</c>. This class walks that path by reflection.
/// </para>
/// <para>
/// It is strictly <b>best effort and fail-soft</b>: any missing member (an SDK upgrade renamed it), an
/// unknown channel or a closed socket yields <see langword="null"/>, and a caller treats null as
/// "address unknown" — the per-endpoint budget is skipped and the process-wide one still applies. It
/// never throws. An over-the-wire test (<c>RemoteEndpointResolutionTests</c>) pins that it resolves on
/// the SDK version in use, so an upgrade that breaks the path fails a test instead of silently
/// degrading to process-wide accounting.
/// </para>
/// </remarks>
public static class SecureChannelRemoteEndpoint
{
private const BindingFlags Instance = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
/// <summary>
/// Finds the remote endpoint of the secure channel <paramref name="secureChannelId"/> among
/// <paramref name="listeners"/>.
/// </summary>
/// <param name="listeners">The server's transport listeners.</param>
/// <param name="secureChannelId">The global secure channel id (<c>{ListenerId}-{ChannelId}</c>).</param>
/// <returns>The client's IP endpoint (IPv4-mapped addresses normalised to IPv4), or null when unknown.</returns>
public static IPEndPoint? Resolve(IEnumerable<ITransportListener>? listeners, string? secureChannelId)
{
if (listeners is null || string.IsNullOrEmpty(secureChannelId)) return null;
try
{
foreach (var listener in listeners)
{
if (listener is null) continue;
var listenerId = listener.ListenerId;
if (string.IsNullOrEmpty(listenerId)
|| !secureChannelId.StartsWith(listenerId + "-", StringComparison.Ordinal)
|| !uint.TryParse(secureChannelId.AsSpan(listenerId.Length + 1), out var channelId))
{
continue;
}
if (FindChannel(listener, channelId) is { } channel && RemoteEndpointOf(channel) is { } endpoint)
return endpoint;
}
}
catch
{
// Fail-soft by contract: the caller falls back to process-wide accounting.
}
return null;
}
/// <summary>The listener's channel with id <paramref name="channelId"/>, read from its private channel map.</summary>
private static object? FindChannel(ITransportListener listener, uint channelId)
{
var field = FindField(listener.GetType(), "m_channels");
if (field?.GetValue(listener) is not IDictionary channels) return null;
return channels.Contains(channelId) ? channels[channelId] : null;
}
/// <summary>The channel's socket's remote endpoint, as an <see cref="IPEndPoint"/>.</summary>
private static IPEndPoint? RemoteEndpointOf(object channel)
{
var socketProperty = FindProperty(channel.GetType(), "Socket");
var socket = socketProperty?.GetValue(channel);
if (socket is null) return null;
var remote = FindProperty(socket.GetType(), "RemoteEndpoint")?.GetValue(socket) as IPEndPoint;
if (remote is null) return null;
return remote.Address.IsIPv4MappedToIPv6 ? new IPEndPoint(remote.Address.MapToIPv4(), remote.Port) : remote;
}
private static FieldInfo? FindField(Type? type, string name)
{
for (; type is not null; type = type.BaseType)
{
if (type.GetField(name, Instance | BindingFlags.DeclaredOnly) is { } field) return field;
}
return null;
}
private static PropertyInfo? FindProperty(Type? type, string name)
{
for (; type is not null; type = type.BaseType)
{
if (type.GetProperty(name, Instance | BindingFlags.DeclaredOnly) is { } property) return property;
}
return null;
}
}