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:
2026-11-10 13:07:18 +01:00
co-authored by Claude Sonnet 5.5
parent c3c8bcfd5e
commit a58374abeb
12 changed files with 510 additions and 10 deletions
@@ -0,0 +1,18 @@
# CMS-U2 gate-and-limiter: code summary
## What changed
- `Core/Availability/PublicSiteEndpointAttribute` (sealed, methods only). `AvailabilityMiddleware`: right after the prefix bypass, an endpoint whose metadata carries the marker is let through before the master/local gate (deny-by-default; prefix bypasses and the verified-admin bypass untouched). Marked now: `OfferingsController.GetOfferings` only.
- `AddCmsRateLimiting.OnRejected`: still logs the security event, now also sets `Retry-After` (whole seconds, rounded up, minimum 1, from the limiter lease; 60 when the lease reports none) and writes `application/problem+json` with only `type`, `title`, `status`. Policy numbers and bucket behaviour are unchanged.
## RELEASE CONDITION (must be met before the contact routes go live)
On the base branch (master) the rate-limit policies are **global buckets**: there is no per-client partition key and no ForwardedHeaders handling (both exist only on the ASF branch `feature/identity-email-notifications`). Any per-IP policy added in CMS-U3 (ContactForm 5 per 10 minutes, ContactToken 20 per 10 minutes, ContactResend) would therefore be shared by all visitors: after five submissions the whole site would be blocked. **The contact routes must not be enabled on a public host before the per-client partitioning from the ASF branch is on the base branch (after the ASF merge and the rebase).** Also required on that host: `ForwardedHeaders:KnownProxies` (env). Recorded in contract § 4.5 and repeated in the CMS-U3 plan.
## Verification (real output)
- Build: 0 errors. Tests: Core.Tests 205 passed (9 new in RateLimiterOnRejectedTests: Retry-After rounding, fallback 60, problem+json with exactly type/title/status, no leak of policy name/path/limiter terms, only `Retry-After` and `Content-Type` headers, security event logged once, 429 status), Availability.Tests 95 passed (13 new in PublicSiteEndpointGateTests: marked endpoint passes for CMS disabled / maintenance / master blocks; unmarked, and no endpoint, still 503; Availability/status open; route table: marker on exactly `OfferingsController.GetOfferings`, marked actions are anonymous and never `[Authorize]`, every other action of the Availability/Identity/Master/Offerings/System controllers answers 503 at a closed gate, and the only action that passes is `GetOfferings`), Offerings.Tests 86 passed (2 new marker tests), Identity.Tests 37, Master.Tests 57, Api.Tests 4 passed with a temporary MariaDB (the existing Sentry-tunnel limiter test now also asserts Retry-After, `application/problem+json` and the three-field body).
- Real pipeline smoke (SlpSoftware host against the temporary MariaDB, CMS state set via the AvailabilityState table): with the CMS **disabled** `GET /api/v1/offerings` 200, `GET /api/v1/offerings/categories` 503, `GET /api/v1/System/capabilities` 503, `GET /api/v1/Availability/status` 200, unauthenticated `PUT /api/v1/offerings/order` 503; in **maintenance** offerings 200 and capabilities 503. The host was stopped and the container removed afterwards.
## Handoff
Public site endpoints stay reachable when the CMS is off or in maintenance; the frontend 503 fallback now only applies to admin calls. Extending the public set is deliberate: add the action to `AllowedPublicActions` in `PublicSiteEndpointGateTests` in the same change (CMS-U3 adds the contact token GET and POST).
## Rebase notes (after the ASF merge)
Expected conflicts: `AvailabilityMiddleware.cs` (one block) and `AddCmsRateLimiting` (`OnRejected`). Re-run the Core, Availability and Offerings tests and the Api.Tests after the rebase.
@@ -13,18 +13,18 @@ Phase 1, base: current branch (master `d9cc539` + CMS-U0/U1/U1b). Decisions: D6
3. **Marked now**: only `OfferingsController.GetOfferings`. (U3 adds the two contact routes.)
## Steps
- [ ] **Step 1 — Core**: add `Core/Availability/PublicSiteEndpointAttribute.cs`.
- [ ] **Step 2 — Middleware (minimal diff)**: in `AvailabilityMiddleware.InvokeAsync`, right after the prefix-bypass block, add the endpoint-metadata check (4 lines + comment). No other change.
- [ ] **Step 3 — OnRejected (minimal diff)**: make the lambda `async`, keep the security event call, then set `Retry-After` and write the ProblemDetails JSON.
- [ ] **Step 4 — Mark** `GetOfferings` with `[PublicSiteEndpoint]` (Offerings module).
- [ ] **Step 5 — Tests**:
- [x] **Step 1 — Core**: add `Core/Availability/PublicSiteEndpointAttribute.cs`.
- [x] **Step 2 — Middleware (minimal diff)**: in `AvailabilityMiddleware.InvokeAsync`, right after the prefix-bypass block, add the endpoint-metadata check (4 lines + comment). No other change.
- [x] **Step 3 — OnRejected (minimal diff)**: make the lambda `async`, keep the security event call, then set `Retry-After` and write the ProblemDetails JSON.
- [x] **Step 4 — Mark** `GetOfferings` with `[PublicSiteEndpoint]` (Offerings module).
- [x] **Step 5 — Tests**:
- Core.Tests: `OnRejected` (resolved from `IOptions<RateLimiterOptions>` after `AddCmsRateLimiting`, called with a fake lease): sets `Retry-After` rounded up from the lease value (e.g. 0.2 s -> 1, 61.5 s -> 62), falls back to 60 without lease metadata, writes `application/problem+json` with status 429, still logs the security event with policy name and path, status code is 429.
- Api.Tests (existing real-pipeline test `Sentry_tunnel_rate_limiter_engages_after_the_configured_permit_limit`): additionally assert the `Retry-After` header and the problem+json content type on the 429 (needs the local database; run with the container).
- Availability.Tests (middleware): marked endpoint passes while the system is disabled, in maintenance, and with the master gate closed; an unmarked endpoint with the same state still gets 503; a marked endpoint does not bypass anything else (a request without endpoint metadata is still blocked); `/api/v1/Availability/status` stays open.
- Availability.Tests (route-table style, SLS request): reflection over the controllers of the referenced modules (Availability, Identity, Master, Offerings; test-only project references): (a) the set of actions carrying the marker is exactly `{ OfferingsController.GetOfferings }` (the test names the allowlist so CMS-U3 extends it deliberately); (b) for every other controller action the middleware, given an endpoint with that action's attributes and a closed gate, answers 503 — which covers all admin routes, and the marker is never on an `AdminOnly`/`[Authorize]` action.
- Offerings.Tests: `GetOfferings` has the marker and `[AllowAnonymous]`; no other action of the controller has the marker.
- [ ] **Step 6 — Verification (automatic)**: build solution; Core, Availability, Offerings, Identity, Master tests; Api.Tests with a temporary MariaDB container (started for the run and removed afterwards, as agreed); report real output.
- [ ] **Step 7 — Docs/commit**: `construction/cms-u2-gate-and-limiter/code/summary.md` incl. handoff note (public routes stay reachable when the CMS is disabled/maintenance; frontend 503 fallback now only applies to admin calls); one commit, no push.
- [x] **Step 6 — Verification (automatic)**: build solution; Core, Availability, Offerings, Identity, Master tests; Api.Tests with a temporary MariaDB container (started for the run and removed afterwards, as agreed); report real output.
- [x] **Step 7 — Docs/commit**: `construction/cms-u2-gate-and-limiter/code/summary.md` incl. handoff note (public routes stay reachable when the CMS is disabled/maintenance; frontend 503 fallback now only applies to admin calls); one commit, no push.
## Explicitly NOT touched
Policy numbers and partitioning (no `ClientPartitionKey` here; that is the ASF branch), ForwardedHeaders, host config, Email, Identity, Master, bypass-prefix list, other controllers' routes, `POST admin/status` (CMS-U7, phase 2).