Beschikbaarheidspoort laat publieke site-endpoints door en 429 geeft Retry-After met ProblemDetails
Nieuwe marker [PublicSiteEndpoint] (deny-by-default): alleen GET /offerings blijft bereikbaar als het CMS uit staat of in onderhoud is; alle andere acties, adminroutes inbegrepen, blijven 503. OnRejected zet Retry-After en schrijft application/problem+json met alleen type, title en status. Op master zijn de limiet-policies nog globale buckets: de contactroutes mogen pas live na de per-client-partitionering van de ASF-branch. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
namespace SlpModularCms.Core.Availability;
|
||||
|
||||
/// <summary>
|
||||
/// Marks an endpoint as public site functionality that stays reachable while the CMS is
|
||||
/// switched off, in maintenance or blocked by the master.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The availability statuses exist to stop the CMS (changing data and settings through the
|
||||
/// admin). Visitors of the public website must keep working, so the few endpoints the website
|
||||
/// itself calls carry this marker. The gate is deny-by-default: an endpoint without the marker
|
||||
/// is blocked, and the marker must never be placed on an endpoint that requires authorization.
|
||||
/// It is read from the endpoint metadata by <c>AvailabilityMiddleware</c>.
|
||||
/// </remarks>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
|
||||
public sealed class PublicSiteEndpointAttribute : Attribute;
|
||||
@@ -171,7 +171,7 @@ public static class ServiceCollectionExtensions
|
||||
// Without this callback a brute-force attempt against /api/v1/Auth/login returns 429
|
||||
// and leaves NO trace anywhere — the one rate limiter this application has would be
|
||||
// entirely unobservable, and the alert rule for it could never fire.
|
||||
options.OnRejected = (context, _) =>
|
||||
options.OnRejected = async (context, cancellationToken) =>
|
||||
{
|
||||
var logger = context.HttpContext.RequestServices
|
||||
.GetRequiredService<ILoggerFactory>()
|
||||
@@ -202,11 +202,24 @@ public static class ServiceCollectionExtensions
|
||||
var window = configuration.GetValue<int>($"RateLimiting:{section}:WindowSeconds", 60);
|
||||
waitSeconds = policyName == "refresh" ? window / 4.0 : window;
|
||||
}
|
||||
// Every 429 carries a retry hint, also for policies that report no wait (the contact and offerings policies).
|
||||
waitSeconds ??= 60;
|
||||
if (waitSeconds is not null)
|
||||
context.HttpContext.Response.Headers.RetryAfter =
|
||||
Math.Max(1, (int)Math.Ceiling(waitSeconds.Value)).ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
|
||||
return ValueTask.CompletedTask;
|
||||
|
||||
// Machine-readable body with only the standard type/title/status: nothing about the limiter, the policy or the caller.
|
||||
await context.HttpContext.Response.WriteAsJsonAsync(
|
||||
new Microsoft.AspNetCore.Mvc.ProblemDetails
|
||||
{
|
||||
Type = "https://www.rfc-editor.org/rfc/rfc6585#section-4",
|
||||
Title = "Too many requests",
|
||||
Status = StatusCodes.Status429TooManyRequests,
|
||||
},
|
||||
options: null,
|
||||
contentType: "application/problem+json",
|
||||
cancellationToken: cancellationToken);
|
||||
};
|
||||
|
||||
// Every named policy is partitioned per client address (ClientPartitionKey). Behind a
|
||||
|
||||
Reference in New Issue
Block a user