From 6130b1338ec4bf555d5d621f698501f952f71de2 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Mon, 9 Nov 2026 12:05:11 -0500 Subject: [PATCH] fix(server): compare API-key ids ordinal-ignore-case in the verifier cache guard (mxaccessgw#173) The generation counter and key-id eviction index used ordinal comparison, so an Invalidate spelled with different case than the presented token missed the guard. Both now use OrdinalIgnoreCase; two tests added (fail without the fix). --- .../Authentication/CachingApiKeyVerifier.cs | 7 +++- .../CachingApiKeyVerifierTests.cs | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/CachingApiKeyVerifier.cs b/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/CachingApiKeyVerifier.cs index 48d3d8cf..113516a8 100644 --- a/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/CachingApiKeyVerifier.cs +++ b/src/ZB.MOM.WW.MxGateway.Server/Security/Authentication/CachingApiKeyVerifier.cs @@ -89,7 +89,10 @@ public sealed class CachingApiKeyVerifier : IApiKeyVerifier, IApiKeyCacheInvalid // it out of reach of a later Invalidate. Every operation under the lock is O(1) and the lock is // only taken on misses, evictions and invalidations — never on a cache hit. private readonly object _indexSync = new(); - private readonly Dictionary> _keyIdIndex = new(StringComparer.Ordinal); + // Key ids compare ordinal-ignore-case: the token's key id (as typed by the client), the verified + // identity's key id and the id the admin path passes to Invalidate can differ in case, and the + // generation guard / eviction must not depend on which spelling reached it (mxaccessgw#173). + private readonly Dictionary> _keyIdIndex = new(StringComparer.OrdinalIgnoreCase); // (cache key, key generation) -> the in-flight inner verification for that pair (single-flight). private readonly ConcurrentDictionary<(string CacheKey, long Generation), Lazy>> _inflight = @@ -100,7 +103,7 @@ public sealed class CachingApiKeyVerifier : IApiKeyVerifier, IApiKeyCacheInvalid // keyId -> monotonic generation, bumped by Invalidate before it evicts. A VerifyAsync snapshots // the generation before calling the inner verifier and only caches if it is unchanged after the // inner call and again after the Set — closing the revoke-vs-in-flight-repopulation race. - private readonly ConcurrentDictionary _generations = new(StringComparer.Ordinal); + private readonly ConcurrentDictionary _generations = new(StringComparer.OrdinalIgnoreCase); /// Initializes a new instance of the class. /// The wrapped verifier (the library verifier) reached on a cache miss. diff --git a/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/CachingApiKeyVerifierTests.cs b/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/CachingApiKeyVerifierTests.cs index 146002cd..7278d642 100644 --- a/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/CachingApiKeyVerifierTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Tests/Security/Authentication/CachingApiKeyVerifierTests.cs @@ -129,6 +129,48 @@ public sealed class CachingApiKeyVerifierTests Assert.Equal(2, inner.CallCount); } + /// + /// The generation guard must not depend on key-id case: an Invalidate spelled differently from the + /// key id in the presented token still discards an in-flight verification's repopulation + /// (mxaccessgw#173). + /// + /// A task that represents the asynchronous operation. + [Fact] + public async Task Invalidate_DifferentKeyIdCase_DuringInFlightVerification_DiscardsStaleRepopulation() + { + const string mixedCaseHeader = "Bearer mxgw_Operator01_secret"; + GatedVerifier inner = new(Success("operator01")); + using MemoryCache cache = NewCache(); + CachingApiKeyVerifier verifier = new(inner, cache, TimeSpan.FromSeconds(30)); + + Task inFlight = verifier.VerifyAsync(mixedCaseHeader, CancellationToken.None); + await inner.Entered; + + ((IApiKeyCacheInvalidator)verifier).Invalidate("OPERATOR01"); + + inner.Release(); + await inFlight; + + await verifier.VerifyAsync(mixedCaseHeader, CancellationToken.None); + Assert.Equal(2, inner.CallCount); + } + + /// Invalidate reaches a cached entry regardless of the case of the key id it is given (mxaccessgw#173). + /// A task that represents the asynchronous operation. + [Fact] + public async Task Invalidate_DifferentKeyIdCase_EvictsCachedEntry() + { + FakeVerifier inner = new(Success("operator01")); + using MemoryCache cache = NewCache(); + CachingApiKeyVerifier verifier = new(inner, cache, TimeSpan.FromSeconds(30)); + + await verifier.VerifyAsync(Header, CancellationToken.None); + ((IApiKeyCacheInvalidator)verifier).Invalidate("Operator01"); + await verifier.VerifyAsync(Header, CancellationToken.None); + + Assert.Equal(2, inner.CallCount); + } + /// /// Single-flight: concurrent misses for the same token (a TTL expiry under load) share one /// inner verification instead of each issuing its own SQLite read.