From 99066f7ed6caff8336345bfb54714e348ebd352d Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Tue, 10 Nov 2026 13:25:43 +0100 Subject: [PATCH] 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 --- .../ContactRetentionAndPartitionTests.cs | 26 +++++++++++++------ .../Services/ContactSeams.cs | 17 ++++++------ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/SlpModularCms.Modules.Contact.Tests/ContactRetentionAndPartitionTests.cs b/src/SlpModularCms.Modules.Contact.Tests/ContactRetentionAndPartitionTests.cs index 915781e..3f89d0c 100644 --- a/src/SlpModularCms.Modules.Contact.Tests/ContactRetentionAndPartitionTests.cs +++ b/src/SlpModularCms.Modules.Contact.Tests/ContactRetentionAndPartitionTests.cs @@ -124,21 +124,31 @@ public class ContactRetentionServiceTests public class ContactRateLimitPartitionTests { /// - /// 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 - /// ContactRateLimitPartition.KeyFor becomes return ClientPartitionKey.For(context); - /// 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 + /// (ClientPartitionKey): different client addresses never share a bucket. /// [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); } } diff --git a/src/SlpModularCms.Modules.Contact/Services/ContactSeams.cs b/src/SlpModularCms.Modules.Contact/Services/ContactSeams.cs index c68ea7a..bb55c42 100644 --- a/src/SlpModularCms.Modules.Contact/Services/ContactSeams.cs +++ b/src/SlpModularCms.Modules.Contact/Services/ContactSeams.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Http; +using SlpModularCms.Core.Hosting.Proxy; using SlpModularCms.Modules.Contact.Data.Entities; namespace SlpModularCms.Modules.Contact.Services; @@ -46,16 +47,14 @@ public sealed class NoOpContactMailSender : IContactMailSender /// The single place that decides which rate-limit bucket a request belongs to. /// /// -/// On the base branch the rate limiter has no per-client partitioning (no forwarded-header -/// handling, no client key): every visitor is the same caller. This returns a constant, so the -/// contact policies behave as ONE shared bucket per policy, which is why the contact routes are -/// disabled (Contact:Enabled=false) until the per-client partitioning from the ASF branch -/// has been merged. Switching to per-client is the one-line change documented in the rebase -/// checklist: replace the body with ClientPartitionKey.For(context). +/// Every contact policy is partitioned per client, with the same key as the other policies of the +/// host (: the remote address as seen by the pipeline, the real +/// visitor once ForwardedHeaders:KnownProxies is configured). That setting is required on +/// every host that enables the contact routes: without it all visitors share the proxy's address, +/// i.e. one bucket for the whole site. The routes stay disabled by default +/// (Contact:Enabled=false) until that has been checked on the host. /// public static class ContactRateLimitPartition { - public const string GlobalKey = "global"; - - public static string KeyFor(HttpContext context) => GlobalKey; + public static string KeyFor(HttpContext context) => ClientPartitionKey.For(context); }