Separates website and admin roots, adds /health, hardens the availability gate

Prepares the single-host layout for deployment. The customer's public
website moves from wwwroot/ to wwwroot/web/, so a CMS deploy can no
longer overwrite content it does not own: with the website in its own
directory, the release directory can be swapped without touching it.

Each front-end gets its own file provider, and both tolerate a missing
directory at startup — a fresh deployment has no website until a
separate workspace deploys one, and the CMS must still serve /admin and
the API. When the website's index.html is absent, an embedded
placeholder is served instead of a 404, which also doubles as proof the
CMS itself is running. The placeholder is embedded in the assembly
rather than shipped into wwwroot/web/, because that directory is owned
and overwritten by the website workspace.

Adds GET /health for uptime monitoring. It reports infrastructure
liveness only and is deliberately NOT the same thing as
/api/v1/Availability/status or /api/v1/System/capabilities: those are
CMS domain state that also serve the master/slave protocol. A healthy
instance can be switched off by design, and a switched-on instance can
be unhealthy, so conflating them would alert on business state and stay
silent on real outages. /health is on the availability gate's bypass
list for the same reason.

Fixes a real defect in the gate's admin bypass. It parsed the bearer
token with ReadJwtToken, which reads claims without verifying the
signature, so an unauthenticated caller could forge an unsigned token
carrying an Owner role claim and bypass the gate that suspends a
customer's site. Protected endpoints still rejected them, so nothing
leaked — but the gate itself was bypassable. The token is now fully
validated against the same parameters as the bearer scheme, resolved
from one shared source so the two cannot drift apart.

Host wiring for these changes lands with the data-durability commit,
since both units touch the same lines of Program.cs.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HHoJpxYXzHACSQguHrC5fw
This commit is contained in:
2026-07-28 00:00:13 +02:00
co-authored by Claude Opus 5
parent 8568ca43c6
commit 29a93ef873
21 changed files with 1677 additions and 86 deletions
@@ -0,0 +1,64 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;
namespace SlpModularCms.Core.Hosting.Security;
/// <summary>
/// Validates a bearer token against the application's JWT validation parameters and checks
/// for an administrative role.
/// </summary>
/// <remarks>
/// This replaces an earlier implementation that parsed the token with
/// <c>JwtSecurityTokenHandler.ReadJwtToken</c> — which reads claims WITHOUT verifying the
/// signature. Under that implementation an unauthenticated caller could present a self-made,
/// unsigned token carrying an Owner role claim and bypass the availability gate. Protected
/// endpoints still rejected such a caller, so no data was exposed, but the gate that suspends
/// a customer's site could be bypassed by anyone who knew the claim name.
/// </remarks>
public sealed class AdminTokenValidator : IAdminTokenValidator
{
private const string BearerPrefix = "Bearer ";
private static readonly string[] AdminRoles = ["Owner", "Administrator"];
private readonly TokenValidationParameters _validationParameters;
private readonly JwtSecurityTokenHandler _handler = new();
public AdminTokenValidator(TokenValidationParameters validationParameters)
{
_validationParameters = validationParameters ?? throw new ArgumentNullException(nameof(validationParameters));
}
public bool IsVerifiedAdmin(string? authorizationHeader)
{
if (string.IsNullOrEmpty(authorizationHeader) ||
!authorizationHeader.StartsWith(BearerPrefix, StringComparison.OrdinalIgnoreCase))
{
return false;
}
var token = authorizationHeader[BearerPrefix.Length..].Trim();
if (token.Length == 0)
{
return false;
}
ClaimsPrincipal principal;
try
{
// Validates signature, issuer, audience and lifetime. A forged or expired token
// throws here and is treated as "not an admin" rather than as an error — deciding
// whether to serve is this component's job; returning 401 is not.
principal = _handler.ValidateToken(token, _validationParameters, out _);
}
catch (Exception)
{
return false;
}
return AdminRoles.Any(role => principal.IsInRole(role))
|| principal.FindAll(ClaimTypes.Role).Any(c => AdminRoles.Contains(c.Value))
|| principal.FindAll("role").Any(c => AdminRoles.Contains(c.Value));
}
}
@@ -0,0 +1,24 @@
namespace SlpModularCms.Core.Hosting.Security;
/// <summary>
/// Decides whether a request carries a genuinely valid Owner or Administrator token.
/// </summary>
/// <remarks>
/// Used by the availability gate, which runs before authentication middleware and therefore
/// cannot read <c>HttpContext.User</c>. Validation uses the same parameters as the JWT bearer
/// scheme (see <see cref="JwtTokenValidation"/>), so the gate can never be more permissive
/// than authentication itself.
/// </remarks>
public interface IAdminTokenValidator
{
/// <summary>
/// Returns true only when the supplied Authorization header contains a bearer token that
/// validates successfully and carries the Owner or Administrator role.
/// </summary>
/// <param name="authorizationHeader">Raw Authorization header value; may be null or empty.</param>
/// <returns>
/// True when the caller is a verified Owner or Administrator; false in every other case,
/// including an absent, malformed, forged, expired or non-admin token. Never throws.
/// </returns>
bool IsVerifiedAdmin(string? authorizationHeader);
}