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
@@ -214,10 +214,17 @@ public class CookieSessionValidatorTests
Assert.Equal(clock.GetUtcNow(), refreshedAt);
}
/// <summary>Verifies that a user whose group was revoked loses the roles on refresh.</summary>
/// <summary>
/// Verifies that a user whose every role mapping was revoked has the session ENDED on refresh
/// (ScadaBridge#144: no role, no login — and so no role-less session either). Before #144 the
/// session was replaced with a role-less principal that could still read bare-[Authorize] pages.
/// </summary>
/// <param name="failClosed">Whether the caller is an authoritative (fail-closed) check.</param>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ValidateAsync_RevokedGroupUser_LosesRolesOnRefresh()
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task ValidateAsync_RevokedGroupUser_RefreshToZeroRoles_EndsTheSession(bool failClosed)
{
var clock = new TestTimeProvider(Start);
var principal = Session(Start, ["SCADA-Admins"],
@@ -227,13 +234,44 @@ public class CookieSessionValidatorTests
var mapper = new FakeGroupRoleMapper(new RoleMappingResult([], [], IsSystemWideDeployment: false));
var sut = CreateValidator(mapper, clock);
clock.Advance(TimeSpan.FromMinutes(16));
var result = await sut.ValidateAsync(principal, failClosed: failClosed);
Assert.Equal(SessionValidationAction.Reject, result.Action);
Assert.Null(result.Principal);
Assert.Equal(1, mapper.CallCount);
}
/// <summary>A forced (circuit) refresh to zero roles ends the session too, before the threshold.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ValidateAsync_ForcedRefreshToZeroRoles_EndsTheSession()
{
var clock = new TestTimeProvider(Start);
var principal = Session(Start, ["SCADA-Admins"],
new RoleMappingResult([Roles.Administrator], [], true));
var sut = CreateValidator(new FakeGroupRoleMapper(new RoleMappingResult([], [], false)), clock);
var result = await sut.ValidateAsync(principal, recordActivity: false, forceRefresh: true);
Assert.Equal(SessionValidationAction.Reject, result.Action);
}
/// <summary>A refresh that keeps one role still replaces the principal (the zero-role rule is not over-broad).</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task ValidateAsync_RefreshToOneRole_KeepsTheSession()
{
var clock = new TestTimeProvider(Start);
var principal = Session(Start, ["SCADA-Admins"],
new RoleMappingResult([Roles.Administrator], [], true));
var sut = CreateValidator(new FakeGroupRoleMapper(new RoleMappingResult([Roles.Viewer], [], false)), clock);
clock.Advance(TimeSpan.FromMinutes(16));
var result = await sut.ValidateAsync(principal);
Assert.Equal(SessionValidationAction.Replace, result.Action);
// The user has lost ALL roles mid-session — every authorization policy will now deny.
Assert.Empty(result.Principal!.FindAll(JwtTokenService.RoleClaimType));
Assert.Empty(result.Principal.FindAll(JwtTokenService.SiteIdClaimType));
Assert.Equal([Roles.Viewer], result.Principal!.FindAll(JwtTokenService.RoleClaimType).Select(c => c.Value));
}
/// <summary>Verifies that validation does not refresh roles before the refresh threshold.</summary>