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).
This commit is contained in:
Joseph Doherty
2026-11-09 12:05:11 -05:00
parent 42f69e4f00
commit 6130b1338e
2 changed files with 47 additions and 2 deletions
@@ -129,6 +129,48 @@ public sealed class CachingApiKeyVerifierTests
Assert.Equal(2, inner.CallCount);
}
/// <summary>
/// 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).
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[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<ApiKeyVerification> 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);
}
/// <summary>Invalidate reaches a cached entry regardless of the case of the key id it is given (mxaccessgw#173).</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[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);
}
/// <summary>
/// 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.