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:
@@ -102,5 +102,15 @@ public class PipelineTests : IClassFixture<ApiWebApplicationFactory>
|
||||
// request instead, which would otherwise mask a rate limiter that silently stopped
|
||||
// engaging).
|
||||
last!.StatusCode.Should().Be(HttpStatusCode.TooManyRequests);
|
||||
|
||||
// The rejection tells the client when to retry and carries a machine-readable body with
|
||||
// only the standard fields (nothing about the limiter, the policy or the caller).
|
||||
last.Headers.TryGetValues("Retry-After", out var retryAfter).Should().BeTrue();
|
||||
int.Parse(retryAfter!.Single()).Should().BeGreaterThanOrEqualTo(1);
|
||||
last.Content.Headers.ContentType?.MediaType.Should().Be("application/problem+json");
|
||||
var body = await last.Content.ReadAsStringAsync();
|
||||
using var json = System.Text.Json.JsonDocument.Parse(body);
|
||||
json.RootElement.EnumerateObject().Select(p => p.Name).Should().BeEquivalentTo(["type", "title", "status"]);
|
||||
json.RootElement.GetProperty("status").GetInt32().Should().Be(429);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
using System.Text.Json;
|
||||
using System.Threading.RateLimiting;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Observability;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Core.Tests.Hosting;
|
||||
|
||||
/// <summary>
|
||||
/// The 429 response of every rate-limit policy: a retry hint and a machine-readable body, and
|
||||
/// nothing about the limiter or the caller.
|
||||
/// </summary>
|
||||
public class RateLimiterOnRejectedTests
|
||||
{
|
||||
private sealed class TestLease(TimeSpan? retryAfter) : RateLimitLease
|
||||
{
|
||||
public override bool IsAcquired => false;
|
||||
public override IEnumerable<string> MetadataNames =>
|
||||
retryAfter is null ? [] : [MetadataName.RetryAfter.Name];
|
||||
|
||||
public override bool TryGetMetadata(string metadataName, out object? metadata)
|
||||
{
|
||||
if (retryAfter is not null && metadataName == MetadataName.RetryAfter.Name)
|
||||
{
|
||||
metadata = retryAfter.Value;
|
||||
return true;
|
||||
}
|
||||
|
||||
metadata = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class CapturingLoggerProvider : ILoggerProvider
|
||||
{
|
||||
public List<(EventId EventId, string Message)> Entries { get; } = [];
|
||||
|
||||
public ILogger CreateLogger(string categoryName) => new CapturingLogger(Entries);
|
||||
public void Dispose() { }
|
||||
|
||||
private sealed class CapturingLogger(List<(EventId, string)> entries) : ILogger
|
||||
{
|
||||
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
|
||||
public bool IsEnabled(LogLevel logLevel) => true;
|
||||
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception,
|
||||
Func<TState, Exception?, string> formatter) => entries.Add((eventId, formatter(state, exception)));
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<(HttpContext Context, string Body, CapturingLoggerProvider Logs)> RejectAsync(
|
||||
TimeSpan? retryAfter, string policy = "login")
|
||||
{
|
||||
var logs = new CapturingLoggerProvider();
|
||||
var services = new ServiceCollection();
|
||||
services.AddLogging(b => b.AddProvider(logs));
|
||||
services.AddCmsRateLimiting(new ConfigurationBuilder().Build());
|
||||
var provider = services.BuildServiceProvider();
|
||||
|
||||
var options = provider.GetRequiredService<IOptions<RateLimiterOptions>>().Value;
|
||||
|
||||
var context = new DefaultHttpContext { RequestServices = provider };
|
||||
context.Request.Path = "/api/v1/Auth/login";
|
||||
context.Response.Body = new MemoryStream();
|
||||
context.SetEndpoint(new Endpoint(
|
||||
_ => Task.CompletedTask,
|
||||
new EndpointMetadataCollection(new EnableRateLimitingAttribute(policy)),
|
||||
"test"));
|
||||
|
||||
await options.OnRejected!(
|
||||
new OnRejectedContext { HttpContext = context, Lease = new TestLease(retryAfter) },
|
||||
CancellationToken.None);
|
||||
|
||||
context.Response.Body.Position = 0;
|
||||
var body = await new StreamReader(context.Response.Body).ReadToEndAsync();
|
||||
return (context, body, logs);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RejectionStatusCode_Is429()
|
||||
{
|
||||
var provider = new ServiceCollection()
|
||||
.AddLogging()
|
||||
.AddCmsRateLimiting(new ConfigurationBuilder().Build())
|
||||
.BuildServiceProvider();
|
||||
|
||||
provider.GetRequiredService<IOptions<RateLimiterOptions>>().Value.RejectionStatusCode
|
||||
.Should().Be(StatusCodes.Status429TooManyRequests);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0.2, "1")]
|
||||
[InlineData(1.0, "1")]
|
||||
[InlineData(59.01, "60")]
|
||||
[InlineData(61.5, "62")]
|
||||
public async Task OnRejected_SetsRetryAfter_InWholeSecondsRoundedUp(double seconds, string expected)
|
||||
{
|
||||
var (context, _, _) = await RejectAsync(TimeSpan.FromSeconds(seconds));
|
||||
|
||||
context.Response.Headers.RetryAfter.ToString().Should().Be(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnRejected_FallsBackTo60Seconds_WhenTheLeaseReportsNoWait()
|
||||
{
|
||||
var (context, _, _) = await RejectAsync(retryAfter: null);
|
||||
|
||||
context.Response.Headers.RetryAfter.ToString().Should().Be("60");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnRejected_WritesProblemJson_WithOnlyTypeTitleAndStatus()
|
||||
{
|
||||
var (context, body, _) = await RejectAsync(TimeSpan.FromSeconds(30));
|
||||
|
||||
context.Response.ContentType.Should().StartWith("application/problem+json");
|
||||
using var json = JsonDocument.Parse(body);
|
||||
json.RootElement.EnumerateObject().Select(p => p.Name)
|
||||
.Should().BeEquivalentTo(["type", "title", "status"]);
|
||||
json.RootElement.GetProperty("status").GetInt32().Should().Be(429);
|
||||
json.RootElement.GetProperty("title").GetString().Should().Be("Too many requests");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnRejected_DoesNotLeakThePolicyPathOrTheLimiterInTheResponse()
|
||||
{
|
||||
var (context, body, _) = await RejectAsync(TimeSpan.FromSeconds(30), policy: "super-secret-policy");
|
||||
|
||||
body.Should().NotContain("super-secret-policy");
|
||||
body.Should().NotContain("/api/v1/Auth/login");
|
||||
body.ToLowerInvariant().Should().NotContain("permit").And.NotContain("window").And.NotContain("lease");
|
||||
context.Response.Headers.Keys.Should().BeEquivalentTo(["Retry-After", "Content-Type"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task OnRejected_StillLogsTheRateLimitSecurityEvent_ExactlyOnce()
|
||||
{
|
||||
// The message template is deliberately constant (see SecurityEventsTests); the policy and
|
||||
// path travel as structured properties, so only the event id is asserted here.
|
||||
var (_, _, logs) = await RejectAsync(TimeSpan.FromSeconds(30), policy: "login");
|
||||
|
||||
logs.Entries.Count(e => e.EventId.Id == SecurityEvents.RateLimitTriggeredEventId).Should().Be(1);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,241 @@
|
||||
using System.Reflection;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Routing;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using NSubstitute;
|
||||
using SlpModularCms.Core.Availability;
|
||||
using SlpModularCms.Core.Hosting;
|
||||
using SlpModularCms.Core.Hosting.Security;
|
||||
using SlpModularCms.Core.Observability;
|
||||
using SlpModularCms.Modules.Availability.Controllers;
|
||||
using SlpModularCms.Modules.Availability.Middleware;
|
||||
using SlpModularCms.Modules.Availability.Services;
|
||||
using SlpModularCms.Modules.Identity.Controllers;
|
||||
using SlpModularCms.Modules.Master.Controllers;
|
||||
using SlpModularCms.Modules.Offerings.Controllers;
|
||||
using Xunit;
|
||||
|
||||
namespace SlpModularCms.Modules.Availability.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// The availability statuses stop the CMS only. These tests pin down which endpoints stay
|
||||
/// reachable behind a closed gate (deny-by-default, marker per action) and prove that every
|
||||
/// other endpoint, admin routes included, is still blocked.
|
||||
/// </summary>
|
||||
public class PublicSiteEndpointGateTests
|
||||
{
|
||||
private readonly IAvailabilityService _service = Substitute.For<IAvailabilityService>();
|
||||
private readonly IMasterAvailabilityService _masterService = Substitute.For<IMasterAvailabilityService>();
|
||||
private readonly IAdminTokenValidator _adminTokenValidator = Substitute.For<IAdminTokenValidator>();
|
||||
private readonly RequestDelegate _next = Substitute.For<RequestDelegate>();
|
||||
private readonly AvailabilityMiddleware _middleware;
|
||||
|
||||
public PublicSiteEndpointGateTests()
|
||||
{
|
||||
_middleware = new AvailabilityMiddleware(
|
||||
_next, NullLogger<AvailabilityMiddleware>.Instance, _adminTokenValidator);
|
||||
|
||||
// No admin token on any request in these tests.
|
||||
_adminTokenValidator.Validate(Arg.Any<string>())
|
||||
.Returns(new AdminTokenResult(false, BypassRejectionReason.Absent));
|
||||
_masterService.GetMasterStatus().Returns(new MasterGateStatus(true, null));
|
||||
}
|
||||
|
||||
public enum ClosedGate
|
||||
{
|
||||
CmsDisabled,
|
||||
Maintenance,
|
||||
MasterBlocks,
|
||||
}
|
||||
|
||||
private void CloseGate(ClosedGate gate)
|
||||
{
|
||||
switch (gate)
|
||||
{
|
||||
case ClosedGate.CmsDisabled:
|
||||
_service.IsAvailableAsync().Returns(AvailabilityStatus.NotAvailable);
|
||||
break;
|
||||
case ClosedGate.Maintenance:
|
||||
_service.IsAvailableAsync().Returns(AvailabilityStatus.Maintenance);
|
||||
break;
|
||||
case ClosedGate.MasterBlocks:
|
||||
_service.IsAvailableAsync().Returns(AvailabilityStatus.Available);
|
||||
_masterService.GetMasterStatus().Returns(new MasterGateStatus(false, "Geblokkeerd door master"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<int> StatusFor(Endpoint? endpoint)
|
||||
{
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Path = "/api/v1/some-resource";
|
||||
context.Response.Body = new MemoryStream();
|
||||
if (endpoint is not null)
|
||||
{
|
||||
context.SetEndpoint(endpoint);
|
||||
}
|
||||
|
||||
_next.ClearReceivedCalls();
|
||||
await _middleware.InvokeAsync(context, _service, _masterService);
|
||||
|
||||
return _next.ReceivedCalls().Any() ? StatusCodes.Status200OK : context.Response.StatusCode;
|
||||
}
|
||||
|
||||
private static Endpoint EndpointWith(params object[] metadata)
|
||||
=> new(_ => Task.CompletedTask, new EndpointMetadataCollection(metadata), "test");
|
||||
|
||||
/// <summary>The metadata MVC would attach to an action: the controller's attributes, then the action's.</summary>
|
||||
private static Endpoint EndpointOf(MethodInfo action)
|
||||
{
|
||||
var metadata = action.DeclaringType!.GetCustomAttributes(inherit: true)
|
||||
.Concat(action.GetCustomAttributes(inherit: true))
|
||||
.ToArray();
|
||||
return new Endpoint(_ => Task.CompletedTask, new EndpointMetadataCollection(metadata), action.Name);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ClosedGate.CmsDisabled)]
|
||||
[InlineData(ClosedGate.Maintenance)]
|
||||
[InlineData(ClosedGate.MasterBlocks)]
|
||||
public async Task AMarkedEndpoint_StaysReachable_WhateverTheClosedGate(ClosedGate gate)
|
||||
{
|
||||
CloseGate(gate);
|
||||
|
||||
(await StatusFor(EndpointWith(new PublicSiteEndpointAttribute()))).Should().Be(200);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(ClosedGate.CmsDisabled)]
|
||||
[InlineData(ClosedGate.Maintenance)]
|
||||
[InlineData(ClosedGate.MasterBlocks)]
|
||||
public async Task AnUnmarkedEndpoint_IsStillBlocked(ClosedGate gate)
|
||||
{
|
||||
CloseGate(gate);
|
||||
|
||||
(await StatusFor(EndpointWith(new AuthorizeAttribute("AdminOnly")))).Should().Be(503);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ARequestWithoutEndpointMetadata_IsStillBlocked()
|
||||
{
|
||||
CloseGate(ClosedGate.CmsDisabled);
|
||||
|
||||
(await StatusFor(endpoint: null)).Should().Be(503);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheMarkerDoesNotBlockAnything_WhenTheCmsIsAvailable()
|
||||
{
|
||||
_service.IsAvailableAsync().Returns(AvailabilityStatus.Available);
|
||||
|
||||
(await StatusFor(EndpointWith(new PublicSiteEndpointAttribute()))).Should().Be(200);
|
||||
(await StatusFor(endpoint: null)).Should().Be(200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheAvailabilityStatusEndpoint_StaysOpen_BecauseOfItsPathBypass()
|
||||
{
|
||||
CloseGate(ClosedGate.CmsDisabled);
|
||||
var context = new DefaultHttpContext();
|
||||
context.Request.Path = "/api/v1/Availability/status";
|
||||
context.Response.Body = new MemoryStream();
|
||||
|
||||
await _middleware.InvokeAsync(context, _service, _masterService);
|
||||
|
||||
await _next.Received(1).Invoke(context);
|
||||
}
|
||||
|
||||
// ---- Route table: which actions of all modules carry the marker ----
|
||||
|
||||
/// <summary>
|
||||
/// The complete list of actions that stay reachable when the CMS is off. Extending it is a
|
||||
/// deliberate act: add the action here and in the code in the same change. (CMS-U3 adds the two
|
||||
/// public contact routes.)
|
||||
/// </summary>
|
||||
private static readonly string[] AllowedPublicActions =
|
||||
[
|
||||
$"{nameof(OfferingsController)}.{nameof(OfferingsController.GetOfferings)}",
|
||||
];
|
||||
|
||||
private static IReadOnlyList<MethodInfo> AllActions() =>
|
||||
new[]
|
||||
{
|
||||
typeof(AvailabilityController).Assembly,
|
||||
typeof(UsersController).Assembly,
|
||||
typeof(CmsInstanceController).Assembly,
|
||||
typeof(OfferingsController).Assembly,
|
||||
typeof(SystemController).Assembly,
|
||||
}
|
||||
.Distinct()
|
||||
.SelectMany(a => a.GetTypes())
|
||||
.Where(t => typeof(ControllerBase).IsAssignableFrom(t) && !t.IsAbstract)
|
||||
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
|
||||
.Where(m => m.GetCustomAttributes<HttpMethodAttribute>().Any())
|
||||
.ToList();
|
||||
|
||||
private static string Name(MethodInfo m) => $"{m.DeclaringType!.Name}.{m.Name}";
|
||||
|
||||
[Fact]
|
||||
public void TheMarkerIsOnExactlyTheAllowedPublicActions()
|
||||
{
|
||||
var actions = AllActions();
|
||||
actions.Count.Should().BeGreaterThan(20, "the reflection must actually find the controllers of the modules");
|
||||
|
||||
actions.Where(m => m.GetCustomAttribute<PublicSiteEndpointAttribute>() is not null)
|
||||
.Select(Name)
|
||||
.Should().BeEquivalentTo(AllowedPublicActions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AMarkedActionIsAnonymous_AndNeverAnAuthorizedOne()
|
||||
{
|
||||
foreach (var action in AllActions().Where(m => m.GetCustomAttribute<PublicSiteEndpointAttribute>() is not null))
|
||||
{
|
||||
action.GetCustomAttribute<AllowAnonymousAttribute>()
|
||||
.Should().NotBeNull($"{Name(action)} is public so it must be explicitly anonymous");
|
||||
action.GetCustomAttributes<AuthorizeAttribute>()
|
||||
.Should().BeEmpty($"{Name(action)} must not require authorization");
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task EveryOtherAction_IncludingAllAdminRoutes_IsBlockedByAClosedGate()
|
||||
{
|
||||
CloseGate(ClosedGate.CmsDisabled);
|
||||
var others = AllActions()
|
||||
.Where(m => m.GetCustomAttribute<PublicSiteEndpointAttribute>() is null)
|
||||
.ToList();
|
||||
others.Should().NotBeEmpty();
|
||||
|
||||
var reachable = new List<string>();
|
||||
foreach (var action in others)
|
||||
{
|
||||
if (await StatusFor(EndpointOf(action)) != 503)
|
||||
{
|
||||
reachable.Add(Name(action));
|
||||
}
|
||||
}
|
||||
|
||||
reachable.Should().BeEmpty("only marked actions may pass a closed gate");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TheMarkedActions_AreTheOnlyOnesThatPassAClosedGate()
|
||||
{
|
||||
CloseGate(ClosedGate.Maintenance);
|
||||
|
||||
var passing = new List<string>();
|
||||
foreach (var action in AllActions())
|
||||
{
|
||||
if (await StatusFor(EndpointOf(action)) == 200)
|
||||
{
|
||||
passing.Add(Name(action));
|
||||
}
|
||||
}
|
||||
|
||||
passing.Should().BeEquivalentTo(AllowedPublicActions);
|
||||
}
|
||||
}
|
||||
+8
@@ -31,4 +31,12 @@
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Availability\SlpModularCms.Modules.Availability.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Only for the route-table test (PublicSiteEndpointGateTests): it inspects the controllers of
|
||||
every module a host can load to prove which endpoints stay reachable behind the gate. -->
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Identity\SlpModularCms.Modules.Identity.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Master\SlpModularCms.Modules.Master.csproj" />
|
||||
<ProjectReference Include="..\SlpModularCms.Modules.Offerings\SlpModularCms.Modules.Offerings.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -69,6 +69,15 @@ public class AvailabilityMiddleware
|
||||
return;
|
||||
}
|
||||
|
||||
// Public site functionality (e.g. the offerings list the website shows) stays reachable
|
||||
// whatever the status: the statuses only stop the CMS. Deny-by-default: only an endpoint
|
||||
// that explicitly carries the marker passes here.
|
||||
if (context.GetEndpoint()?.Metadata.GetMetadata<PublicSiteEndpointAttribute>() is not null)
|
||||
{
|
||||
await _next(context);
|
||||
return;
|
||||
}
|
||||
|
||||
var admin = CheckAdmin(context);
|
||||
if (admin.IsVerifiedAdmin)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
using System.Reflection;
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using SlpModularCms.Core.Availability;
|
||||
using SlpModularCms.Modules.Offerings.Controllers;
|
||||
|
||||
namespace SlpModularCms.Modules.Offerings.Tests.Controllers;
|
||||
|
||||
public class OfferingsPublicMarkerTests
|
||||
{
|
||||
private static IEnumerable<MethodInfo> Actions() =>
|
||||
new[] { typeof(OfferingsController), typeof(OfferingCategoriesController) }
|
||||
.SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly));
|
||||
|
||||
[Fact]
|
||||
public void GetOfferings_IsMarkedAsPublicSiteEndpoint_AndAnonymous()
|
||||
{
|
||||
var action = typeof(OfferingsController).GetMethod(nameof(OfferingsController.GetOfferings))!;
|
||||
|
||||
action.GetCustomAttribute<PublicSiteEndpointAttribute>().Should().NotBeNull();
|
||||
action.GetCustomAttribute<AllowAnonymousAttribute>().Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NoOtherOfferingsAction_CarriesTheMarker()
|
||||
{
|
||||
Actions()
|
||||
.Where(m => m.Name != nameof(OfferingsController.GetOfferings))
|
||||
.Where(m => m.GetCustomAttribute<PublicSiteEndpointAttribute>() is not null)
|
||||
.Should().BeEmpty("writes and the category management stay behind the availability gate");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.RateLimiting;
|
||||
using SlpModularCms.Core.Availability;
|
||||
using SlpModularCms.Modules.Offerings.Models;
|
||||
using SlpModularCms.Modules.Offerings.Services;
|
||||
|
||||
@@ -20,6 +21,7 @@ public class OfferingsController(IOfferingsService service) : ControllerBase
|
||||
{
|
||||
[HttpGet]
|
||||
[AllowAnonymous]
|
||||
[PublicSiteEndpoint]
|
||||
[EnableRateLimiting("offerings-public")]
|
||||
public async Task<IActionResult> GetOfferings()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user