fix(auth): end a cookie session whose roles drop to zero on refresh (#144)

The mid-session refresh ended a session only when the directory returned no
groups (or the user was gone/disabled/ambiguous). A user still in some groups
but in no mapped one, or whose DB mappings were all revoked, kept a signed-in,
role-less session that could read the bare-[Authorize] pages. CookieSessionValidator
now rejects when the re-map yields zero roles, on ordinary and fail-closed paths.
A mapper fault keeps its existing policy (keep grants; fail closed when asked).

Docs: Component-Security "No role, no login" (incl. the HTTP Basic gap left as a
follow-up), Component-CentralUI, CLAUDE.md.

Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
This commit is contained in:
Joseph Doherty
2026-11-17 11:21:00 -05:00
parent 5a642d1cdf
commit 2d8d5a3d6f
7 changed files with 119 additions and 14 deletions
@@ -37,7 +37,7 @@ public enum SessionValidationAction
/// <summary>Leave the principal as-is (no idle timeout, no refresh due, or a refresh error we swallow).</summary>
Keep,
/// <summary>The session is idle-timed-out, or the directory says the user can no longer sign in; reject + sign out.</summary>
/// <summary>The session is idle-timed-out, the directory says the user can no longer sign in, or the user's groups now map to no role; reject + sign out.</summary>
Reject,
/// <summary>The role mapping was refreshed; replace the principal. The HTTP adapter decides whether renewal is appropriate.</summary>
@@ -66,7 +66,9 @@ public enum SessionValidationAction
/// = 15): when the elapsed time since <see cref="JwtTokenService.LastRoleRefreshClaimType"/>
/// exceeds the threshold, the stored groups are re-mapped and the principal is rebuilt via
/// <see cref="SessionClaimBuilder"/> (identical shape to <c>/auth/login</c>). If the DB
/// mapping revoked the user's roles, the rebuilt principal reflects the loss.
/// mapping revoked some of the user's roles, the rebuilt principal reflects the loss; if it revoked
/// ALL of them, the session ends (<see cref="SessionValidationResult.Reject"/>) — the same
/// "no role, no login" rule <c>/auth/login</c> applies (ScadaBridge#144).
/// </para>
/// <para>
/// <b>Directory group re-query</b> (ScadaBridge#46): when the session's
@@ -251,6 +253,19 @@ public sealed class CookieSessionValidator
// Re-run the DB-backed mapping on the (possibly just re-read) groups.
var mapping = await _roleMapper.MapAsync(groups, ct).ConfigureAwait(false);
// No role, no session (ScadaBridge#144): a login whose groups map to no role is refused, so a
// session whose groups now map to no role ends the same way — on the DB-only re-map and after
// a directory re-query alike. A mapper FAULT is not this case: it throws above and the failure
// policy in ValidateAsync applies (keep on ordinary validation, propagate when fail-closed).
if (mapping.Roles is not { Count: > 0 })
{
_logger.LogWarning(
"Cookie session for {Username} ended: its {GroupCount} group(s) now map to no ScadaBridge role (a new sign-in would be refused).",
username, groups.Count);
return SessionValidationResult.Reject;
}
var scope = mapping.Scope is RoleMappingResult mapped
? mapped
: new RoleMappingResult(mapping.Roles, [], IsSystemWideDeployment: false);
@@ -254,7 +254,7 @@ public static class ServiceCollectionExtensions
/// adapter. It resolves the request-scoped <see cref="CookieSessionValidator"/>,
/// asks it for a decision, and applies it to the cookie context:
/// <list type="bullet">
/// <item><see cref="SessionValidationAction.Reject"/> → <see cref="CookieValidatePrincipalContext.RejectPrincipal"/> + sign out (idle timeout, or the directory reports the user gone/disabled/ambiguous/group-less — ScadaBridge#46).</item>
/// <item><see cref="SessionValidationAction.Reject"/> → <see cref="CookieValidatePrincipalContext.RejectPrincipal"/> + sign out (idle timeout, the directory reports the user gone/disabled/ambiguous/group-less — ScadaBridge#46, or the user's groups now map to no role — ScadaBridge#144).</item>
/// <item><see cref="SessionValidationAction.Replace"/> → <see cref="CookieValidatePrincipalContext.ReplacePrincipal"/> + renewal only for non-passive requests (role mapping refreshed).</item>
/// <item><see cref="SessionValidationAction.Keep"/> → no-op (no refresh due, or a swallowed refresh fault).</item>
/// </list>