Contactroutes: rate limiting per client met de ClientPartitionKey van de host

ContactRateLimitPartition.KeyFor gebruikt nu ClientPartitionKey.For(context) in plaats van een vaste sleutel; de test controleert dat verschillende clientadressen verschillende buckets krijgen. ForwardedHeaders:KnownProxies moet op elke host staan die de contactroutes inschakelt.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-10 13:25:43 +01:00
co-authored by Claude Sonnet 5.5
parent c3dceb203b
commit 99066f7ed6
2 changed files with 26 additions and 17 deletions
@@ -124,21 +124,31 @@ public class ContactRetentionServiceTests
public class ContactRateLimitPartitionTests
{
/// <summary>
/// REBASE CHECKLIST: on master there is no per-client partitioning, so every caller shares one
/// bucket and this test pins the constant. After the ASF merge the body of
/// <c>ContactRateLimitPartition.KeyFor</c> becomes <c>return ClientPartitionKey.For(context);</c>
/// and this test changes to "two different client addresses give two different keys".
/// Every contact policy is partitioned per client with the host's own key
/// (<c>ClientPartitionKey</c>): different client addresses never share a bucket.
/// </summary>
[Fact]
public void OnTheBaseBranch_EveryCallerSharesOneBucket()
public void DifferentClientAddresses_GetDifferentBuckets_AndTheSameAddressTheSameBucket()
{
var first = new DefaultHttpContext();
first.Connection.RemoteIpAddress = System.Net.IPAddress.Parse("203.0.113.10");
var again = new DefaultHttpContext();
again.Connection.RemoteIpAddress = System.Net.IPAddress.Parse("203.0.113.10");
var second = new DefaultHttpContext();
second.Connection.RemoteIpAddress = System.Net.IPAddress.Parse("198.51.100.20");
ContactRateLimitPartition.KeyFor(first).Should().Be(ContactRateLimitPartition.GlobalKey);
ContactRateLimitPartition.KeyFor(second).Should().Be(ContactRateLimitPartition.GlobalKey);
ContactRateLimitPartition.GlobalKey.Should().Be("global");
ContactRateLimitPartition.KeyFor(first).Should().NotBe(ContactRateLimitPartition.KeyFor(second));
ContactRateLimitPartition.KeyFor(first).Should().Be(ContactRateLimitPartition.KeyFor(again));
ContactRateLimitPartition.KeyFor(first).Should().Be("203.0.113.10");
}
[Fact]
public void TheKeyIsTheHostsOwnClientKey()
{
var context = new DefaultHttpContext();
context.Connection.RemoteIpAddress = System.Net.IPAddress.Parse("::ffff:192.0.2.7");
ContactRateLimitPartition.KeyFor(context).Should().Be(SlpModularCms.Core.Hosting.Proxy.ClientPartitionKey.For(context));
ContactRateLimitPartition.KeyFor(new DefaultHttpContext()).Should().Be(SlpModularCms.Core.Hosting.Proxy.ClientPartitionKey.Unknown);
}
}