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>
@@ -27,6 +27,9 @@ public class DirectoryGroupRefreshTests
}
/// <summary>Maps each group to a role by name (Admins→Administrator, Designers→Designer, else Viewer).</summary>
// A group that maps to no ScadaBridge role (e.g. "Domain Users" under a domain-wide search base).
private const string UnmappedGroup = "Domain Users";
private sealed class NamingMapper : IGroupRoleMapper<string>
{
public int Calls { get; private set; }
@@ -40,8 +43,9 @@ public class DirectoryGroupRefreshTests
{
"SCADA-Admins" => Roles.Administrator,
"SCADA-Designers" => Roles.Designer,
UnmappedGroup => null,
_ => Roles.Viewer,
}).Distinct().ToList();
}).OfType<string>().Distinct().ToList();
var result = new RoleMappingResult(roles, [], IsSystemWideDeployment: true);
return Task.FromResult(new GroupRoleMapping<string>(roles, Scope: result));
}
@@ -255,6 +259,41 @@ public class DirectoryGroupRefreshTests
Assert.Equal(SessionValidationAction.Reject, result.Action);
}
/// <summary>
/// ScadaBridge#144: the directory still answers with groups, but none of them maps to a role
/// (the user was removed from every mapped AD group and keeps only e.g. "Domain Users"). Before
/// #144 this kept a signed-in, role-less session; a new login would now be refused, so the
/// session ends — on ordinary validation and on the fail-closed authoritative path.
/// </summary>
[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task FoundWithOnlyUnmappedGroups_EndsTheSession(bool failClosed)
{
var rig = Build();
rig.Lookup.Answer = _ => FoundWith(UnmappedGroup);
rig.Clock.Advance(TimeSpan.FromMinutes(16));
var result = await rig.Sut.ValidateAsync(Login(["SCADA-Admins"]), forceRefresh: failClosed, failClosed: failClosed);
Assert.Equal(SessionValidationAction.Reject, result.Action);
Assert.Equal([UnmappedGroup], rig.Mapper.LastGroups);
}
/// <summary>A directory answer that keeps one mapped group alongside unmapped ones keeps the session.</summary>
[Fact]
public async Task FoundWithOneMappedGroup_KeepsTheSession()
{
var rig = Build();
rig.Lookup.Answer = _ => FoundWith(UnmappedGroup, "SCADA-Designers");
rig.Clock.Advance(TimeSpan.FromMinutes(16));
var result = await rig.Sut.ValidateAsync(Login(["SCADA-Admins"]));
Assert.Equal(SessionValidationAction.Replace, result.Action);
Assert.Equal([Roles.Designer], RolesOf(result.Principal!));
}
[Theory]
[InlineData(LdapGroupLookupStatus.NotSupported)]
[InlineData(LdapGroupLookupStatus.ProviderDisabled)]