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>
61 lines
2.4 KiB
C#
61 lines
2.4 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using SlpModularCms.Core.Hosting.Proxy;
|
|
using SlpModularCms.Modules.Contact.Data.Entities;
|
|
|
|
namespace SlpModularCms.Modules.Contact.Services;
|
|
|
|
/// <summary>
|
|
/// Verifies a captcha token server-side. Implementations must fail open: when the provider
|
|
/// itself is unreachable they return <c>true</c> (and log without personal data), so real
|
|
/// visitors are never locked out by a provider outage.
|
|
/// </summary>
|
|
public interface ICaptchaVerifier
|
|
{
|
|
Task<bool> VerifyAsync(string? captchaToken, CancellationToken cancellationToken);
|
|
}
|
|
|
|
/// <summary>Used while no captcha is configured: everything passes.</summary>
|
|
public sealed class NullCaptchaVerifier : ICaptchaVerifier
|
|
{
|
|
public Task<bool> VerifyAsync(string? captchaToken, CancellationToken cancellationToken) => Task.FromResult(true);
|
|
}
|
|
|
|
public enum ContactMailOutcome
|
|
{
|
|
/// <summary>No mail was attempted (no sender available yet); the message stays pending.</summary>
|
|
Skipped,
|
|
Sent,
|
|
Failed,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sends the notification mail for a genuine message. Called after the message is stored; a
|
|
/// failure must never fail the request. The real implementation arrives with the mail unit.
|
|
/// </summary>
|
|
public interface IContactMailSender
|
|
{
|
|
Task<ContactMailOutcome> SendAsync(ContactMessage message, CancellationToken cancellationToken);
|
|
}
|
|
|
|
public sealed class NoOpContactMailSender : IContactMailSender
|
|
{
|
|
public Task<ContactMailOutcome> SendAsync(ContactMessage message, CancellationToken cancellationToken)
|
|
=> Task.FromResult(ContactMailOutcome.Skipped);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The single place that decides which rate-limit bucket a request belongs to.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Every contact policy is partitioned per client, with the same key as the other policies of the
|
|
/// host (<see cref="ClientPartitionKey"/>: the remote address as seen by the pipeline, the real
|
|
/// visitor once <c>ForwardedHeaders:KnownProxies</c> 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
|
|
/// (<c>Contact:Enabled=false</c>) until that has been checked on the host.
|
|
/// </remarks>
|
|
public static class ContactRateLimitPartition
|
|
{
|
|
public static string KeyFor(HttpContext context) => ClientPartitionKey.For(context);
|
|
}
|