Contactmodule: token- en inzendroute met spamafhandeling, idempotentie en retentietaak
Nieuwe module SlpModularCms.Modules.Contact met eigen DbContext en migratie InitialCreate. GET contact-messages/token en POST contact-messages zijn publiek ([PublicSiteEndpoint]), geven bij echte inzendingen, spam en herhalingen hetzelfde 202 en slaan spam apart op (30 dagen, dagplafond). Standaard staat de module uit (Contact:Enabled=false, 404 met lege body, geen databasetoegang) totdat de rate limiting per client op de basisbranch staat; de rate-limit-sleutel zit in een enkele functie zodat dat later een regel wijzigen is. Mail zit achter een seam (nog een no-op). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading.RateLimiting;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Modules;
|
||||
using SlpModularCms.Modules.Contact.Config;
|
||||
using SlpModularCms.Modules.Contact.Controllers;
|
||||
using SlpModularCms.Modules.Contact.Data;
|
||||
using SlpModularCms.Modules.Contact.Repositories;
|
||||
using SlpModularCms.Modules.Contact.Services;
|
||||
|
||||
namespace SlpModularCms.Modules.Contact;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class ContactModule : IModule
|
||||
{
|
||||
public string Name => "Contact";
|
||||
public string Version => "1.0.0";
|
||||
|
||||
public void RegisterServices(IServiceCollection services)
|
||||
{
|
||||
services.AddOptions<ContactOptions>()
|
||||
.Configure<IConfiguration>((options, config) =>
|
||||
config.GetSection(ContactOptions.SectionName).Bind(options));
|
||||
|
||||
services.AddDbContext<ContactDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
var config = serviceProvider.GetRequiredService<IConfiguration>();
|
||||
options.UseMySQL(config.GetConnectionString("DefaultConnection")!);
|
||||
options.ReplaceService<IHistoryRepository, NonLockingMySQLHistoryRepository>();
|
||||
});
|
||||
|
||||
services.TryAddSingleton(TimeProvider.System);
|
||||
services.AddScoped<IContactRepository, ContactRepository>();
|
||||
services.AddScoped<IContactSubmissionService, ContactSubmissionService>();
|
||||
services.AddSingleton<IContactTokenService, ContactTokenService>();
|
||||
services.AddSingleton<ICaptchaVerifier, NullCaptchaVerifier>();
|
||||
services.AddScoped<IContactMailSender, NoOpContactMailSender>();
|
||||
services.AddScoped<IContactFormGate, HostFlagContactFormGate>();
|
||||
services.AddScoped<ContactRouteFilter>();
|
||||
services.AddHostedService<ContactRetentionService>();
|
||||
|
||||
// Rate-limit policies live in this module so the shared limiter setup stays untouched.
|
||||
// The bucket key comes from one function; see ContactRateLimitPartition.
|
||||
services.AddOptions<RateLimiterOptions>()
|
||||
.Configure<IConfiguration>((limiter, config) =>
|
||||
{
|
||||
limiter.AddPolicy(ContactMessagesController.FormPolicy, context =>
|
||||
Window(config, "ContactForm", 5, 600, context));
|
||||
limiter.AddPolicy(ContactMessagesController.TokenPolicy, context =>
|
||||
Window(config, "ContactToken", 20, 600, context));
|
||||
});
|
||||
}
|
||||
|
||||
public void UseModule(IApplicationBuilder app)
|
||||
{
|
||||
// Nothing is touched while the module is switched off: not even the database.
|
||||
var enabled = app.ApplicationServices.GetRequiredService<IOptions<ContactOptions>>().Value.Enabled;
|
||||
if (!enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
using var scope = app.ApplicationServices.CreateScope();
|
||||
var db = scope.ServiceProvider.GetRequiredService<ContactDbContext>();
|
||||
db.Database.Migrate();
|
||||
}
|
||||
|
||||
private static RateLimitPartition<string> Window(
|
||||
IConfiguration config, string name, int defaultPermits, int defaultSeconds, Microsoft.AspNetCore.Http.HttpContext context)
|
||||
{
|
||||
var section = config.GetSection($"RateLimiting:{name}");
|
||||
return RateLimitPartition.GetFixedWindowLimiter(
|
||||
ContactRateLimitPartition.KeyFor(context),
|
||||
_ => new FixedWindowRateLimiterOptions
|
||||
{
|
||||
PermitLimit = section.GetValue("PermitLimit", defaultPermits),
|
||||
Window = TimeSpan.FromSeconds(section.GetValue("WindowSeconds", defaultSeconds)),
|
||||
QueueLimit = 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user