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:
@@ -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<string, HashSet<string>> _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<string, HashSet<string>> _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<Task<ApiKeyVerification>>> _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<string, long> _generations = new(StringComparer.Ordinal);
|
||||
private readonly ConcurrentDictionary<string, long> _generations = new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
/// <summary>Initializes a new instance of the <see cref="CachingApiKeyVerifier"/> class.</summary>
|
||||
/// <param name="inner">The wrapped verifier (the library verifier) reached on a cache miss.</param>
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user